Beispiel #1
0
        public void ReceiveData_Fragmented()
        {
            var framer           = new WebSocketFramer(1024);
            var receivedMessages = new List <byte[]>();

            framer.MessageReceived += buffer => { receivedMessages.Add(buffer); };

            // Check receiving a different amount of bytes each time
            foreach (var steps in new int[] { 1, 2, 3, 4, 5 })
            {
                // Create messages
                var message1 = new byte[] { 1, 2, 3, 4, 5 };
                var message2 = new byte[] { 5, 6, 7 };
                var message3 = Encoding.UTF8.GetBytes("foobar");
                var message4 = new byte[512];

                var framed1 = framer.Frame(message1, false);
                var framed2 = framer.Frame(message2, true);
                var framed3 = framer.Frame(message3, false);
                var framed4 = framer.Frame(message4, true);

                // Put messages into one array
                var dataList = new List <byte>();
                dataList.AddRange(framed1);
                dataList.AddRange(framed2);
                dataList.AddRange(framed3);
                dataList.AddRange(framed4);

                // Receive data step by step
                for (var i = 0; i < dataList.Count; i += steps)
                {
                    var take = Math.Min(dataList.Count - i, steps);
                    if (take < 0)
                    {
                        continue;
                    }

                    var data = dataList.Skip(i).Take(take).ToArray();
                    framer.ReceiveData(data, data.Length);
                }

                // Check
                Assert.Equal(4, receivedMessages.Count);
                Assert.Equal(framed1, receivedMessages[0]);
                Assert.Equal(framed2, receivedMessages[1]);
                Assert.Equal(framed3, receivedMessages[2]);
                Assert.Equal(framed4, receivedMessages[3]);
                receivedMessages.Clear();
            }
        }
Beispiel #2
0
        public void ReceiveDataMaskedLong_Single()
        {
            var framer           = new WebSocketFramer(102400);
            var receivedMessages = new List <byte[]>();

            framer.MessageReceived += buffer => { receivedMessages.Add(buffer); };

            var data = framer.Frame(new byte[ushort.MaxValue + 10], true);

            framer.ReceiveData(data, data.Length);

            Assert.Equal(1, receivedMessages.Count);
            Assert.Equal(data, receivedMessages[0]);
        }
Beispiel #3
0
        public void ReceiveDataUnmasked_Single()
        {
            var framer           = new WebSocketFramer(1024);
            var receivedMessages = new List <byte[]>();

            framer.MessageReceived += buffer => { receivedMessages.Add(buffer); };

            var data = framer.Frame(new byte[] { 1, 2, 3 }, false);

            framer.ReceiveData(data, data.Length);

            Assert.Equal(1, receivedMessages.Count);
            Assert.Equal(data, receivedMessages[0]);
        }
Beispiel #4
0
        public void ReceiveDataMaskedShort_Single()
        {
            Console.WriteLine("--- ReceiveDataSingleMaskedShort");
            var framer           = new WebSocketFramer(102400);
            var receivedMessages = new List <byte[]>();

            framer.MessageReceived += buffer => { receivedMessages.Add(buffer); };

            var data = framer.Frame(new byte[512], true);

            framer.ReceiveData(data, data.Length);

            Assert.Equal(1, receivedMessages.Count);
            Assert.Equal(data, receivedMessages[0]);
            Console.WriteLine("---");
        }