public WebSocket(IWindow window, String url, params String[] protocols)
        {
            _url    = new Url(url);
            _state  = WebSocketReadyState.Connecting;
            _cts    = new CancellationTokenSource();
            _window = window;

            if (_url.IsInvalid || _url.IsRelative)
            {
                throw new DomException(DomError.Syntax);
            }

            var invalid = protocols.Length - protocols.Distinct().Where(IsValid).Count();

            if (invalid > 0)
            {
                throw new DomException(DomError.Syntax);
            }

            _ws = new ClientWebSocket();

            foreach (var protocol in protocols)
            {
                _ws.Options.AddSubProtocol(protocol);
            }

            _ws.Options.KeepAliveInterval = TimeSpan.FromSeconds(20);
            ConnectAsync(url).Forget();
            _window.Unloaded += OnUnload;
        }
        private async Task CloseAsync()
        {
            _state = WebSocketReadyState.Closing;
            await _ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, _cts.Token).ConfigureAwait(false);

            CancelListener();
            OnDisconnected();
        }
        private async Task ConnectAsync(String url)
        {
            try
            {
                await _ws.ConnectAsync(new Uri(url), _cts.Token).ConfigureAwait(false);

                _state = WebSocketReadyState.Open;
                OnConnected();
                ListenAsync().Forget();
            }
            catch (Exception ex)
            {
                _state = WebSocketReadyState.Closed;
                OnError(ex);
            }
        }
 private void CancelListener()
 {
     _cts.Cancel();
     _ws.Abort();
     _state = WebSocketReadyState.Closed;
 }
Beispiel #5
0
 private void CancelListener()
 {
     _cts.Cancel();
     _ws.Abort();
     _state = WebSocketReadyState.Closed;
 }
Beispiel #6
0
 private async Task CloseAsync()
 {
     _state = WebSocketReadyState.Closing;
     await _ws.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, _cts.Token).ConfigureAwait(false);
     CancelListener();
     OnDisconnected();
 }
Beispiel #7
0
 private async Task ConnectAsync(String url)
 {
     try
     {
         await _ws.ConnectAsync(new Uri(url), _cts.Token).ConfigureAwait(false);
         _state = WebSocketReadyState.Open;
         OnConnected();
         ListenAsync().Forget();
     }
     catch (Exception ex)
     {
         _state = WebSocketReadyState.Closed;
         OnError(ex);
     }
 }
Beispiel #8
0
        public WebSocket(IWindow window, String url, params String[] protocols)
        {
            _url = new Url(url);
            _state = WebSocketReadyState.Connecting;
            _cts = new CancellationTokenSource();
            _window = window;

            if (_url.IsInvalid || _url.IsRelative)
            {
                throw new DomException(DomError.Syntax);
            }

            var invalid = protocols.Length - protocols.Distinct().Where(IsValid).Count();

            if (invalid > 0)
            {
                throw new DomException(DomError.Syntax);
            }

            _ws = new ClientWebSocket();

            foreach (var protocol in protocols)
            {
                _ws.Options.AddSubProtocol(protocol);
            }

            _ws.Options.KeepAliveInterval = TimeSpan.FromSeconds(20);
            ConnectAsync(url).Forget();
            _window.Unloaded += OnUnload;
        }