Ejemplo n.º 1
0
        public void WebSocketModule_DataEvent_Text_ThenBinary()
        {
            var waitHandle = new AutoResetEvent(false);
            var json       = default(JObject);
            var context    = CreateReactContext(new MockInvocationHandler((name, args) =>
            {
                var eventName = (string)args[0];
                switch (eventName)
                {
                case "websocketOpen":
                case "websocketClosed":
                    waitHandle.Set();
                    break;

                case "websocketMessage":
                    json = (JObject)args[1];
                    waitHandle.Set();
                    break;
                }
            }));

            var module         = new WebSocketModule(context);
            var encodedMessage = Convert.ToBase64String(Encoding.UTF8.GetBytes("Hello World"));

            try
            {
                module.connect("ws://echo.websocket.org", null, null, 1);
                Assert.IsTrue(waitHandle.WaitOne(WaitTimeoutInMs));
                module.send("FooBarBaz", 1);
                Assert.IsTrue(waitHandle.WaitOne(WaitTimeoutInMs));
                module.sendBinary(encodedMessage, 1);
                Assert.IsTrue(waitHandle.WaitOne(WaitTimeoutInMs));
            }
            finally
            {
                module.close(1000, "None", 1);
                Assert.IsTrue(waitHandle.WaitOne(WaitTimeoutInMs));

                waitHandle.Dispose();
            }

            Assert.AreEqual(1, json["id"].Value <int>());
            Assert.AreEqual("binary", json["type"].Value <string>());
            Assert.AreEqual(encodedMessage, json["data"].Value <string>());
        }
        public void WebSocketModule_DataEvent()
        {
            var waitHandle = new AutoResetEvent(false);
            var json       = default(JObject);
            var context    = CreateReactContext(new MockInvocationHandler((name, args) =>
            {
                var eventName = (string)args[0];
                switch (eventName)
                {
                case "websocketOpen":
                case "websocketClosed":
                    waitHandle.Set();
                    break;

                case "websocketMessage":
                    json = (JObject)args[1];
                    waitHandle.Set();
                    break;

                default:
                    break;
                }
            }));

            var module = new WebSocketModule(context);

            try
            {
                module.connect("ws://echo.websocket.org", null, null, 1);
                Assert.IsTrue(waitHandle.WaitOne());
                module.send("FooBarBaz", 1);
                Assert.IsTrue(waitHandle.WaitOne());
            }
            finally
            {
                module.close(1000, "None", 1);
                Assert.IsTrue(waitHandle.WaitOne());
            }

            Assert.AreEqual(1, json["id"]);
            Assert.AreEqual("FooBarBaz", json["data"]);
        }