示例#1
0
        public async Task Dispose()
        {
            try
            {
                if (_dispose)
                {
                    return;
                }

                _dispose = true;

                HttpLogger?.Dispose();

                await SendText("close");

                webSocket?.Dispose();

                Thread.Sleep(1000);

                ReceiveHttpAction();
            }
            catch (Exception)
            {
            }
        }
示例#2
0
        public async Task <string> Connect(string uri)
        {
            string key = string.Empty;

            try
            {
                webSocket = new System.Net.WebSockets.Managed.ClientWebSocket();
                await webSocket.ConnectAsync(new Uri(uri), CancellationToken.None);

                key = await ReceiveFirst();

                receiveAction.Debug(!string.IsNullOrEmpty(key)
                    ? $"WebSocket Connect Success:{key},Server excute logs will accept from websocket"
                    : $"WebSocket Connect Fail,Server excute logs will accept from http request");
                if (!string.IsNullOrEmpty(key))
                {
                    mDetectionTimer = new System.Threading.Timer(Ping, null, 1000 * 2, 1000 * 2);
                }
                new Task(async() =>
                {
                    try
                    {
                        await Receive();
                    }
                    catch (Exception)
                    {
                        try
                        {
                            webSocket.Dispose();
                        }
                        catch (Exception)
                        {
                        }
                    }
                }).Start();

                new Task(() =>
                {
                    try
                    {
                        ReceiveHttp();
                    }
                    catch (Exception)
                    {
                    }
                }).Start();
            }
            catch (Exception e)
            {
                //ignore
                receiveAction.Debug($"WebSocket Connect[{uri}] Fail:" + e);
                receiveAction.Debug($"WebSocket Connect Fail,Server excute logs will accept from http request");
            }
            if (string.IsNullOrEmpty(key))
            {
                key = await ReceiveFirst();
            }
            return(key);
        }
示例#3
0
 public void Dispose()
 {
     if (_clientWebSocket.State == WebSocketState.Open)
     {
         _clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
     }
     _clientWebSocket.Dispose();
 }
示例#4
0
        public async Task Connect()
        {
            try
            {
                Log.Debug("Connecting to " + url);
                if (ws != null && (ws.State == System.Net.WebSockets.WebSocketState.Aborted || ws.State == System.Net.WebSockets.WebSocketState.Closed))
                {
                    ws.Dispose();
                    ws = null;
                    // ws = new ClientWebSocket();
                    // ws = (ClientWebSocket)SystemClientWebSocket.CreateClientWebSocket();
                    ws  = new System.Net.WebSockets.Managed.ClientWebSocket();
                    src = new CancellationTokenSource();
                }
                if (ws == null)
                {
                    // ws = new ClientWebSocket();
                    // ws = (ClientWebSocket)SystemClientWebSocket.CreateClientWebSocket();
                    ws  = new System.Net.WebSockets.Managed.ClientWebSocket();
                    src = new CancellationTokenSource();
                }
                if (ws.State == System.Net.WebSockets.WebSocketState.Connecting || ws.State == System.Net.WebSockets.WebSocketState.Open)
                {
                    return;
                }
                if (ws.State == System.Net.WebSockets.WebSocketState.CloseReceived)
                {
                    OnClose?.Invoke("Socket closing");
                    ws.Dispose();
                    ws = null;
                    return;
                }
                await ws.ConnectAsync(new Uri(url), src.Token);

                Log.Information("Connected to " + url);
                Task receiveTask = Task.Run(async() => await receiveLoop(), src.Token);
                Task pingTask    = Task.Run(async() => await PingLoop(), src.Token);
                OnOpen?.Invoke();
            }
            catch (Exception ex)
            {
                Log.Error(ex, "");
                OnClose?.Invoke(ex.Message);
            }
        }
        public Task ConnectAsync()
        {
            _tokenSource = new CancellationTokenSource();
            Uri wsUri = _urlConverter.HttpToWs(_uri, EIO.ToString(), Path, Parameters);

            if (_socket != null)
            {
                _socket.Dispose();
            }
            _socket = new System.Net.WebSockets.Managed.ClientWebSocket();
            bool executed = _socket.ConnectAsync(wsUri, CancellationToken.None).Wait(ConnectTimeout);

            if (!executed)
            {
                throw new TimeoutException();
            }
            Listen();
            return(Task.CompletedTask);
        }
示例#6
0
        private async Task WatchForMessagesFromServer()
        {
            var buffer = new byte[ReceiveChunkSize];

            try
            {
                while (_clientWebSocket.State == WebSocketState.Open)
                {
                    var stringResult = new StringBuilder();


                    WebSocketReceiveResult result;
                    do
                    {
                        result = await _clientWebSocket.ReceiveAsync(new ArraySegment <byte>(buffer), _cancellationToken);

                        if (result.MessageType == WebSocketMessageType.Close)
                        {
                            await _clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);

                            CallOnDisconnected();
                        }
                        else
                        {
                            var str = Encoding.UTF8.GetString(buffer, 0, result.Count);
                            stringResult.Append(str);
                        }
                    } while (!result.EndOfMessage);

                    CallOnMessage(stringResult);
                }
            }
            catch (Exception)
            {
                CallOnDisconnected();
            }
            finally
            {
                _clientWebSocket.Dispose();
            }
        }
示例#7
0
        public async Task Dispose()
        {
            try
            {
                if (_dispose)
                {
                    return;
                }

                _dispose = true;

                mDetectionTimer?.Dispose();

                HttpLogger?.Dispose();

                await SendText("close");

                webSocket?.Dispose();

                Thread.Sleep(1000);

                ReceiveHttpAction();
            }
            catch
            {
                //ignore
            }
            finally
            {
                try
                {
                    client.Dispose();
                }
                catch
                {
                    //ignore
                }
            }
        }
示例#8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="options"></param>
        /// <exception cref="TimeoutException"></exception>
        /// <exception cref="WebSocketException"></exception>
        /// <returns></returns>
        public async Task ConnectAsync(Uri uri)
        {
            if (_ws != null)
            {
                _ws.Dispose();
            }
            _ws = new System.Net.WebSockets.Managed.ClientWebSocket();
            Config?.Invoke(_ws.Options);

            _wsWorkTokenSource = new CancellationTokenSource();
            var wsConnectionTokenSource = new CancellationTokenSource(_io.Options.ConnectionTimeout);

            try
            {
                await _ws.ConnectAsync(uri, wsConnectionTokenSource.Token);

                _ = Task.Run(ListenAsync);
            }
            catch (TaskCanceledException)
            {
                throw new TimeoutException();
            }
        }
示例#9
0
 public void Close()
 {
     ws.Dispose();
 }
示例#10
0
 public void Dispose()
 {
     _ws.Dispose();
 }