Example #1
0
        public void WebSocketModule_FailedEvent()
        {
            var waitHandle = new AutoResetEvent(false);
            var json       = default(JObject);
            var context    = CreateReactContext(new MockInvocationHandler((name, args) =>
            {
                if (name == "emit" && args.Length == 2)
                {
                    var eventName = (string)args[0];
                    switch (eventName)
                    {
                    case "websocketFailed":
                        json = (JObject)args[1];
                        waitHandle.Set();
                        break;
                    }
                }
            }));

            var module = new WebSocketModule(context);

            try
            {
                module.connect("ws://invalid.websocket.address", null, null, 1);
                Assert.IsTrue(waitHandle.WaitOne(WaitTimeoutInMs));
            }
            finally
            {
                module.close(1000, "None", 1);
            }

            Assert.AreEqual(1, json["id"].Value <int>());
        }
Example #2
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>());
        }
Example #3
0
        public void WebSocketModule_OpenClosedEvent()
        {
            var waitHandle  = new AutoResetEvent(false);
            var openParams  = default(JObject);
            var closeParams = default(JObject);
            var context     = CreateReactContext(new MockInvocationHandler((name, args) =>
            {
                if (name == "emit" && args.Length == 2)
                {
                    var eventName = (string)args[0];
                    switch (eventName)
                    {
                    case "websocketClosed":
                        closeParams = (JObject)args[1];
                        waitHandle.Set();
                        break;

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

            var module = new WebSocketModule(context);

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

            Assert.AreEqual(1, openParams["id"].Value <int>());
            Assert.AreEqual(1, closeParams["id"].Value <int>());
            Assert.AreEqual(1000, closeParams["code"].Value <int>());
            Assert.AreEqual("None", closeParams["reason"].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"]);
        }