void OnRead(IAsyncResult ar)
        {
            int length = stream.EndRead(ar);

            if (length <= 0)
            { // Connection closed
                TCPGame.instance.OnDisconnect(this);
                return;
            }

            string newMessage = System.Text.Encoding.UTF8.GetString(readBuffer, 0, length);

            TCPGame.messageToDisplay += newMessage + Environment.NewLine;

            if (TCPGame.instance.isServer)
            {
                TCPGame.BroadcastChatMessage(newMessage);
            }

            stream.BeginRead(readBuffer, 0, readBuffer.Length, OnRead, null);
        }
Beispiel #2
0
 public void Awake()
 {
     instance = this;
     Debug.Log("hellllooooo?");
     if (serverIp == null)
     { // Server: start listening for connections
         this.isServer = true;
         listener      = new TcpListener(System.Net.IPAddress.Any, Globals.port);
         listener.Start();
         listener.BeginAcceptTcpClient(OnServerConnect, null);
         Debug.Log("on wig?");
     }
     else
     { // Client: try connecting to the server
         TcpClient          client          = new TcpClient();
         TcpConnectedClient connectedClient = new TcpConnectedClient(client);
         clientList.Add(connectedClient);
         client.BeginConnect(serverIp, Globals.port, (ar) => connectedClient.EndConnect(ar), null);
         Debug.Log("oy bruv we in");
     }
 }