Example #1
0
        private void AddPower(NetMsg msg)
        {
            string power = ((NetPower)msg).Suggestion;

            Player.Instance.AddPower(power);
        }
Example #2
0
 private void ChangeScene(NetMsg msg)
 {
     SceneManager.LoadScene(((NetChangeScene)msg).Scene);
 }
Example #3
0
        public void CheckIncomingMessage()
        {
            if (!isStarted)
            {
                return;
            }

            int recievingHostId;
            int connectionId;
            int channelId;

            byte[] recievedBuffer = new byte[BYTE_SIZE];
            int    dataSize;
            byte   error;

            NetworkEventType eventType = NetworkTransport.Receive(
                out recievingHostId,
                out connectionId,
                out channelId,
                recievedBuffer,
                BYTE_SIZE,
                out dataSize,
                out error);

            if ((NetworkError)error != NetworkError.Ok)
            {
                Debug.LogError($"Network Error: {(NetworkError)error}");
            }

            // Appropriately handle the network event
            switch (eventType)
            {
            case NetworkEventType.Nothing:
                break;

            case NetworkEventType.ConnectEvent:
                Debug.Log($"User={connectionId}, Host={hostId}");
                AddUserToList(connectionId);
                currentConnections++;
                break;

            case NetworkEventType.DataEvent:
                Debug.Log("Data recieved");
                BinaryFormatter formatter = new BinaryFormatter();
                MemoryStream    ms        = new MemoryStream(recievedBuffer);
                NetMsg          msg       = (NetMsg)formatter.Deserialize(ms);
                OnData(connectionId, msg);
                break;

            case NetworkEventType.DisconnectEvent:
                Debug.Log($"User {connectionId} has disconnected");
                RemoveUserFromList(connectionId);
                currentConnections--;
                break;

            default:
            case NetworkEventType.BroadcastEvent:
                Debug.Log("Unexpected network event type");
                break;
            }
        }