public bool StartClient(string address, int port) { if (IsClientStarted) { Logging.Log(nameof(WsClientWrapper), "Client started, so it can't be started again"); return(false); } #if !UNITY_WEBGL || UNITY_EDITOR IPAddress[] ipAddresses = Dns.GetHostAddresses(address); if (ipAddresses.Length == 0) { Logging.Log(nameof(WsClientWrapper), "Cannot find IP addresses from " + address); return(false); } int indexOfAddress = -1; for (int i = 0; i < ipAddresses.Length; ++i) { if (ipAddresses[i].AddressFamily == AddressFamily.InterNetwork) { indexOfAddress = i; break; } } if (indexOfAddress < 0) { Logging.Log(nameof(WsClientWrapper), "Cannot find index of address from " + address); return(false); } string url = (secure ? "wss://" : "ws://") + ipAddresses[indexOfAddress] + ":" + port; Logging.Log(nameof(WsClientWrapper), $"Connecting to {url}"); if (secure) { SslContext context = new SslContext(sslProtocols, new X509Certificate2(), CertValidationCallback); wssClient = new WssTransportClient(clientEventQueue, context, ipAddresses[indexOfAddress], port); wssClient.OptionDualMode = true; wssClient.OptionNoDelay = true; return(wssClient.ConnectAsync()); } else { wsClient = new WsTransportClient(clientEventQueue, ipAddresses[indexOfAddress], port); wsClient.OptionDualMode = true; wsClient.OptionNoDelay = true; return(wsClient.ConnectAsync()); } #else string url = (secure ? "wss://" : "ws://") + address + ":" + port; Logging.Log(nameof(WsClientWrapper), $"Connecting to {url}"); wsNativeInstance = SocketCreate(url.ToString()); return(true); #endif }
public void StopClient() { #if !UNITY_WEBGL || UNITY_EDITOR if (wssClient != null) { wssClient.Dispose(); } wssClient = null; if (wsClient != null) { wsClient.Dispose(); } wsClient = null; #else SocketClose(wsNativeInstance); #endif }