Пример #1
0
        public void SendBinaryMessageTest()
        {
            try
            {
                WebSocketServer.NewDataReceived -= new SessionHandler <WebSocketSession, byte[]>(WebSocketServer_NewDataReceived);

                var webSocketClient = CreateClient(Version);

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < 10; i++)
                {
                    sb.Append(Guid.NewGuid().ToString());
                }

                string messageSource = sb.ToString();

                Random rd = new Random();

                for (int i = 0; i < 100; i++)
                {
                    int startPos = rd.Next(0, messageSource.Length - 2);
                    int endPos   = rd.Next(startPos + 1, messageSource.Length - 1);

                    string message = messageSource.Substring(startPos, endPos - startPos);
                    var    data    = Encoding.UTF8.GetBytes("ECHO " + message);

                    webSocketClient.Send(data, 0, data.Length);

                    Console.WriteLine("Client:" + message);

                    if (!MessageReceiveEvent.WaitOne(1000))
                    {
                        Assert.Fail("Cannot get response in time!");
                    }

                    Assert.AreEqual(message, CurrentMessage);
                }

                webSocketClient.Close();

                if (!CloseEvent.WaitOne(1000))
                {
                    Assert.Fail("Failed to close session ontime");
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                WebSocketServer.NewDataReceived += new SessionHandler <WebSocketSession, byte[]>(WebSocketServer_NewDataReceived);
            }
        }
Пример #2
0
        private void VerifySendMessage(string message)
        {
            var websocket = CreateClient();

            websocket.Send(message);

            if (!MessageReceiveEvent.WaitOne())
            {
                Assert.Fail("Failed to receive message one time");
            }

            Assert.AreEqual(message, CurrentMessage);

            websocket.Close();
        }
Пример #3
0
        public void SendMessageTest()
        {
            var webSocketClient = CreateClient(m_Version);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < 10; i++)
            {
                sb.Append(Guid.NewGuid().ToString());
            }

            string messageSource = sb.ToString();

            Random rd = new Random();

            for (int i = 0; i < 100; i++)
            {
                int startPos = rd.Next(0, messageSource.Length - 2);
                int endPos   = rd.Next(startPos + 1, messageSource.Length - 1);

                string message = messageSource.Substring(startPos, endPos - startPos);

                webSocketClient.Send("ECHO " + message);

                Console.WriteLine("Client:" + message);

                if (!MessageReceiveEvent.WaitOne(1000))
                {
                    Assert.Fail("Cannot get response in time!");
                }

                Assert.AreEqual(message, CurrentMessage);
            }

            webSocketClient.Close();

            if (!CloseEvent.WaitOne(1000))
            {
                Assert.Fail("Failed to close session ontime");
            }
        }