Ejemplo n.º 1
0
 private void InitHangQingWebSocket()
 {
     try
     {
         HQAbortedWs = false;
         HQWs        = new WebSocket(WEBSOCKET_API.Substring(0, WEBSOCKET_API.Length - 3));
         HQWs.Error += (sender, e) =>
         {
             Console.WriteLine("Error:" + e.Exception.Message.ToString());
         };
         if (port != 0)
         {
             var proxy = new HttpConnectProxy(new IPEndPoint(IPAddress.Parse(address), port));
             HQWs.Proxy = proxy;
         }
         HQWs.Opened       += OnHQOpened;
         HQWs.Closed       += OnHQClosed;
         HQWs.DataReceived += ReceviedHQData;
         HQWs.Open();
         if (!InitHQXinTiao)
         {
             InitHQXinTiao = true;
             HQXinTiaoTh   = new Thread(new ThreadStart(CheckHQXinTiao));
             HQLastTime    = DateTime.Now;
             HQXinTiaoTh.Start();
             SendHQXinTiao = false;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Exception:" + ex.Message);
     }
 }
Ejemplo n.º 2
0
        internal RippleApi(IConfiguration config, ILogger logger, IFeeProvider feeProvider, ITransactionSigner txSigner, string dataApiUrl, string exchIssuerAddress, string fiatCurrencyCode)
        {
            _config = config;
            _logger = logger;
            _feeProvider = feeProvider;
            _txSigner = txSigner;
            _dataApiUrl = dataApiUrl;
            _issuerAddress = exchIssuerAddress;
            _fiatCurreny = fiatCurrencyCode;
            _walletAddress = _config.AccessKey;

            _webSocket = new WebSocket(_rippleSocketUri = _config.GetValue("server"));

            string proxyHost = _config.GetValue("proxyHost");
            string proxyPort = _config.GetValue("proxyPort");
            if (null != proxyHost && null != proxyPort)
            {
                //TODO: these two lines don't belong here, rather to WebClient2
                _webProxy = new WebProxy(proxyHost, int.Parse(proxyPort));
                _webProxy.Credentials = CredentialCache.DefaultCredentials;

                var wsProxy = new HttpConnectProxy(new DnsEndPoint(proxyHost, Int32.Parse(proxyPort)), "1.0");
                _webSocket.Proxy = wsProxy;
            }

            _webSocket.Opened += websocket_Opened;
            _webSocket.Error += websocket_Error;
            _webSocket.Closed += websocket_Closed;
            _webSocket.MessageReceived += websocket_MessageReceived;
        }
Ejemplo n.º 3
0
 public bool InitWebSocket()
 {
     try
     {
         AbortedWs        = false;
         websocket        = new WebSocket(WEBSOCKET_API);
         websocket.Error += (sender, e) =>
         {
             Console.WriteLine("Error:" + e.Exception.Message.ToString());
         };
         if (port != 0)
         {
             var proxy = new HttpConnectProxy(new IPEndPoint(IPAddress.Parse(address), port));
             websocket.Proxy = proxy;
         }
         websocket.Opened          += OnOpened;
         websocket.Closed          += OnClosed;
         websocket.DataReceived    += ReceviedData;
         websocket.MessageReceived += ReceviedMsg;
         websocket.Open();
         if (!InitXinTiao)
         {
             InitXinTiao = true;
             XinTiaoTh   = new Thread(new ThreadStart(CheckXinTiao));
             LastTime    = DateTime.Now;
             XinTiaoTh.Start();
             SendXinTiao = false;
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Exception:" + ex.Message);
     }
     return(true);
 }
Ejemplo n.º 4
0
        protected override void DoOpen()
        {
            var log = LogManager.GetLogger(Global.CallerName());

            log.Info("DoOpen uri =" + this.Uri());

            ws = new WebSocket4Net.WebSocket(this.Uri(), String.Empty, Cookies, MyExtraHeaders)
            {
                EnableAutoSendPing = false
            };
            if (ServerCertificate.Ignore)
            {
                var security = ws.Security;

                if (security != null)
                {
                    security.AllowUnstrustedCertificate   = true;
                    security.AllowNameMismatchCertificate = true;
                }
            }
            ws.Opened          += ws_Opened;
            ws.Closed          += ws_Closed;
            ws.MessageReceived += ws_MessageReceived;
            ws.DataReceived    += ws_DataReceived;
            ws.Error           += ws_Error;

            var destUrl = new UriBuilder(this.Uri());

            if (this.Secure)
            {
                destUrl.Scheme = "https";
            }
            else
            {
                destUrl.Scheme = "http";
            }

            // We don't want to overwrite a user defined proxy.
            var hasNoUserDefinedProxy = Proxy == null;

            if (hasNoUserDefinedProxy)
            {
                Proxy = WebRequest.DefaultWebProxy;
            }

            // The "Proxy" property could be null, in this case we'll let "IsBypassed" be true, so no proxy is used.
            var useProxy = !(Proxy?.IsBypassed(destUrl.Uri) ?? true);

            if (useProxy)
            {
                var proxyUrl = Proxy.GetProxy(destUrl.Uri);
                var proxy    = new HttpConnectProxy(new DnsEndPoint(proxyUrl.Host, proxyUrl.Port), destUrl.Host);
                ws.Proxy = proxy;
            }

            ws.Open();
        }
Ejemplo n.º 5
0
        protected override void DoOpen()
        {
            var log = LogManager.GetLogger(Global.CallerName());

            log.Info("DoOpen uri =" + this.Uri());

            ws = new WebSocket4Net.WebSocket(this.Uri(), String.Empty, Cookies, MyExtraHeaders)
            {
                EnableAutoSendPing = false
            };
            if (ServerCertificate.Ignore)
            {
                var security = ws.Security;

                if (security != null)
                {
                    security.AllowUnstrustedCertificate   = true;
                    security.AllowNameMismatchCertificate = true;
                }
            }
            ws.Opened          += ws_Opened;
            ws.Closed          += ws_Closed;
            ws.MessageReceived += ws_MessageReceived;
            ws.DataReceived    += ws_DataReceived;
            ws.Error           += ws_Error;

            var destUrl = new UriBuilder(this.Uri());

            if (this.Secure)
            {
                destUrl.Scheme = "https";
            }
            else
            {
                destUrl.Scheme = "http";
            }

            var useProxy = !WebRequest.DefaultWebProxy.IsBypassed(destUrl.Uri);

            log.Info($"useProxy = {useProxy}");
            if (useProxy)
            {
                var proxyUrl = WebRequest.DefaultWebProxy.GetProxy(destUrl.Uri);

                log.Info($"proxyUrl = {proxyUrl}");
                log.Info($"destUrl.Host = {destUrl.Host}");

                var proxy = new HttpConnectProxy(new DnsEndPoint(proxyUrl.Host, proxyUrl.Port), destUrl.Host);
                ws.Proxy = proxy;
            }
            ws.Open();
        }
Ejemplo n.º 6
0
        public void TestMatchSecondTime()
        {
            var server      = CreateSimplyRespond(Encoding.ASCII.GetBytes("OK"));
            var proxyServer = CreateSimplyRespond(Encoding.ASCII.GetBytes("OK"));

            ManualResetEvent wait = new ManualResetEvent(false);

            var            proxy     = new HttpConnectProxy(proxyServer.LocalEndPoint);
            ProxyEventArgs eventArgs = null;

            proxy.Completed += (a, e) =>
            {
                eventArgs = e;
                wait.Set();
            };
            proxy.Connect(server.LocalEndPoint);

            Assert.True(wait.WaitOne(5000));
            Assert.Null(eventArgs.Exception);
            Assert.True(eventArgs.Connected);
        }
Ejemplo n.º 7
0
        protected override void DoOpen()
        {
            var log = LogManager.GetLogger(Global.CallerName());

            log.Info("DoOpen uri =" + this.Uri());

            ws = new WebSocket4Net.WebSocket(this.Uri(), "", Cookies);
            ws.EnableAutoSendPing = false;
            if (ServerCertificate.Ignore)
            {
                var security = ws.Security;

                if (security != null)
                {
                    security.AllowUnstrustedCertificate   = true;
                    security.AllowNameMismatchCertificate = true;
                }
            }
            ws.Opened          += ws_Opened;
            ws.Closed          += ws_Closed;
            ws.MessageReceived += ws_MessageReceived;
            ws.DataReceived    += ws_DataReceived;
            ws.Error           += ws_Error;

            var destUrl = new UriBuilder(this.Uri());

            destUrl.Scheme = this.Secure ? "https" : "http";
            var useProxy = !WebRequest.DefaultWebProxy.IsBypassed(destUrl.Uri);

            if (useProxy)
            {
                var proxyUrl          = WebRequest.DefaultWebProxy.GetProxy(destUrl.Uri);
                var receiveBufferSize = 512 * 1024;
                var proxy             = new HttpConnectProxy(new DnsEndPoint(proxyUrl.Host, proxyUrl.Port), receiveBufferSize, destUrl.Host);
                ws.Proxy = proxy;
            }
            ws.Open();
        }
Ejemplo n.º 8
0
        public void SetProxy(string host, int port)
        {
            var proxy = new HttpConnectProxy(new IPEndPoint(IPAddress.Parse(host), port));

            _socket.Proxy = (SuperSocket.ClientEngine.IProxyConnector)proxy;
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            // remote sevice information
            string remoteHost = "112.74.207.57";
            //string remoteHost = "10.193.228.69"; //"a23126-04"; // 10.193.228.69
            string remotePort = "2012";

            // get default web proxy
            //Uri serviceUrl = new Uri(string.Format("http://{0}", remoteHost));
            Uri serviceUrl = new Uri(string.Format("http://www.baidu.com", remoteHost));
            Uri proxyUri   = WebRequest.DefaultWebProxy.GetProxy(serviceUrl);

            if (serviceUrl == proxyUri)
            {
                proxyUri = null;
            }

            //wsClient = new WebSocket("ws://112.74.207.57:2012/");
            //wsClient = new WebSocket("ws://112.74.207.5:2012/", version: WebSocketVersion.None,
            //httpConnectProxy: new DnsEndPoint("wwwgate0.mot.com", 1080));
            //wsClient = new WebSocket("ws://a23126-04:2012/", "", null, null, "", "", WebSocketVersion.None,
            //    new DnsEndPoint("wwwgate0.mot.com", 1080));


            //List<KeyValuePair<String, String>> list_customHeaderItems = new List<KeyValuePair<string, string>>();
            //String userName = "******";
            //String userPassword = "******";
            //string authInfo = userName + ":" + userPassword;
            //authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            //list_customHeaderItems.Add(new KeyValuePair<string, string>("Authorization", "Basic " + authInfo));

            wsClient = new WebSocket(string.Format("ws://{0}:{1}/", remoteHost, remotePort));
            //customHeaderItems: list_customHeaderItems);

            if (proxyUri != null)
            {
                Console.WriteLine("use proxy: [" + proxyUri.ToString() + "]");
                HttpConnectProxy proxy = new HttpConnectProxy(new DnsEndPoint(proxyUri.Host, proxyUri.Port));
                wsClient.Proxy = proxy;
            }

            wsClient.Opened          += wsClient_Opened;
            wsClient.DataReceived    += wsClient_DataReceived;
            wsClient.MessageReceived += wsClient_MessageReceived;
            wsClient.Closed          += wsClient_Closed;
            wsClient.Error           += wsClient_Error;

            wsClient.Open();
            if (!OpenedEvent.WaitOne(5000))
            {
                Console.WriteLine("Failed to open!");
                Console.ReadKey();
                return;
            }

            Random rnd = new Random();
            string msg = "echo " + rnd.Next(0, 100).ToString();

            Console.WriteLine("sending msg ... [" + msg + "]");
            wsClient.Send(msg);

            Console.ReadKey();
        }