Beispiel #1
0
 private void onClientDisconnected(IWebSocketConnection socket, Gateway client)
 {
     if (client == null)
     {
         _unregisteredConnections.Remove(socket);
     }
     else
     {
         DisconnectClient(client);
     }
 }
Beispiel #2
0
        private void onClientMessage(IWebSocketConnection socket, byte[] message, Gateway gateway)
        {
            Utils.Log("Incoming binary message:{0}", BitConverter.ToString(message).Replace("-", ""));
            try
            {
                Packet packet = Packet.FromBytes(message);
                Utils.Log("{0}", packet);

                if (gateway == null)
                {
                    RegisterGateway(packet, socket);
                }
                else
                {
                    gateway.HandleMessage(packet);
                }
            }
            catch (BadPacketException)
            {
                Utils.Log("Failed to parse incoming packet");
            }
        }
Beispiel #3
0
 private void onClientMessage(IWebSocketConnection socket, string message, Gateway gateway)
 {
     Utils.Log("Incoming text message:{0}", message);
     try
     {
         byte[] data = Convert.FromBase64String(message);
         onClientMessage(socket, data, gateway);
     }
     catch (FormatException)
     {
         Utils.Log("Base64 string decoding failed:{0}", message);
     }
 }
Beispiel #4
0
 private void DisconnectClient(Gateway c)
 {
     c.Close();
     _allClients.Remove(c.Id);
 }
Beispiel #5
0
        private void RegisterGateway(Packet packet, IWebSocketConnection socket)
        {
            if (packet.Type != PacketType.Register)
            {
                Utils.Log("Unexpected packet:{0}", packet);
                return;
            }

            Gateway c = null;
            RegisterPacket rp = packet as RegisterPacket;

            if (!_allClients.TryGetValue(rp.GatewayId, out c))
            {
                c = new Gateway(rp.GatewayId, rp.GatewayAddr, rp.NetworkKey, socket);
                _allClients.Add(rp.GatewayId, c);
            }
            else
            {
                c.SetConnection(socket);
            }

            socket.OnMessage = (message) =>
            {
                onClientMessage(socket, message, c);
            };

            socket.OnBinary = (message) =>
            {
                onClientMessage(socket, message, c);
            };

            socket.OnClose = () =>
            {
                onClientDisconnected(socket, c);
            };

            c.HandleMessage(packet);
        }