private void Start() { _listener.NetworkReceiveEvent += (fromPeer, dataReader, deliveryMethod) => { using (var writer = new NetDataWriter()) { try { switch ((MessageType)dataReader.GetByte()) { case MessageType.AuthorizationResponse: //We got auth response Players.Add(new Player(dataReader.GetString(), dataReader.GetUInt(), true)); break; case MessageType.UserConnected: //new user just connected dataReader.GetString(); var connId = dataReader.GetUInt(); UserHandler.OnlineCount++; UserHandler.UpdateHeader(UserHandler.OnlineCount); if (connId == LocalId) { return; } writer.Put(connId); SendMessage(MessageType.GetPlayerStats, writer); //UserHandler.UpdateUserState(UserHandler.Users.FirstOrDefault(x => x.Id == connectedId), UserStare.Online); break; case MessageType.UserDisconnected: //user just disconnected UserHandler.RemoveUser( UserHandler.Users.FirstOrDefault(x => x.Id == dataReader.GetUInt())); UserHandler.OnlineCount--; UserHandler.UpdateHeader(UserHandler.OnlineCount); break; case MessageType.GetConcurrentUsersResponse: //we got concurrent users response var count = dataReader.GetUInt(); UserHandler.UpdateHeader(count); Debug.Log(count); for (var i = 0; i < count; i++) { var message = dataReader.GetUInt(); //writer.Put(message); ConnectedPeersId.Add(message); //SendMessage(MessageType.GetPlayerStats, writer); } break; case MessageType.UpdateProfileSettingsResponse: //We got UpdateProfileSettingsResponse response break; case MessageType.PlayerStatsResponse: //We got player stats response var user = dataReader.Get <User>(); UserHandler.AddUser(user); break; case MessageType.NewRoomAvailable: //We got avatar response break; case MessageType.IncomingChatMessage: //We got incoming chat message ChatHandler.ReceiveMessage(dataReader.GetString(), dataReader.GetString(), (PlayerType)Enum.Parse(typeof(PlayerType), dataReader.GetString())); break; default: throw new ArgumentOutOfRangeException(); } dataReader.Recycle(); } catch (Exception) { // ignored } writer.Reset(); writer.Dispose(); } }; _listener.PeerDisconnectedEvent += (peer, info) => { Debug.Log($"{peer.EndPoint} just disconnected : {info.Reason}"); _client.Stop(); }; _listener.PeerConnectedEvent += peer => { _isConnected = true; SendMessage(MessageType.GetConcurrentUsers, new NetDataWriter()); Debug.Log("peer connected"); //ScreenManager.Instance.ChangeScreen(); ScreenManager.Instance.ChangeScreen(ScreenManager.Instance.GetScreen(ScreenType.MainScreen)); //WindowButtonHandler.ShowMainWindow(); }; }