private bool StartServer(ConnectionConfig config, int maxConnections) { InitializeSingleton(); OnStartServer(); if (runInBackground) { Application.runInBackground = true; } QNetworkCRC.scriptCRCCheck = scriptCRCCheck; QNetworkServer.useWebSockets = useWebSockets; if (m_GlobalConfig != null) { NetworkTransport.Init(m_GlobalConfig); } if (customConfig && m_ConnectionConfig != null && config == null) { m_ConnectionConfig.Channels.Clear(); foreach (var channel in channels) { m_ConnectionConfig.AddChannel(channel); } QNetworkServer.Configure(m_ConnectionConfig, this.maxConnections); } if (config != null) { QNetworkServer.Configure(config, maxConnections); } if (serverBindToIP && !string.IsNullOrEmpty(serverBindAddress)) { if (!QNetworkServer.Listen(serverBindAddress, networkPort)) { QLog.FatalError($"StartServer listen on {serverBindAddress} failed."); return(false); } } else if (!QNetworkServer.Listen(networkPort)) { QLog.FatalError("StartServer listen failed."); return(false); } RegisterServerMessages(); QLog.Log($"NetworkManager StartServer port:{networkPort}"); isNetworkActive = true; var name = SceneManager.GetSceneAt(0).name; if (!string.IsNullOrEmpty(onlineScene) && onlineScene != name && onlineScene != offlineScene) { ServerChangeScene(onlineScene); } else { QNetworkServer.SpawnObjects(); } return(true); }
private void OnServerAddPlayerInternal(QNetworkConnection conn, short playerControllerId) { if (playerPrefab == null) { QLog.FatalError("The PlayerPrefab is empty on the QSBNetworkManager. Please setup a PlayerPrefab object."); } else if (playerPrefab.GetComponent <QNetworkIdentity>() == null) { QLog.FatalError("The PlayerPrefab does not have a QSBNetworkIdentity. Please add a QSBNetworkIdentity to the player prefab."); } else if (playerControllerId < conn.PlayerControllers.Count && conn.PlayerControllers[playerControllerId].IsValid && conn.PlayerControllers[playerControllerId].Gameobject != null) { QLog.Warning("There is already a player at that playerControllerId for this connections."); } else { var player = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity); QNetworkServer.AddPlayerForConnection(conn, player, playerControllerId); } }