Example #1
0
        public static async Task Main(string[] args)
        {
            _timer = new Timer(OnTickAsync, null, 2000, 1);

RESTART:
            var socketOptions = new PureWebSocketOptions()
            {
                DebugMode           = true,
                SendDelay           = 100,
                IgnoreCertErrors    = true,
                MyReconnectStrategy = new ReconnectStrategy(3000, 6000)
            };

            _ws = new PureWebSocket("wss://demos.kaazing.com/echo", socketOptions);

            _ws.OnStateChanged += Ws_OnStateChanged;
            _ws.OnMessage      += Ws_OnMessage;
            _ws.OnClosed       += Ws_OnClosed;
            _ws.OnSendFailed   += Ws_OnSendFailed;
            try
            {
                await _ws.ConnectAsync();
            }
            catch (Exception e) {
                _ws.Dispose();
                goto RESTART;
            }


            Console.ReadLine();
            _ws.Dispose(true);
            goto RESTART;
        }
Example #2
0
        internal async Task <JsonZKill.Killmail> GetSocketResponce()
        {
            try
            {
                if (_webSocket == null || _webSocket.State != WebSocketState.Open)
                {
                    if (_webSocket?.State == WebSocketState.Connecting)
                    {
                        return(null);
                    }
                    var o = new PureWebSocketOptions();
                    _webSocket?.Dispose();
                    _webSocket            = new PureWebSocket(SettingsManager.Settings.Config.ZKillboardWebSocketUrl, o);
                    _webSocket.OnMessage += _webSocket_OnMessage;
                    _webSocket.OnError   += async exception => { await LogHelper.LogEx("_webSocket.OnError", exception, LogCat.ZKill); };

                    if (!_webSocket.Connect())
                    {
                        _webSocket?.Dispose();
                        _webSocket = null;
                        return(null);
                    }
                    else
                    {
                        if (!_webSocket.Send("{\"action\":\"sub\",\"channel\":\"killstream\"}"))
                        {
                            _webSocket?.Dispose();
                            _webSocket = null;
                            return(null);
                        }

                        await LogHelper.LogInfo("ZKB feed core WebSocket connect successful!", LogCat.ZKill);
                    }
                }

                if (!_webMailsQueue.IsEmpty && _webMailsQueue.TryDequeue(out var km))
                {
                    return(km);
                }
            }
            catch (Exception ex)
            {
                await LogHelper.LogEx("GetSocketResponce", ex, LogCat.ZKill);

                _webSocket?.Dispose();
                _webSocket = null;
            }

            return(null);
        }
Example #3
0
 public static void CloseWs()
 {
     if (_ws != null && _ws.State == WebSocketState.Open)
     {
         _ws.Dispose(true);
     }
 }
Example #4
0
        /// <summary>
        ///     Connect to the server and start processing messages
        /// </summary>
        public void Connect()
        {
            _webSocket?.Dispose();

            _disconnectCalled = false;

            // get the connection info from the signalr host
            var connInf = SignalR.Negotiate(_httpHost, _hubs);

            // we only work with websockets
            if (!connInf.TryWebSockets)
            {
                throw new WebSocketException(WebSocketError.UnsupportedProtocol,
                                             "WebSocket Connections Not Supported By This Host");
            }

            _connectionToken = connInf.ConnectionToken;
            _connectionId    = connInf.ConnectionId;

            _webSocket = SignalR.Connect(_wsHost, _connectionToken, _hubs);

            HookupEvents();

            _webSocket.Connect();

            if (Debug)
            {
                WriteLine($"Connect Called, URL: {_rootHost}");
            }
        }
Example #5
0
        public async void Start()
        {
            var socketOptions = new PureWebSocketOptions()
            {
                DebugMode           = false,
                SendDelay           = 100,
                IgnoreCertErrors    = true,
                MyReconnectStrategy = new ReconnectStrategy(2000, 4000, 20)
            };

            ws = new PureWebSocket("ws://" + _url + "/websocket", socketOptions);
            ws.OnStateChanged += Ws_OnStateChanged;
            ws.OnMessage      += Ws_OnMessage;
            ws.OnClosed       += Ws_OnClosed;
            ws.OnSendFailed   += Ws_OnSendFailed;
            await ws.ConnectAsync();

            Console.WriteLine("SENDING WEBSOCKET MESSAGE");
            string[] s = new string[1];
            s[0] = "1";
            WebSocket.ConnectMsg c = new WebSocket.ConnectMsg()
            {
                msg     = "connect",
                version = "1",
                support = s
            };

            await ws.SendAsync(JsonConvert.SerializeObject(c));

            Console.ReadLine();
            ws.Dispose(true);

            await ws.ConnectAsync();
        }
Example #6
0
        public static async Task Main(string[] args)
        {
            // this is just a timer to send data for us (simulate activity)
            _timer = new Timer(OnTickAsync, null, 4000, 1);

RESTART:
            var socketOptions = new PureWebSocketOptions
            {
                DebugMode           = false,                                // set this to true to see a ton O' logging
                SendDelay           = 100,                                  // the delay in ms between sending messages
                IgnoreCertErrors    = true,
                MyReconnectStrategy = new ReconnectStrategy(2000, 4000, 20) // automatic reconnect if connection is lost
            };

            _ws = new PureWebSocket("wss://demos.kaazing.com/echo", socketOptions, "MyOptionalInstanceName1");

            _ws.OnStateChanged += Ws_OnStateChanged;
            _ws.OnMessage      += Ws_OnMessage;
            _ws.OnClosed       += Ws_OnClosed;
            _ws.OnSendFailed   += Ws_OnSendFailed;
            await _ws.ConnectAsync();

            Console.ReadLine();
            _ws.Dispose(true);
            goto RESTART;
        }
Example #7
0
        public static void Main(string[] args)
        {
            _timer = new Timer(OnTick, null, 2000, 1);

RESTART:
            var socketOptions = new PureWebSocketOptions()
            {
                DebugMode           = true,
                SendDelay           = 100,
                IgnoreCertErrors    = true,
                MyReconnectStrategy = new ReconnectStrategy(2000, 4000, 20)
            };

            _ws = new PureWebSocket("wss://echo.websocket.org", socketOptions);

            _ws.OnStateChanged += Ws_OnStateChanged;
            _ws.OnMessage      += Ws_OnMessage;
            _ws.OnClosed       += Ws_OnClosed;
            _ws.OnSendFailed   += Ws_OnSendFailed;
            _ws.Connect();

            Console.ReadLine();
            _ws.Dispose(true);
            goto RESTART;
        }
        /// <summary>
        /// Disconnect the WebSocket and dispose the objects involved
        /// </summary>
        public void DisconnectAndDispose()
        {
            logger.LogInformation("DisconnectAndDispose() is being called");
            PureWebSocket pureWebSocket = new PureWebSocket(webSocketConfiguration.FullUrl, socketOptions);

            pureWebSocket?.Disconnect();
            pureWebSocket?.Dispose();
        }
Example #9
0
        private void ConnectionMonitorCheck(object state)
        {
            if (_disposedValue || _disconnectCalled)
            {
                return;
            }
            if (_lastMessageTime.AddSeconds(30) > DateTime.Now)
            {
                return;
            }

            if (Debug)
            {
                WriteLine("Connection Timeout, Atempting Reconnect");
            }

            _webSocket.Dispose(false);
            _webSocket = SignalR.Reconnect(_wsHost, _connectionToken, _hubs, _lastMessageId, _groupsToken);

            HookupEvents();

            _webSocket.Connect();
        }
Example #10
0
 public void Close()
 {
     if (_socket != null && _socket.State == WebSocketState.Open)
     {
         _socket.OnStateChanged -= _socket_OnStateChanged;
         _socket.OnMessage      -= _socket_OnMessage;
         _socket.OnClosed       -= _socket_OnClosed;
         _socket.OnSendFailed   -= _socket_OnSendFailed;
         _socket.OnOpened       -= _socket_OnOpened;
         _socket.OnError        -= _socket_OnError;
         _socket.Dispose(true);
         _socket = null;
     }
 }
Example #11
0
        /// <summary>
        /// Generates a listener for socket close events.
        /// </summary>
        /// <param name="connection">A refernce to the connection.</param>
        /// <returns></returns>
        private Closed Socket_OnClosed(IActorRef connection)
        {
            return((WebSocketCloseStatus reason) => {
                Log.Information("Socket closed : {0}.", reason);

                if (_socket.State == WebSocketState.Open ||
                    _socket.State == WebSocketState.CloseReceived ||
                    _socket.State == WebSocketState.CloseSent)
                {
                    _socket.Dispose();
                    _socket = null;
                }

                connection.Tell(new Socket_Disconnected());
            });
        }
Example #12
0
        public void Disconnect(Action callback)
        {
            if (_conn != null)
            {
                // _conn.OnClose(); //TODO how to clear event handler?
                if (_conn.State == WebSocketState.Open)
                {
                    _conn.Disconnect();
                }

                _conn.Dispose();
                _conn = null;
            }

            callback?.Invoke();
        }
Example #13
0
        public static void Main(string[] args)
        {
            _timer = new Timer(OnTick, null, 2000, 1);

RESTART:
            _ws                 = new PureWebSocket("wss://echo.websocket.org", new ReconnectStrategy(10000, 60000));
            _ws.SendDelay       = 100;
            _ws.OnStateChanged += Ws_OnStateChanged;
            _ws.OnMessage      += Ws_OnMessage;
            _ws.OnClosed       += Ws_OnClosed;
            _ws.OnSendFailed   += Ws_OnSendFailed;
            _ws.Connect();

            Console.ReadLine();
            _ws.Dispose(true);
            goto RESTART;
        }
Example #14
0
        private bool _disposedValue; // To detect redundant calls

        protected virtual void Dispose(bool disposing, bool waitForSendsToComplete)
        {
            if (!_disposedValue)
            {
                if (disposing)
                {
                    try
                    {
                        _socket.Dispose(waitForSendsToComplete);
                    }
                    catch
                    {
                        // ignored
                    }
                }

                _disposedValue = true;
            }
        }