/// <summary> /// Try to connect once to the setting server. /// </summary> /// <param name="hostname"> Internet Protocal </param> /// <param name="port"> Port Number </param> public void Connect(string hostname, int port) { try { // Close the socket if it is still open if (mSocket != null && mSocket.Connected) { mSocket.Shutdown(SocketShutdown.Both); System.Threading.Thread.Sleep(10); mSocket.Close(); } // Create the socket object mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Define the Server address and port IPEndPoint epServer = new IPEndPoint(IPAddress.Parse(hostname), port); // Connect to server non-Blocking method mSocket.Blocking = false; AsyncCallback onconnect = new AsyncCallback(OnConnect); mSocket.BeginConnect(epServer, onconnect, mSocket); // set receive callback SetupRecieveCallback(mSocket); } catch (Exception ex) { Debug.LogError("Server Connect failed: " + ex.Message); JCS_UtilityFunctions.PopIsConnectDialogue(); } }
/// <summary> /// Do ping pong packet action. /// </summary> /// <returns>true : connect, false: disconnect</returns> public void CheckConnectionWithTime() { // do the following script only when is online mode if (!JCS_NetworkSettings.instance.ONLINE_MODE) { return; } mConnectionCounter += Time.deltaTime; if (mConnectionCounter < JCS_NetworkConstant.CONNECT_TIME) { return; } else { if (SERVER_CLOSE) { Debug.Log("Server End!"); JCS_UtilityFunctions.PopIsConnectDialogue(); } else if (FIRST_LOGIN) { FIRST_LOGIN = false; JCS_PatchManager.instance.LoadNextLevel(); } mConnectionCounter = 0; } }