public void NotSendPacketsIfSocketCloses()
        {
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");

            var noPacket = true;

            socket = new Socket(CreateOptions());
            socket.On(Socket.EVENT_OPEN, () =>
            {
                noPacket = true;

            });

            socket.Open();
            socket.On(Socket.EVENT_PACKET_CREATE, () =>
            {
                noPacket = false;
                log.Info("NotSendPacketsIfSocketCloses EVENT_PACKET_CREATE noPacket = " + noPacket);
            });
            socket.Close();
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
            //await Task.Delay(1000);
            log.Info("NotSendPacketsIfSocketCloses end noPacket = " + noPacket);
            Assert.True(noPacket);
        }
        public void ConnectToLocalhost2()
        {
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);
            this.Message = "";

            var options = CreateOptions();
            options.Transports = new List<string>() { "polling" };
            socket = new Socket(options);

            //socket = new Socket(CreateOptions());
            socket.On(Socket.EVENT_OPEN, () =>
            {
                log.Info("open");
                //socket.Send("test send");

            });
            socket.On(Socket.EVENT_MESSAGE, (d) =>
            {
                var data = (string) d;

                log.Info("message2 = " + data);
                this.Message = data;
                _manualResetEvent.Set();
            });

            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();
            Assert.Equal("hi", this.Message);
        }
        public void Handshake()
        {
            LogManager.SetupLogManager();
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");

            HandshakeData handshake_data = null;

            var socket = new Socket(CreateOptionsSecure());

            socket.On(Socket.EVENT_HANDSHAKE, (data) =>
            {
                log.Info(Socket.EVENT_HANDSHAKE + string.Format(" data = {0}", data));
                handshake_data = data as HandshakeData;
            });

            socket.Open();
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(4));
            socket.Close();

            Assert.IsNotNull(handshake_data);
            Assert.IsNotNull(handshake_data.Upgrades);
            Assert.IsTrue(handshake_data.Upgrades.Count > 0);
            Assert.IsTrue(handshake_data.PingInterval > 0);
            Assert.IsTrue(handshake_data.PingTimeout > 0);
        }
        public void Handshake()
        {
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);

            HandshakeData handshake_data = null;

            var socket = new Socket(CreateOptionsSecure());

            socket.On(Socket.EVENT_HANDSHAKE, (data) =>
            {
                log.Info(Socket.EVENT_HANDSHAKE + string.Format(" data = {0}", data));
                handshake_data = data as HandshakeData;
                _manualResetEvent.Set();
            });

            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();

            Assert.NotNull(handshake_data);
            Assert.NotNull(handshake_data.Upgrades);
            Assert.True(handshake_data.Upgrades.Count > 0);
            Assert.True(handshake_data.PingInterval > 0);
            Assert.True(handshake_data.PingTimeout > 0);
        }
        public void Messages()
        {
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);

            var events = new ConcurrentQueue<string>();

            var socket = new Socket(CreateOptionsSecure());
            socket.On(Socket.EVENT_OPEN, () =>
            {
                log.Info("EVENT_OPEN");
                socket.Send("hello");
            });
            socket.On(Socket.EVENT_MESSAGE, (d) =>
            {
                var data = (string)d;
                log.Info("EVENT_MESSAGE data = " + data);
                events.Enqueue(data);
                if (events.Count > 1)
                {
                    _manualResetEvent.Set();
                }
            });
            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();

            string result;
            events.TryDequeue(out result);
            Assert.Equal("hi", result);
            events.TryDequeue(out result);
            Assert.Equal("hello", result);
        }
        public void ReceiveBinaryData()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);

            var events = new Queue<object>();

            var binaryData = new byte[5];
            for (int i = 0; i < binaryData.Length; i++)
            {
                binaryData[i] = (byte) (i + 0);
            }

            var options = CreateOptions();
            var socket = new Socket(options);

            socket.On(Socket.EVENT_OPEN, () =>
            {
                log.Info(Socket.EVENT_OPEN);
            });

            socket.On(Socket.EVENT_UPGRADE, () =>
            {
                log.Info(Socket.EVENT_UPGRADE);
                socket.Send(binaryData);
                //socket.Send("why");
            });

            socket.On(Socket.EVENT_MESSAGE, (d) =>
            {
                var data = d as string;
                log.Info(string.Format("EVENT_MESSAGE data ={0} d = {1} ", data, d));

                if (data == "hi")
                {
                    return;
                }
                events.Enqueue(d);
                _manualResetEvent.Set();
            });

            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();

            var binaryData2 = new byte[5];
            for (int i = 0; i < binaryData.Length; i++)
            {
                binaryData2[i] = (byte) (i + 1);
            }

            object result = events.Dequeue();
            if (result is string)
            {
                log.Info(result as string);
            }
            CollectionAssert.AreEqual(binaryData, (byte[]) result);
        }
        public void ReceiveBinaryData()
        {
            LogManager.Enabled = true;
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);

            var events = new ConcurrentQueue<object>();

            var binaryData = new byte[5];
            for (int i = 0; i < binaryData.Length; i++)
            {
                binaryData[i] = (byte) i;
            }

            var options = CreateOptions();
            options.Transports = ImmutableList.Create<string>(Polling.NAME);

            var socket = new Socket(options);

            socket.On(Socket.EVENT_OPEN, () =>
            {

                log.Info("EVENT_OPEN");

                socket.Send(binaryData);
                //socket.Send("cash money €€€");

            });

            socket.On(Socket.EVENT_MESSAGE, (d) =>
            {

                var data = d as string;
                log.Info(string.Format("EVENT_MESSAGE data ={0} d = {1} ", data, d));

                if (data == "hi")
                {
                    return;
                }
                events.Enqueue(d);
                _manualResetEvent.Set();
            });

            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();
            log.Info("ReceiveBinaryData end");

            var binaryData2 = new byte[5];
            for (int i = 0; i < binaryData2.Length; i++)
            {
                binaryData2[i] = (byte) (i + 1);
            }

            object result;
            events.TryDequeue(out result);
            Assert.Equal(binaryData, result);
        }
        public void ReceiveBinaryData()
        {
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);

            var events = new Queue<object>();

            var binaryData = new byte[5];
            for (int i = 0; i < binaryData.Length; i++)
            {
                binaryData[i] = (byte) (i + 0);
            }

            var options = CreateOptions();

            var socket = new Socket(options);

            socket.On(Socket.EVENT_OPEN, () =>
            {
                log.Info(Socket.EVENT_OPEN);
            });

            socket.On(Socket.EVENT_UPGRADE, () =>
            {
                log.Info(Socket.EVENT_UPGRADE);
                socket.Send(binaryData);
            });

            socket.On(Socket.EVENT_MESSAGE, (d) =>
            {

                var data = d as string;
                log.Info(string.Format("EVENT_MESSAGE data ={0} d = {1} ", data, d));

                if (data == "hi")
                {
                    return;
                }
                events.Enqueue(d);
                _manualResetEvent.Set();
            });

            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();
            log.Info("ReceiveBinaryData end");

            var binaryData2 = new byte[5];
            for (int i = 0; i < binaryData2.Length; i++)
            {
                binaryData2[i] = (byte) (i + 1);
            }

            object result;
            result = events.Dequeue();
            Assert.Equal(binaryData, result);
        }
        public void Constructors()
        {
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");

            var socket = new Socket(CreateOptions());

            socket.Open();
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3));
            Assert.NotNull(socket.Transport);

            socket.Close();
        }
        public void ConnectToLocalhost()
        {
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);

            socket = new Socket(CreateOptions());
            socket.On(Socket.EVENT_OPEN, new TestListener());
            socket.On(Socket.EVENT_MESSAGE, new MessageListener(socket, this));
            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();
            Assert.Equal("hi", this.Message);
        }
예제 #11
0
 protected Transport(Options options)
 {
     this.Path = options.Path;
     this.Hostname = options.Hostname;
     this.Port = options.Port;
     this.Secure = options.Secure;
     this.Query = options.Query;
     this.TimestampParam = options.TimestampParam;
     this.TimestampRequests = options.TimestampRequests;
     this.Socket = options.Socket;
     this.Agent = options.Agent;
     this.ForceBase64 = options.ForceBase64;
     this.ForceJsonp = options.ForceJsonp;
     this.Cookie = options.GetCookiesAsString();
 }
예제 #12
0
        public void FilterUpgrades()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");

            var options = CreateOptions();
            options.Transports = ImmutableList<string>.Empty.Add("polling");

            socket = new Socket(options);

            var immutablelist = socket.FilterUpgrades(ImmutableList<string>.Empty.Add("polling").Add("websocket"));

            Assert.AreEqual("polling", immutablelist[0]);
            Assert.AreEqual(1, immutablelist.Count);
        }
예제 #13
0
        public void FilterUpgrades()
        {
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");

            var options = CreateOptions();
            options.Transports = new List<string>(){"polling"};

            socket = new Socket(options);

            var List = socket.FilterUpgrades( new List<string>(){"polling","websocket"});

            Assert.Equal("polling", List[0]);
            Assert.Equal(1, List.Count);
        }
        public void ConnectToLocalhost()
        {

            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");
            log.Info(CoreApplication.Id);
            _manualResetEvent = new ManualResetEvent(false);

            socket = new Socket(CreateOptions());
            socket.On(Socket.EVENT_OPEN, new TestListener());
            socket.On(Socket.EVENT_MESSAGE, new MessageListener(socket, this));
            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();
            Assert.AreEqual("hi", this.Message);
        }
예제 #15
0
        public void FilterUpgrades()
        {

            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");

            var options = CreateOptions();
            options.Transports = ImmutableList<string>.Empty.Add("polling");

            socket = new Socket(options);

            var immutablelist = socket.FilterUpgrades(ImmutableList<string>.Empty.Add("polling").Add("websocket"));

            Assert.Equal("polling", immutablelist[0]);
            Assert.Equal(1, immutablelist.Count);
        }
        public void Handshake2()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");

            var socket = new Socket(CreateOptionsSecure());
            var testListener = new TestHandshakeListener();
            socket.On(Socket.EVENT_HANDSHAKE, testListener);
            socket.Open();
            Task.Delay(TimeSpan.FromSeconds(3)).Wait();
            socket.Close();

            Assert.IsNotNull(testListener.HandshakeData);
            Assert.IsNotNull(testListener.HandshakeData.Upgrades);
            Assert.IsTrue(testListener.HandshakeData.Upgrades.Count > 0);
            Assert.IsTrue(testListener.HandshakeData.PingInterval > 0);
            Assert.IsTrue(testListener.HandshakeData.PingTimeout > 0);
        }
예제 #17
0
        public void Usage2()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");

            var options = CreateOptions();
            var socket = new Socket(options);

            //Receiving data
            //var socket = new Socket("ws://localhost:3000");
            socket.On(Socket.EVENT_OPEN, () =>
            {
                socket.On(Socket.EVENT_MESSAGE, (data) => Debug.WriteLine((string)data));
            });
            socket.Open();

            //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2));
            socket.Close();
        }
        public void Handshake2()
        {
            LogManager.SetupLogManager();
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");

            var socket = new Socket(CreateOptionsSecure());
            var testListener = new TestHandshakeListener();
            socket.On(Socket.EVENT_HANDSHAKE, testListener);
            socket.Open();
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(4));
            socket.Close();

            Assert.IsNotNull(testListener.HandshakeData);
            Assert.IsNotNull(testListener.HandshakeData.Upgrades);
            Assert.IsTrue(testListener.HandshakeData.Upgrades.Count > 0);
            Assert.IsTrue(testListener.HandshakeData.PingInterval > 0);
            Assert.IsTrue(testListener.HandshakeData.PingTimeout > 0);
        }
예제 #19
0
        public void Usage1()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");

            var options = CreateOptions();
            var socket = new Socket(options);

            //You can use `Socket` to connect:
            //var socket = new Socket("ws://localhost");
            socket.On(Socket.EVENT_OPEN, () =>
            {
                socket.Send("hi");
                socket.Close();
            });
            socket.Open();

            //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2));
        }
        public void Handshake2()
        {
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);

            var socket = new Socket(CreateOptionsSecure());
            var testListener = new TestHandshakeListener(this);
            socket.On(Socket.EVENT_HANDSHAKE, testListener);
            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();

            Assert.NotNull(testListener.HandshakeData);
            Assert.NotNull(testListener.HandshakeData.Upgrades);
            Assert.True(testListener.HandshakeData.Upgrades.Count > 0);
            Assert.True(testListener.HandshakeData.PingInterval > 0);
            Assert.True(testListener.HandshakeData.PingTimeout > 0);
        }
예제 #21
0
        public void Usage1()
        {
            LogManager.SetupLogManager();
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");

            var options = CreateOptions();
            var socket = new Socket(options);

            //You can use `Socket` to connect:
            //var socket = new Socket("ws://localhost");
            socket.On(Socket.EVENT_OPEN, () =>
            {
                socket.Send("hi");
                socket.Close();
            });
            socket.Open();

            //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2));
        }
예제 #22
0
        public void Usage2()
        {
            LogManager.SetupLogManager();
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);

            var options = CreateOptions();
            var socket = new Socket(options);

            //Receiving data
            //var socket = new Socket("ws://localhost:3000");
            socket.On(Socket.EVENT_OPEN, () =>
            {
                socket.On(Socket.EVENT_MESSAGE, (data) => Console.WriteLine((string)data));
                _manualResetEvent.Set();
            });
            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();
        }
예제 #23
0
        public void SocketClosing()
        {
            LogManager.SetupLogManager();
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");
            var ManualResetEvent = new ManualResetEvent(false);

            var closed = false;
            //var error = false;

            var options = CreateOptions();

            socket = new Socket("ws://0.0.0.0:8080", options);
            socket.On(Socket.EVENT_OPEN, () =>
            {
                log.Info("EVENT_OPEN");
                //socket.Send("test send");

            });
            socket.On(Socket.EVENT_CLOSE, () =>
            {
                log.Info("EVENT_CLOSE = ");
                closed = true;
                ManualResetEvent.Set();
            });

            socket.Once(Socket.EVENT_ERROR, () =>
            {
                log.Info("EVENT_ERROR = ");
                //error = true;
            });

            socket.Open();
            ManualResetEvent.WaitOne();
            log.Info("After WaitOne");
            Assert.IsTrue(closed);
            //Assert.IsTrue(error);
        }
예제 #24
0
        public async Task SocketClosing()
        {

            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");

            var closed = false;
            var error = false;

            var options = CreateOptions();

            socket = new Socket("ws://0.0.0.0:8080", options);
            socket.On(Socket.EVENT_OPEN, () =>
            {
                log.Info("EVENT_OPEN");
                //socket.Send("test send");

            });
            socket.On(Socket.EVENT_CLOSE, () =>
            {
                log.Info("EVENT_CLOSE = ");
                closed = true;

            });

            socket.Once(Socket.EVENT_ERROR, () =>
            {
                log.Info("EVENT_ERROR = ");
                error = true;

            });

            socket.Open();
            //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
            await Task.Delay(1000);
            Assert.True(closed);
            Assert.True(error);
        }
예제 #25
0
        public void SocketClosing()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);
            var manualResetEventError = new ManualResetEvent(false);
            var closed = false;
            var error = false;

            var options = CreateOptions();

            socket = new Socket("ws://0.0.0.0:8080", options);
            socket.On(Socket.EVENT_OPEN, () =>
            {
                log.Info("EVENT_OPEN");
                //socket.Send("test send");
            });
            socket.On(Socket.EVENT_CLOSE, () =>
            {
                log.Info("EVENT_CLOSE = ");
                closed = true;
                _manualResetEvent.Set();
            });

            socket.Once(Socket.EVENT_ERROR, () =>
            {
                log.Info("EVENT_ERROR = ");
                error = true;
                manualResetEventError.Set();
            });

            socket.Open();
            _manualResetEvent.WaitOne();
            manualResetEventError.WaitOne();
            Assert.IsTrue(closed);
            Assert.IsTrue(error);
        }
예제 #26
0
        public void WebsocketClosesAfterWritingPingPacket()
        {
            var log = LogManager.GetLogger(Global.CallerName());
            log.Info("Start");
            bool open = false;
            bool close = false;

            var options = CreateOptions();
            //options.ForceBase64 = true;
            //options.Secure = false;
            //options.RememberUpgrade = false;
            //options.TimestampRequests = true;
            var socket = new Socket(options);

            //You can use `Socket` to connect:
            //var socket = new Socket("ws://localhost");

            socket.On(Socket.EVENT_OPEN, () =>
            {
                log.Info("EVENT_OPEN");
                open = true;
            });

            socket.On(Socket.EVENT_CLOSE, () =>
            {
                log.Info("EVENT_CLOSE");
                close = true;
            });
            socket.Open();

            Task.Delay(60000).Wait();
            Assert.IsTrue(open);
            Assert.IsFalse(close);

            socket.Close();
        }
        public void Cookie()
        {
            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);

            var events = new Queue<string>();

            var options = CreateOptions();
            options.Cookies.Add("foo", "bar");
            var socket = new Socket(options);
            socket.On(Socket.EVENT_OPEN, () =>
            {
                log.Info("EVENT_OPEN");
                socket.Send("cookie");
            });
            socket.On(Socket.EVENT_MESSAGE, (d) =>
            {
                var data = (string)d;
                log.Info("EVENT_MESSAGE data = " + data);
                events.Enqueue(data);
                if (events.Count > 1)
                {
                    _manualResetEvent.Set();
                }
            });
            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();

            string result;
            result = events.Dequeue();
            Assert.Equal("hi", result);
            result = events.Dequeue();
            Assert.Equal("got cookie", result);
        }
 public MessageListener(Socket socket, ConnectionTest connectionTest)
 {
     this.socket = socket;
     this.connectionTest = connectionTest;
 }
 public MessageListener(Socket socket)
 {
     this.socket = socket;
 }
        public void Testemoji()
        {

            var log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod());
            log.Info("Start");
            _manualResetEvent = new ManualResetEvent(false);
            const string SendMessage = "\uD800-\uDB7F\uDB80-\uDBFF\uDC00-\uDFFF\uE000-\uF8FF";

            var options = CreateOptions();
            socket = new Socket(options);

            socket.On(Socket.EVENT_OPEN, () =>
            {
                log.Info("open");

              
                socket.Send(SendMessage);
            });

            socket.On(Socket.EVENT_MESSAGE, (d) =>
            {
                var data = (string)d;

                log.Info(Socket.EVENT_MESSAGE);

                if (data == "hi")
                {
                    return;
                }

                this.Message = data;
                _manualResetEvent.Set();
            });

            socket.Open();
            _manualResetEvent.WaitOne();
            socket.Close();	
  
            Assert.True(SendMessage == this.Message);

        }