private void Client_BinaryReceived(object sender, BinaryReceivedEventArgs e)
        {
            var args = new ServerReceivedBinaryEventArgs(sender as Connection, e);

            BinaryReceived?.Invoke(this, args);
            OnBinaryReceived(args);
        }
Exemple #2
0
 private void HandleIncomingBinary(Frame frame)
 {
     binary.AddRange(frame.UnmaskedData);
     if (frame.FIN)
     {
         BinaryReceived?.Invoke(this as T, binary.ToArray());
         binary.Clear();
     }
     else
     {
         currentOpCode = OpCode.BinaryFrame;
     }
 }
Exemple #3
0
        private async void Read()
        {
            byte[] bytes = null; OpCode operation = default;
            while (State == State.Open)
            {
                try
                {
                    var frame = await GetFrameInfo();

                    if (frame.OpCode == OpCode.Ping)
                    {
                        Pong(frame);
                    }
                    else if (frame.OpCode == OpCode.ConnectionClose)
                    {
                        HandleCloseRequest(frame);
                        break;
                    }
                    else
                    {
                        if (frame.OpCode == OpCode.ContinuationFrame)
                        {
                            bytes = (bytes == null) ? frame.UnmaskedData : bytes.Concat(frame.UnmaskedData).ToArray();
                        }
                        else
                        {
                            bytes     = frame.UnmaskedData;
                            operation = frame.OpCode;
                        }
                        if (!frame.FIN)
                        {
                            continue;
                        }
                        if (operation == OpCode.TextFrame)
                        {
                            string msg = Encoding.UTF8.GetString(bytes);
                            MessageReceived?.Invoke(this as T, msg);
                        }
                        else
                        {
                            BinaryReceived?.Invoke(this as T, (byte[])bytes.Clone());
                        }
                        bytes = null;
                    }
                }
                catch { Close(); }
            }
        }
        private void HandleFrame(Frame frame)
        {
            if (frame.FIN)
            {
                switch (frame.OpCode)
                {
                case OpCode.TextFrame:
                    binary.AddRange(frame.UnmaskedData);
                    TextReceived?.Invoke(this, Encoding.UTF8.GetString(binary.ToArray()));
                    break;

                case OpCode.BinaryFrame:
                    binary.AddRange(frame.UnmaskedData);
                    BinaryReceived?.Invoke(this, binary.ToArray());
                    break;

                case OpCode.ConnectionClose:
                    HandleCloseRequest(frame);
                    break;

                case OpCode.Ping:
                    Pong(frame);
                    break;

                case OpCode.ContinuationFrame:
                    if (currentOpCode == OpCode.TextFrame)
                    {
                        TextReceived?.Invoke(this, Encoding.UTF8.GetString(binary.ToArray()));
                    }
                    else
                    {
                        BinaryReceived?.Invoke(this, binary.ToArray());
                    }
                    break;

                default:
                    Close();
                    break;
                }
                binary.Clear();
            }
            else
            {
                binary.AddRange(frame.UnmaskedData);
                currentOpCode = frame.OpCode;
            }
        }
Exemple #5
0
        private void HandleFrame(Frame frame)
        {
            if (frame.FIN)
            {
                switch (frame.OpCode)
                {
                case OpCode.TextFrame:
                    var ignore1 = Task.Run(() => TextReceived?.Invoke(this, new TextReceivedEventArgs(Encoding.UTF8.GetString(frame.UnmaskedData))));
                    break;

                case OpCode.BinaryFrame:
                    var ignore2 = Task.Run(() => BinaryReceived?.Invoke(this, new BinaryReceivedEventArgs(frame.UnmaskedData)));
                    break;

                case OpCode.ConnectionClose:
                    HandleCloseRequest(frame);
                    break;

                case OpCode.Ping:
                    Pong(frame);
                    break;

                case OpCode.ContinuationFrame:
                    binary.AddRange(frame.UnmaskedData);
                    if (currentOpCode == OpCode.TextFrame)
                    {
                        var ignore3 = Task.Run(() => TextReceived?.Invoke(this, new TextReceivedEventArgs(Encoding.UTF8.GetString(binary.ToArray()))));
                    }
                    else
                    {
                        var ignore4 = Task.Run(() => BinaryReceived?.Invoke(this, new BinaryReceivedEventArgs(binary.ToArray())));
                    }
                    break;

                default:
                    Close();
                    break;
                }
                binary.Clear();
            }
            else
            {
                binary.AddRange(frame.UnmaskedData);
                currentOpCode = frame.OpCode;
            }
        }
        public async Task ReceiveAsync()
        {
            if (_cts != null)
            {
                return;
            }
            _cts = new CancellationTokenSource();
            _ws  = new ClientWebSocket();
            await _ws.ConnectAsync(new Uri(_url), _cts.Token);

            Opened?.Invoke(this, EventArgs.Empty);

            var          arr   = new ArraySegment <byte>(_buf);
            int          count = 0;
            MemoryStream ms    = new MemoryStream();

            while (true)
            {
                var result = await _ws.ReceiveAsync(arr, _cts.Token);

                if (result.MessageType == WebSocketMessageType.Close)
                {
                    break;
                }
                else if (result.MessageType == WebSocketMessageType.Text)
                {
                    if (result.EndOfMessage)
                    {
                        string s;
                        if (count == 0)
                        {
                            s = Encoding.UTF8.GetString(_buf, 0, result.Count);
                        }
                        else
                        {
                            ms.Write(_buf, 0, result.Count);
                            var k = ms.ToArray();
                            s     = Encoding.UTF8.GetString(k, 0, k.Length);
                            ms    = new MemoryStream();
                            count = 0;
                        }
                        Received?.Invoke(this, s);
                    }
                    else
                    {
                        ms.Write(_buf, 0, result.Count);
                        count++;
                    }
                }
                else if (result.MessageType == WebSocketMessageType.Binary)
                {
                    if (result.EndOfMessage)
                    {
                        byte[] s;
                        if (count == 0)
                        {
                            s = _buf.Take(result.Count).ToArray();// Encoding.UTF8.GetString(_buf, 0, result.Count);
                        }
                        else
                        {
                            ms.Write(_buf, 0, result.Count);
                            var k = ms.ToArray();
                            s     = k;
                            ms    = new MemoryStream();
                            count = 0;
                        }
                        BinaryReceived?.Invoke(this, s);
                    }
                    else
                    {
                        ms.Write(_buf, 0, result.Count);
                        count++;
                    }
                }
            }

            _cts = null;
        }
Exemple #7
0
 protected override void OnBinaryReceived(byte[] payload)
 {
     BinaryReceived?.Invoke(this, payload);
 }