/** * Start lobby in client-mode. */ public void StartClient(string address, int port) { var networkManager = ICNetworkUtilities.GetNetworkManager(); // Mark as client _isClient = true; Debug.Log("Connecting to '" + address + "' at port " + port.ToString()); // Setup event listeners networkManager.ClientError.RemoveAllListeners(); networkManager.ClientError.AddListener((conn, code) => { Debug.LogError("Error while connecting to server, code: " + code.ToString()); }); networkManager.ClientConnect.RemoveAllListeners(); networkManager.ClientConnect.AddListener((conn) => { Debug.Log("Connecting to server..."); }); // Connect to server networkManager.networkAddress = address; networkManager.networkPort = port; networkManager.StartClient(); // Disable start button startButton.enabled = false; startButton.onClick.RemoveAllListeners(); }
/** * Start the experiment (by changing the scene) */ public void StartExperiment(ICExperiment experiment) { if (!experiment) { return; } lobby.StopBroadcast(); // Switch back to neutral panel SwitchToPanel(experimentBrowser.gameObject); currentState = State.ExperimentBrowser; // Change level ICEventfulNetworkManager networkManager = ICNetworkUtilities.GetNetworkManager(); networkManager.ServerChangeScene(experiment.sceneName); }
/** * Start the lobby in server-mode. */ public void StartServer(ICExperiment experiment) { var networkDiscovery = ICNetworkUtilities.GetNetworkDiscovery(); var networkManager = ICNetworkUtilities.GetNetworkManager(); // Mark as server _isServer = true; // Update listeners SetupServerEventListeners(networkManager); // Start server and initialize broadcast networkManager.StartHost(); networkDiscovery.Initialize(); networkDiscovery.broadcastData = "NetworkManager:" + networkManager.networkAddress + ":" + networkManager.networkPort + ":" + experiment.getDisplayName(); if (!networkDiscovery.StartAsServer()) { throw new Exception("StartAsServer returned false in ICLobbyController."); } // Enable start button startButton.enabled = true; startButton.onClick.RemoveAllListeners(); startButton.onClick.AddListener(() => { experimentSetup.StartExperiment(experiment); }); // Remove old synchronization script if (syncScript) { NetworkServer.Destroy(syncScript.gameObject); Destroy(syncScript.gameObject); } // Spawn synchronization script on the network syncScript = GameObject.Instantiate(syncScriptPrefab).GetComponent <ICLobbySync>(); NetworkServer.Spawn(syncScript.gameObject); syncScript.transform.SetParent(gameObject.transform); }
/** * Update participant list - server side update */ private void UpdateParticipantList() { var networkManager = ICNetworkUtilities.GetNetworkManager(); Debug.Log("Updating list of participants, found: " + networkManager.connections.Count.ToString()); // Update server-side participant list participantList.items.Clear(); for (var i = 0; i < networkManager.connections.Count; i++) { participantList.items.Add( networkManager.connections[i].address, networkManager.connections[i].address); } // Invoke update function on clients if (syncScript) { syncScript.UpdateClientList(); } }
/** * Stop everything, used when cancelling the lobby. */ public void StopAll() { var networkDiscovery = ICNetworkUtilities.GetNetworkDiscovery(); var networkManager = ICNetworkUtilities.GetNetworkManager(); if (_isServer) { networkManager.StopHost(); if (networkDiscovery.isServer) { networkDiscovery.StopBroadcast(); } _isServer = false; } if (isClient) { networkManager.StopClient(); _isClient = false; } }