예제 #1
0
        public override void UpdateListeners()
        {
            NetIncomingMessage msg;

            while ((msg = client.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.VerboseDebugMessage:
                    Debug.Log(msg.ReadString());
                    break;

                case NetIncomingMessageType.WarningMessage:
                    Debug.LogWarning(msg.ReadString());
                    break;

                case NetIncomingMessageType.ErrorMessage:
                    Debug.LogError(msg.ReadString());
                    break;

                case NetIncomingMessageType.StatusChanged:
                    NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();
                    string statusMsg           = msg.ReadString();

                    switch (status)
                    {
                    case NetConnectionStatus.Disconnected:
                        if (Disconnected != null)
                        {
                            Disconnected(this, new DisconnectArgs(statusMsg));
                        }
                        break;

                    default:
                        Debug.Log("Status change received: " + status + " - Message: " + statusMsg);
                        break;
                    }
                    break;

                case NetIncomingMessageType.Data:

                    switch (msg.ReadByte())
                    {
                    case MessageType.MatchMessage:
                        double       timestamp = msg.ReadTime(false);
                        MatchMessage message   = Newtonsoft.Json.JsonConvert.DeserializeObject <MatchMessage>(msg.ReadString(), serializerSettings);

                        //Use reflection to call ReceiveMessage with the proper type parameter
                        MethodInfo methodToCall   = typeof(OnlineMatchMessenger).GetMethod("ReceiveMessage", BindingFlags.NonPublic | BindingFlags.Instance);
                        MethodInfo genericVersion = methodToCall.MakeGenericMethod(message.GetType());
                        genericVersion.Invoke(this, new object[] { message, timestamp });

                        break;

                    case MessageType.PlayerMovementMessage:
                        double         time     = msg.ReadTime(false);
                        PlayerMovement movement = PlayerMovement.ReadFromMessage(msg);
                        if (OnPlayerMovement != null)
                        {
                            OnPlayerMovement(this, new PlayerMovementArgs(time, movement));
                        }
                        break;
                    }
                    break;

                default:
                    Debug.Log("Received unhandled message of type " + msg.MessageType);
                    break;
                }
            }
        }
예제 #2
0
 public PlayerMovementArgs(double timestamp, PlayerMovement movement)
 {
     Timestamp = timestamp;
     Movement  = movement;
 }