Beispiel #1
0
        public AutoConectWebSocket(string url, bool autoConnect = true, bool useProxy = false, System.Net.CookieContainer cookies = null, Dictionary <string, string> requestHeader = null) : base(url)
        {
            if (autoConnect)
            {
                Thread thread = new Thread(() =>
                {
                    while (true)
                    {
                        try
                        {
                            if (ReadyState != WebSocketState.Open)
                            {
                                if (cookies != null)
                                {
                                    Socket.Options.Cookies = cookies;
                                }

                                if (ReadyState != WebSocketState.Open && ReadyState != WebSocketState.Connecting)
                                {
                                    if (useProxy)
                                    {
                                        string proxyIP = HttpProxy.GetRandomProxyIP();
                                        if (proxyIP != "")
                                        {
                                            IWebProxy webProxy   = new WebProxy(proxyIP, HttpProxy.port);
                                            Socket.Options.Proxy = webProxy;
                                        }
                                    }
                                }

                                if (requestHeader != null)
                                {
                                    foreach (KeyValuePair <string, string> item in requestHeader)
                                    {
                                        Socket.Options.SetRequestHeader(item.Key, item.Value);
                                    }
                                }
                                Connect();
                            }
                        }
                        catch (Exception ex)
                        {
                            LogRecord.Warn("AutoConectWebSocket", string.Format("Client connect {0} error {1}.", Url, ex.ToString()));
                        }
                        resetEvent.WaitOne(3000);
                    }
                });
                thread.Start();
            }

            ConnectionClosed += AutoConectWebSocket_OnClose;
            Error            += AutoConectWebSocket_OnError;
            ConnectionOpened += AutoConectWebSocket_OnOpen;
        }
 protected bool Ping()
 {
     try
     {
         Ping(webSocket);
         return(true);
     }
     catch (Exception ex)
     {
         LogRecord.Warn("StompWebSocketMaketClientBase", $"{Name} Ping error {ex}.");
     }
     return(false);
 }