/// <summary> /// Handles an incoming message for this client. /// </summary> /// <param name="msg"></param> private void HandleMessage( NetworkingClientComponent client, NetIncomingMessage msg ) { NetOpCode opcode = (NetOpCode)msg.ReadInt16(); try { switch (opcode) { // Handle the client is ready. case NetOpCode.ClientIsReady: { Trace.WriteLine("Client is ready."); client.Ready = true; // Send welcome packet NetOutgoingMessage welcome = PacketWriter.WelcomePacket( client.NetConnection, client.Entity ); client.OutgoingMessageQueue.Enqueue(welcome); } break; // Handle input from the client. case NetOpCode.UpdatePlayerInput: { // Force input client.Entity.Engine.InputForce.X = (msg.ReadByte() - 1f) * client.Entity.Engine.EngineForce.X; client.Entity.Engine.InputForce.Y = (msg.ReadByte() - 1f) * client.Entity.Engine.EngineForce.Y; client.Entity.Engine.InputAngularForce = (msg.ReadByte() - 1f) * client.Entity.Engine.EngineAngularForce; // Weapon input client.Entity.Weapon.Trigger = msg.ReadBoolean(); // Update last update client.Entity.LastUpdated = GameMaster.ElapsedMilliseconds; } break; default: Trace.WriteLine("Received unexpected opcode."); break; } } catch { } }
public static byte[] Create_Msg_Json <T>(NetOpCode id, T msg) where T : IMessage { JObject msg_json = new JObject(); msg_json["Id"] = (ushort)id; msg_json["Data"] = JObject.FromObject(msg); var str_msg = JsonConvert.SerializeObject(msg_json); return(LZString.CompressToUint8Array(str_msg)); }
public static byte[] Create_Msg_Json(NetOpCode id, JObject data) { JObject msg_json = new JObject(); msg_json["Id"] = (ushort)id; msg_json["Data"] = data; var str_msg = JsonConvert.SerializeObject(msg_json); return(LZString.CompressToUint8Array(str_msg)); }