Exemple #1
0
        protected void ConnectWebSocket()
        {
            if (string.IsNullOrEmpty(WebSocketUri))
            {
                Debug.LogError("WebSocketUri must be set");
                return;
            }

            if (_ws == null || !_ws.IsConfigured())
            {
                var customHeaders = new List <KeyValuePair <string, string> >();
                if (Headers != null)
                {
                    foreach (UnityKeyValue header in Headers)
                    {
                        customHeaders.Add(new KeyValuePair <string, string>(header.key, header.value));
                    }
                }

                Debug.Log("Create Web Socket: " + WebSocketUri);
#if ENABLE_WINMD_SUPPORT
                Debug.Log("Using UWP Web Socket");
                _ws = new WebSocketUWP();
#elif UNITY_EDITOR || ENABLE_MONO
                Debug.Log("Using Mono Web Socket");
                _ws = new WebSocketMono();
#endif
                _ws.ConfigureWebSocket(WebSocketUri, customHeaders);
            }

            if (!isAttached)
            {
                Debug.Log("Connect Web Socket: " + _ws.Url());
                AttachHandlers();
                _ws.ConnectAsync();
            }
        }