public void InitTcpGameClient() { tcpClient = new TcpClient(new IPEndPoint(ipAdress, clientPort)); tcpClient.Connect(new IPEndPoint(ipAdress, serverPort)); // todo :: handle failures to connect IPEndPoint serverEndPoint = tcpClient.Client.RemoteEndPoint as IPEndPoint; clientPort = ((IPEndPoint)tcpClient.Client.LocalEndPoint).Port; Console.WriteLine($"Connected to server {serverEndPoint.Address}:{serverEndPoint.Port}"); Console.WriteLine(); var stream = tcpClient.GetStream(); ThreadPool.QueueUserWorkItem(ReadIncomingStream, stream, true); try { var gameMessage = new NetworkGameMessage() { serviceName = "chat", operationName = "join", datamembers = new List <string> { JsonSerializer.Serialize(userName) } }; var bytes = gameMessage.AsJsonBytes; stream.Write(bytes); } catch (IOException e) { Console.WriteLine(e.Message + e.InnerException?.Message); } }
public void SendPixelUpdate(Pixels pixels) { if (tcpClient == null) { return; } if (tcpClient.Connected == false) { tcpClient.Dispose(); Debug.LogWarning(" ------- not connected to server!"); InitGameClient(ipAdress, serverPort, userName); } try { var gameMessage = new NetworkGameMessage() { serviceName = "game", operationName = "newPixels", datamembers = new List <string> { pixels.AsJsonString } }; var bytes = gameMessage.AsJsonBytes; var stream = tcpClient.GetStream(); stream.Write(bytes, 0, bytes.Length); } catch (IOException e) { Debug.Log(e.Message + e.InnerException?.Message); } }
public void InitGameClient(string ipAdress, int port, string userName = null) { Task.Run(() => { this.ipAdress = ipAdress; if (userName != null) { this.userName = userName; } serverPort = port; networkMessageQueue = new ConcurrentQueue <NetworkGameMessage>(); Debug.Log($"Try connect to server {ipAdress}:{serverPort}"); //tcpClient = new TcpClient(new IPEndPoint(ipAdress, clientPort)); tcpClient = new TcpClient(ipAdress, serverPort); //tcpClient.Connect(ipAdress, serverPort); // todo :: handle failures to connect IPEndPoint serverEndPoint = tcpClient.Client.RemoteEndPoint as IPEndPoint; clientPort = ((IPEndPoint)tcpClient.Client.LocalEndPoint).Port; Debug.Log($"Connected to server {serverEndPoint.Address}:{serverEndPoint.Port}"); var stream = tcpClient.GetStream(); try { var gameMessage = new NetworkGameMessage() { serviceName = "chat", operationName = "join", datamembers = new List <string> { JsonConvert.SerializeObject(userName) } }; var bytes = gameMessage.AsJsonBytes; stream.Write(bytes, 0, bytes.Length); } catch (IOException e) { Debug.Log(e.Message + e.InnerException?.Message); } ThreadPool.QueueUserWorkItem(new WaitCallback(ReadIncomingStream <NetworkStream>), stream); ServerServiceHelper.instance.initialized = true; }); }
//internal void RecieveNetworkGameMessage(byte[] utf8bytes) // note :: we don't need to thread this because everything was handled in webgl. This is detached from socket/server code. internal void RecieveNetworkGameMessage(string jsonString) // note :: we don't need to thread this because everything was handled in webgl. This is detached from socket/server code. { SendMessageToBrowser("recieve message"); try { //byte[] jsonBuffer = new byte[readBufferSize]; NetworkGameMessage networkMessage = JsonConvert.DeserializeObject <NetworkGameMessage>(jsonString); networkMessageQueue.Enqueue(networkMessage); } catch (JsonException e) { Console.WriteLine(e.Message + e.InnerException?.Message); } }
private void GameService(NetworkGameMessage serverMessage) { if (serverMessage.operationName == "pixelUpdate") { try { Pixels updatedPixels = JsonConvert.DeserializeObject <Pixels>(serverMessage.datamembers[0]); pixelUpdateCallback(updatedPixels); } catch (JsonException e) { ErrorDebugMessage(e, "Error happened while processing json - service game - operation pixelUpdate:\n"); } catch (Exception e) { ErrorDebugMessage(e, "Unknown Error happened while calling service game - operation pixelUpdate:\n"); } } }
public void SendChatMessage(string message) { if (tcpClient == null) { return; } if (tcpClient.Connected == false) { tcpClient.Dispose(); Debug.LogWarning(" ------- not connected to server!"); InitGameClient(ipAdress, serverPort, userName); } try { var chatMessage = new ChatMessage() { timestamp = DateTime.Now, user = userName, message = message }; var gameMessage = new NetworkGameMessage() { serviceName = "chat", operationName = "message", datamembers = new List <string> { chatMessage.AsJsonString } }; var bytes = gameMessage.AsJsonBytes; var stream = tcpClient.GetStream(); stream.Write(bytes, 0, bytes.Length); } catch (IOException e) { Debug.Log(e.Message + e.InnerException?.Message); } }
internal void ServerConnected() { SendMessageToBrowser("server connected"); // todo :: send join message try { NetworkGameMessage networkMessage = new NetworkGameMessage() { serviceName = "chat", operationName = "join", datamembers = new List <string> { JsonConvert.SerializeObject(userName) } }; SendNetworkMessageToServer(networkMessage.AsJsonString); } catch (JsonException e) { Console.WriteLine(e.Message + e.InnerException?.Message); } }
// todo :: fancy interface something to autohandle/semi-autohandle operations and their parameters. private void ChatService(NetworkGameMessage serverMessage) { if (serverMessage.operationName == "message") { try { ChatMessage message = JsonConvert.DeserializeObject <ChatMessage>(serverMessage.datamembers[0]); SendMessageToBrowser("message from: " + message.user + " - " + message.message); messageCallback(message); } catch (JsonException e) { ErrorDebugMessage(e, "Error happened while processing json - service chat - operation join:\n"); } catch (Exception e) { ErrorDebugMessage(e, "Unknown Error happened while calling service chat - operation message:\n"); } } else if (serverMessage.operationName == "join") { try { ChatMessage message = JsonConvert.DeserializeObject <ChatMessage>(serverMessage.datamembers[0]); userjoinCallback(message); } catch (JsonException e) { ErrorDebugMessage(e, "Error happened while processing json - service chat - operation join:\n"); } catch (Exception e) { ErrorDebugMessage(e, "Unknown Error happened while calling service chat - operation message:\n"); } } else { // todo : unknown operation, just skip could be spam } }
public bool SendMessage(string message) { if (tcpClient.Connected == false) { tcpClient.Dispose(); Console.WriteLine(" ------- not connected to server!"); InitTcpGameClient(); Console.WriteLine(); } try { var chatMessage = new ChatMessage() { timestamp = DateTime.Now, user = userName, message = message }; var gameMessage = new NetworkGameMessage() { serviceName = "chat", operationName = "message", datamembers = new List <string> { chatMessage.AsJsonString } }; var bytes = gameMessage.AsJsonBytes; var stream = tcpClient.GetStream(); stream.Write(bytes); return(true); } catch (IOException e) { Console.WriteLine(e.Message + e.InnerException?.Message); return(false); } }
public void SendChatMessage(string message) { SendMessageToBrowser("send chatmessage"); // todo :: "re"ConnectToServer? ChatMessage chatMessage = new ChatMessage() { timestamp = DateTime.Now, user = userName, message = message }; NetworkGameMessage networkMessage = new NetworkGameMessage() { serviceName = "chat", operationName = "message", datamembers = new List <string> { chatMessage.AsJsonString } }; SendNetworkMessageToServer(networkMessage.AsJsonString); }