Esempio n. 1
0
        public void ConnectToServer()
        {
            connection.EventReceived += LogChatEventReceived;

            // connect for the first time:
            connection.HandshakeSync();
            Assert.AreEqual(BayeuxConnectionState.Connected, connection.State, "Invalid state of the connection!");
            Assert.IsNotNull(connection.ClientID, "Not received ClientID!");

            var clientID = connection.ClientID;

            connection.ConnectSync();
            Assert.AreEqual(BayeuxConnectionState.Connected, connection.State, "Invalid state of the connection!");
            Assert.IsNotNull(connection.ClientID);

            connection.DisconnectSync();
            Assert.AreEqual(BayeuxConnectionState.Disconnected, connection.State, "Invalid state after disconnection!");
            Assert.IsNull(connection.ClientID);

            // reconnect:
            connection.HandshakeSync();
            Assert.AreEqual(BayeuxConnectionState.Connected, connection.State, "Invalid state of the connection!");
            Assert.IsNotNull(connection.ClientID, "Not received ClientID!");

            connection.ConnectSync();
            Assert.AreEqual(BayeuxConnectionState.Connected, connection.State, "Invalid state of the connection!");
            Assert.IsNotNull(connection.ClientID);

            Assert.AreNotEqual(clientID, connection.ClientID, "ClientID before reconnection should be different than the current one!");

            // subscribe to chat:
            connection.SubscribeSync("/chat/demo");
            Assert.IsTrue(connection.Subscribed("/chat/demo"), "Connection should be subscribed");

            connection.SubscribeSync("/chat/members");
            Assert.IsTrue(connection.Subscribed("/chat/members"), "Connection should be subscribed");

            // join the chat room:
            connection.PublishSync("/service/members", new BayeuxEventData("user", "Test-User", "room", "/chat/demo"));
            connection.PublishSync("/chat/demo", new BayeuxEventData("user", "Test-User", "membership", "join", "chat", "Test-User has joinded!"));

            // wait for some time:
            Thread.Sleep(5000);

            // ussubscribe from chat:
            connection.PublishSync("/chat/demo", new BayeuxEventData("user", "Test-User", "membership", "leave", "chat", "Test-User is leaving!"));
            //connection.UnsubscribeSync("/chat/demo");
            connection.DisconnectSync();
        }
Esempio n. 2
0
        /// <summary>
        /// Handles all the messages received from server
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LogDataReceived(object sender, BayeuxConnectionEventArgs e)
        {
            if (!_connection.IsLongPolling)
            {
                return;
            }

            if (_connection.Subscribed(_channel))
            {
                _isConnected = true;
            }

            var reader = new JSonReader();

            if (e.Message != null)
            {
                var reply = reader.ReadAsJSonObject(e.Message.ToString());

                if (reply.ArrayItems != null)
                {
                    foreach (var aItem in reply.ArrayItems)
                    {
                        if (aItem.ObjectItems != null)
                        {
                            foreach (var objItem in aItem.ObjectItems)
                            {
                                if (objItem.Key != "advice")
                                {
                                    continue;
                                }

                                if (objItem.Value != null)
                                {
                                    var msg = objItem.Value.ToString();

                                    if (msg.Equals("{\r\n    \"reconnect\": \"handshake\"\r\n}"))
                                    {
                                        Disconnect();
                                        Connect("");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }