Beispiel #1
0
        private WebSocketState GetReadyState()
        {
            int readyState = BrowserInterop.GetReadyState(_innerWebSocket);

            // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState
            return(readyState switch
            {
                0 => WebSocketState.Connecting, // 0 (CONNECTING)
                1 => WebSocketState.Open,       // 1 (OPEN)
                2 => WebSocketState.CloseSent,  // 2 (CLOSING)
                3 => WebSocketState.Closed,     // 3 (CLOSED)
                _ => WebSocketState.None
            });
Beispiel #2
0
        private async Task ConnectAsyncCore(Uri uri, List <string>?requestedSubProtocols, CancellationToken cancellationToken)
        {
            try
            {
                string[]? subProtocols = requestedSubProtocols?.ToArray();
                var onClose = (int code, string reason) =>
                {
                    _closeStatus            = (WebSocketCloseStatus)code;
                    _closeStatusDescription = reason;
                    WebSocketState state = State;
                    if (state == WebSocketState.Connecting || state == WebSocketState.Open || state == WebSocketState.CloseSent)
                    {
                        _state = WebSocketState.Closed;
                    }
                };

                Memory <int> responseMemory = new Memory <int>(responseStatus);
                responseStatusHandle = responseMemory.Pin();

                _innerWebSocket = BrowserInterop.UnsafeCreate(uri.ToString(), subProtocols, responseStatusHandle.Value, onClose);
                var openTask    = BrowserInterop.WebSocketOpen(_innerWebSocket);
                var wrappedTask = CancelationHelper(openTask !, cancellationToken, _state);

                await wrappedTask.ConfigureAwait(true);

                if (State == WebSocketState.Connecting)
                {
                    _state = WebSocketState.Open;
                }
            }
            catch (OperationCanceledException ex)
            {
                _state = WebSocketState.Closed;
                if (_aborted)
                {
                    throw new WebSocketException(WebSocketError.Faulted, SR.net_webstatus_ConnectFailure, ex);
                }
                throw;
            }
            catch (Exception)
            {
                Dispose();
                throw;
            }
        }