Exemplo n.º 1
0
        public void Open()
        {
            var mockOpenedHandler = new Mock <OnOpened>();

            var processor = new EngineIOProtocolProcessor();

            processor.Process(new MessageContext
            {
                Message       = "0{\"sid\":\"6lV4Ef7YOyGF-5dCBvKy\",\"upgrades\":[],\"pingInterval\":10000,\"pingTimeout\":5000}",
                OpenedHandler = mockOpenedHandler.Object
            });

            Assert.AreEqual(typeof(OpenedProcessor), processor.NextProcessor.GetType());
            mockOpenedHandler.Verify(x => x(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()), Times.Once());
        }
Exemplo n.º 2
0
        private async Task ListenAsync()
        {
            while (true)
            {
                var buffer = new byte[ReceiveChunkSize];
                int count  = 0;
                WebSocketReceiveResult result = null;
                while (_ws.State == WebSocketState.Open)
                {
                    try
                    {
                        //result = await _ws.ReceiveAsync(new ArraySegment<byte>(buffer), _wsWorkTokenSource.Token);
                        var subBuffer = new byte[ReceiveChunkSize];
                        result = await _ws.ReceiveAsync(new ArraySegment <byte>(subBuffer), CancellationToken.None);

                        if (result.MessageType == WebSocketMessageType.Close)
                        {
                            Close("io server disconnect");
                            break;
                        }
                        else if (result.MessageType == WebSocketMessageType.Text || result.MessageType == WebSocketMessageType.Binary)
                        {
                            if (buffer.Length - count < result.Count)
                            {
                                Array.Resize(ref buffer, buffer.Length + result.Count);
                            }
                            Buffer.BlockCopy(subBuffer, 0, buffer, count, result.Count);
                            count += result.Count;
                        }
                        if (result.EndOfMessage)
                        {
                            break;
                        }
                    }
                    catch (WebSocketException e)
                    {
                        Close(e.Message);
                        break;
                    }
                }
                if (result == null)
                {
                    break;
                }
                if (result.MessageType == WebSocketMessageType.Text)
                {
                    string message = Encoding.UTF8.GetString(buffer, 0, count);
#if DEBUG
                    System.Diagnostics.Trace.WriteLine($"⬇ {DateTime.Now} {message}");
#endif
                    processor.Process(new MessageContext
                    {
                        Message  = message,
                        SocketIO = _io
                    });
                }
                else if (result.MessageType == WebSocketMessageType.Binary)
                {
#if DEBUG
                    System.Diagnostics.Trace.WriteLine($"⬇ {DateTime.Now} Binary message");
#endif
                    byte[] bytes;
                    if (_io.Options.EIO == 3)
                    {
                        count -= 1;
                        bytes  = new byte[count];
                        Buffer.BlockCopy(buffer, 1, bytes, 0, count);
                    }
                    else
                    {
                        bytes = new byte[count];
                        Buffer.BlockCopy(buffer, 0, bytes, 0, count);
                    }
                    _io.InvokeBytesReceived(bytes);
                }
            }
        }