/** * <summary> * Send a move command to the server * </summary> * * <param name="direction"></param> * <param name="speed"></param> * * <returns> * void * </returns> */ private void SendMoveCommand(string direction, int speed) { // Make connection to the server IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050); try { //string json = "[{\"command\": \"move-left\"}]"; ClientCommand command = new ClientCommand(); // Convert data into json string ClientMoveData clientData = new ClientMoveData(); string data = JsonConvert.SerializeObject(clientData); // Convert command into json string command.ID = ClientCommandID.MOVE; command.Data = data; string json = JsonConvert.SerializeObject(command); // Convert json string to bytes and send over to the server. Byte[] sendBytes = Encoding.ASCII.GetBytes(json); this.client.Send(sendBytes, sendBytes.Length, endPoint); } catch (Exception e) { Debug.Log(e.ToString()); } }
/** * <summary> * Send a move command to the server * </summary> * * <param name="direction"></param> * <param name="speed"></param> * * <returns> * void * </returns> */ private void SendMoveCommand(string direction, int speed) { // Make connection to the server IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 9050); try { //string json = "[{\"command\": \"move-left\"}]"; ClientCommand command = new ClientCommand(); // Building client data object ClientMoveData clientData = new ClientMoveData(); clientData.Position = this.transform.position; clientData.Direction = direction; clientData.Speed = speed; // Convert data into json string string data = JsonConvert.SerializeObject( clientData, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore } ); // Convert command into json string command.ID = ClientCommandID.MOVE; command.Data = data; string json = JsonConvert.SerializeObject(command); // Convert json string to bytes and send over to the server. Byte[] sendBytes = Encoding.ASCII.GetBytes(json); this.client.Send(sendBytes, sendBytes.Length, endPoint); //this.client.Send(sendBytes, sendBytes.Length); } catch (Exception e) { Debug.Log(e.ToString()); } }
/** * <summary> * Process the client commands * </summary> * * <param name="command"></param> * * <returns> * void * </returns> */ private void ProcessClientCommand(ClientCommand command) { switch (command.ID) { case ClientCommandID.MOVE: ClientMoveData data = JsonConvert.DeserializeObject <ClientMoveData>(command.Data); Vector3 position = data.Position; string direction = data.Direction; int speed = data.Speed; // Move the game object //this.Move(position, direction, speed); Dispatcher.RunOnMainThread(delegate() { this.Move(position, direction, speed); }); break; } }