/// <summary> /// Starts an endless while-loop, where the tcp client listens for new messages from the endpoint /// </summary> /// <returns></returns> public IEnumerator ListenForMessages() { while (!isDisconnecting) { if (Connected) { if (networkStream.DataAvailable) { ChatDataPackage msg = JsonUtility.FromJson <ChatDataPackage>(TCPHelper.ReadMessage(networkStream)); foreach (var item in msg.ChatDataPackages) { item.SenderClient = this; clientName = item.PlayerName; lock (ChatServer.tickMessages) { ChatServer.tickMessages.ChatDataPackages.Add(item); } } //Debug.Log(msg); } yield return(null); } else { isDisconnecting = true; DisconnectClient(); } } }
public IEnumerator ListenToServer() { Debug.Log("ListenToServer Started"); while (true) { if (client.GetStream().DataAvailable) { string packetString = TCPHelper.ReadMessage(client.GetStream()); ChatDataPackage packet = JsonUtility.FromJson <ChatDataPackage>(packetString); foreach (var item in packet.ChatDataPackages) { // dele op i grupper her string senderClient = item.PlayerName; string msg = senderClient + ": " + item.Message; // check for at ændre farve chatSystem.SendMessageToChat(msg, item.typeColor); msg = ""; } } yield return(null); } }
public string ReceiveFromClient() { string packetICarry = TCPHelper.ReadMessage(networkStream); return(packetICarry); }