Ejemplo n.º 1
0
    void Update()
    {
        if (!_isStarted)
        {
            return;
        }
        int recHostId;
        int connectionId;
        int channelId;

        byte[]           recBuffer  = new byte[1024];
        int              bufferSize = 1024;
        int              dataSize;
        byte             error;
        NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

        switch (recData)
        {
        case NetworkEventType.Nothing:
            break;

        case NetworkEventType.ConnectEvent:
        {
            bool IamConnectingToTheServer     = !_isServer;
            bool IamTheServerBeingConnectedTo = _isServer;

            if (IamConnectingToTheServer)
            {
                //save the hosts details
                hostId = recHostId;
                connId = connectionId;

                //Send my name to the server

                NetworkMessage.Send(hostId, connId, m_CommunicationChannel, MessageType.setName, myName);
            }

            if (IamTheServerBeingConnectedTo)
            {        //Add person to list here
                connId = connectionId;
                connectedUsers.Add(connId, "unset");
            }


            Debug.Log(String.Format("Connect from host {0} connection {1}", recHostId, connectionId));
            break;
        }

        case NetworkEventType.DataEvent:                  //if server will receive echo it will send it back to client, when client will receive echo from serve wit will send other message
        {
            recievedCount++;



            Debug.Log(String.Format("MEOW Received event host {0} connection {1} channel {2} message length {3}", recHostId, connectionId, channelId, dataSize));
            //msg = NetworkConverter.NetToStr(recBuffer, dataSize);
            // DecodeMessage(msg);

            DecodeMessage(NetworkConverter.NetToStr(recBuffer, dataSize), connectionId);

            //this should only happen for messages?
            if (_isServer)
            {
                string   recStr    = NetworkConverter.NetToStr(recBuffer, dataSize);
                string[] pieceList = recStr.Split(new string[] { "--" }, StringSplitOptions.None);

                string type    = pieceList[0];
                string payload = pieceList[1];

                payload = connectedUsers[connectionId] + ": " + payload;

                string sendStr = type + "--" + payload;

                recBuffer = NetworkConverter.StrToNet(sendStr);

                foreach (var connection in connectedUsers)
                {
                    NetworkTransport.Send(hostId, connection.Key, m_CommunicationChannel, recBuffer, recBuffer.Length, out error);
                }
            }
        }
        break;

        case NetworkEventType.DisconnectEvent:
        {
            if (!_isServer)
            {
                Debug.Log(String.Format("DisConnect from host {0} connection {1}", recHostId, connectionId));
                break;
            }
            else
            {
                //remove person from list here
                connectedUsers.Remove(connectionId);
                break;
            }
        }
        }
    }