Exemple #1
0
    // Update is called once per frame
    void Update()
    {
        int connectionId;
        int channelId;

        byte[]           recBuffer  = new byte[1024];
        int              bufferSize = 1024;
        int              dataSize;
        byte             error;
        int              recPort;
        string           addr;
        NetworkEventType recData = NetworkTransport.ReceiveFromHost(hostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

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

        case NetworkEventType.ConnectEvent:                    //2
            NetworkID network;
            NodeID    dstNode;
            NetworkTransport.GetConnectionInfo(hostId, connectionId, out addr, out recPort, out network, out dstNode, out error);
            Debug.Log("Connect request: " + (NetworkError)error + " on conId " + connectionId + " addr " + addr + " port " + recPort + " net " + network + " dstNode " + dstNode);
            if (!otherPlayerConnections.ContainsValue(addr))
            {
                otherPlayerConnections[connectionId] = addr;
            }
            break;

        case NetworkEventType.DataEvent:                       //3
            string data = Encoding.ASCII.GetString(recBuffer);
            Debug.Log("Got data: " + data + " on conId " + connectionId);
            if (recieve)
            {
                micMan.HandleSpeech(data);
            }
            break;

        case NetworkEventType.DisconnectEvent:                 //4
            Debug.Log("Disconnect: " + (NetworkError)error + " on conId " + connectionId);
            otherPlayerConnections.Remove(connectionId);
            break;

        case NetworkEventType.BroadcastEvent:
            addr = NetworkTransport.GetBroadcastConnectionInfo(hostId, out recPort, out error);
            int recSize;
            NetworkTransport.GetBroadcastConnectionMessage(hostId, recBuffer, bufferSize, out recSize, out error);
            string message = Encoding.ASCII.GetString(recBuffer, 0, recSize);
            //Debug.Log("saw broadcast from " + addr + " with message of len " + message.Length +  ":" +  message + "|");
            if (message != deviceId || !ignoreSelf)
            {
                if (!otherPlayerConnections.ContainsValue(addr))
                {
                    int conId = NetworkTransport.Connect(hostId, addr, port, 0, out error);
                    Debug.Log("Connect attempt to " + addr + " result: " + (NetworkError)error + " conid:" + conId);
                    otherPlayerConnections[conId] = addr;
                }
            }
            break;
        }
    }