예제 #1
0
    // Update is called once per frame
    void Update()
    {
        int recvHostId;
        int recvConnectionId;
        int recvChannelId;

        byte[] recvBuffer = new byte[1024];
        int    bufferSize = 1024;
        int    datasize;

        NetworkEventType recvNetworkEvent = NetworkTransport.Receive(out recvHostId,
                                                                     out recvConnectionId, out recvChannelId, recvBuffer, bufferSize,
                                                                     out datasize, out error);

        switch (recvNetworkEvent)
        {
        case NetworkEventType.ConnectEvent:
            //code for starting game
            if (!connected)
            {
                connectionIdClient = recvConnectionId;
                hostIdClient       = recvHostId;
                Debug.Log("ConnectEvent Triggered.");
                connected = true;
                networkControl.ServerConnected();
            }
            else if (recvConnectionId != connectionIdClient || recvHostId != hostIdClient)
            {
                NetworkTransport.Disconnect(recvHostId, recvConnectionId, out error);
            }
            break;

        case NetworkEventType.DataEvent:
            string msg = Encoding.Unicode.GetString(recvBuffer, 0, datasize);
            Debug.Log("Receiving " + msg);
            string[] splitData = msg.Split('|');
            networkControl.ParseMessage(splitData);
            break;

        case NetworkEventType.DisconnectEvent:
            connected = false;
            networkControl.GameTimedOut();
            break;
        }
        if (connected)
        {
            CancelInvoke();
        }
    }
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        int recvHostId;
        int recvConnectionId;
        int recvChannelId;

        byte[] recvBuffer = new byte[1024];
        int    bufferSize = 1024;
        int    datasize;

        NetworkEventType recvNetworkEvent = NetworkTransport.Receive(out recvHostId,
                                                                     out recvConnectionId, out recvChannelId, recvBuffer, bufferSize, out datasize, out error);

        switch (recvNetworkEvent)
        {
        case NetworkEventType.ConnectEvent:
            if (!connected)
            {
                connectionIdServer = recvConnectionId;
                hostIdServer       = recvHostId;
                connected          = true;
            }
            break;

        case NetworkEventType.DisconnectEvent:
            connected = false;
            networkControl.GameTimedOut();
            break;

        case NetworkEventType.DataEvent:
            string msg = Encoding.Unicode.GetString(recvBuffer, 0, datasize);
            Debug.Log("Receiving " + msg);
            string[] splitData = msg.Split('|');
            networkControl.ParseMessage(splitData);
            break;
        }
    }