Пример #1
0
        private void ListenForMessages()
        {
            Console.WriteLine("SERVER: Listening for messages");
            while (isListening)
            {
                NetIncomingMessage message;
                while ((message = server.ReadMessage()) != null)
                {
                    switch (message.MessageType)
                    {
                    case NetIncomingMessageType.Data:
                        //read
                        PacketTypes type      = (PacketTypes)message.ReadByte();
                        PingPacket  payPacket = PacketHelper.Get <PingPacket>(type);
                        payPacket.Deserialize(message);
                        Console.WriteLine($"We got a packet of: {type} with PingMs {payPacket.PingMs}");

                        //send
                        NetOutgoingMessage msg      = server.CreateMessage();
                        PongPacket         outbound = PacketHelper.Get <PongPacket>(PacketTypes.Pong);
                        outbound.PingMs = DateTime.Now.Millisecond;
                        outbound.Serialize(msg);
                        server.SendMessage(msg, message.SenderConnection, NetDeliveryMethod.ReliableOrdered);
                        Console.WriteLine($"We send a packet of: {PacketTypes.Pong} with PingMs {outbound.PingMs}");

                        break;

                    case NetIncomingMessageType.StatusChanged:
                        // handle connection status messages
                        //switch (message.SenderConnection.Status) {
                        /* .. */
                        //}
                        Console.WriteLine($"SERVER: Status Changed = {message.SenderConnection.Status}");
                        break;

                    case NetIncomingMessageType.DebugMessage:
                        // handle debug messages
                        // (only received when compiled in DEBUG mode)
                        Console.WriteLine($"SERVER: {message.ReadString()}");
                        break;

                    /* .. */
                    case NetIncomingMessageType.WarningMessage:
                        Console.WriteLine($"SERVER: WARNING: {message}");
                        break;

                    default:
                        Console.WriteLine("SERVER: unhandled message with type: "
                                          + message.MessageType);
                        break;
                    }
                }
            }
            Console.WriteLine("SERVER: Done listening for messages");
        }
Пример #2
0
        public override void HandlePacket(object sender, string packet_raw)
        {
            Packet packet = Packet.Deserialize(packet_raw);

            if (packet.Type == "PingPacket")
            {
                // Handle incoming ping
                PingPacket received_ping = PingPacket.Deserialize(packet_raw);
                Thread.Sleep(1000);
                PingPacket new_pingpacket = new PingPacket(received_ping.Checksum);
                Connection.SendPacket(new_pingpacket);
            }
        }