예제 #1
0
        public async Task <WebSocket> ConnectAsync(HttpWebRequest request, CancellationToken cancellationToken)
        {
            HttpWebResponse response;

            using (cancellationToken.Register(request.Abort)) {
                response = (HttpWebResponse)await request.GetResponseAsync();
            }

            InspectResponse?.Invoke(response);

            // TODO: Validate handshake
            HttpStatusCode statusCode = response.StatusCode;

            if (statusCode != HttpStatusCode.SwitchingProtocols)
            {
                response.Dispose();
                throw new InvalidOperationException("Incomplete handshake, invalid status code: " + statusCode);
            }
            // TODO: Validate Sec-WebSocket-Key/Sec-WebSocket-Accept

            string subProtocol = response.Headers[Constants.Headers.SecWebSocketProtocol];

            if (!string.IsNullOrEmpty(subProtocol) && !SubProtocols.Contains(subProtocol, StringComparer.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("Incomplete handshake, the server specified an unknown sub-protocol: " + subProtocol);
            }

            var stream = response.GetResponseStream();

            return(CommonWebSocket.CreateClientWebSocket(stream, subProtocol, KeepAliveInterval, ReceiveBufferSize, UseZeroMask));
        }
예제 #2
0
        private Handshake GetHandshakeResponse(Handshake handshakeRequest)
        {
            Handshake handshakeResponse;

            if (handshakeRequest.ReservedOctects != 0)
            {
                handshakeResponse = new Handshake(HandshakeErrorCode.UseOfReservedBits);
            }
            else
            {
                SerializerType serializerType = handshakeRequest.SerializerType;

                string requestedSubprotocol = GetSubProtocol(serializerType);

                if (!SubProtocols.Contains(requestedSubprotocol))
                {
                    handshakeResponse = new Handshake(HandshakeErrorCode.SerializerUnsupported);
                }
                else
                {
                    handshakeResponse = new Handshake(MaxSize, serializerType);
                }
            }

            return(handshakeResponse);
        }
예제 #3
0
        public async Task <WebSocket> ConnectAsync(Uri uri, CancellationToken cancellationToken)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            CancellationTokenRegistration cancellation = cancellationToken.Register(() => request.Abort());

            request.Headers[Constants.Headers.SecWebSocketVersion] = Constants.Headers.SupportedVersion;
            if (SubProtocols.Count > 0)
            {
                request.Headers[Constants.Headers.SecWebSocketProtocol] = string.Join(", ", SubProtocols);
            }

            if (ConfigureRequest != null)
            {
                ConfigureRequest(request);
            }

            HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();

            cancellation.Dispose();

            if (InspectResponse != null)
            {
                InspectResponse(response);
            }

            // TODO: Validate handshake
            HttpStatusCode statusCode = response.StatusCode;

            if (statusCode != HttpStatusCode.SwitchingProtocols)
            {
                response.Dispose();
                throw new InvalidOperationException("Incomplete handshake, invalid status code: " + statusCode);
            }
            // TODO: Validate Sec-WebSocket-Key/Sec-WebSocket-Accept

            string subProtocol = response.Headers[Constants.Headers.SecWebSocketProtocol];

            if (!string.IsNullOrEmpty(subProtocol) && !SubProtocols.Contains(subProtocol, StringComparer.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("Incomplete handshake, the server specified an unknown sub-protocol: " + subProtocol);
            }

            Stream stream = response.GetResponseStream();

            return(CommonWebSocket.CreateClientWebSocket(stream, subProtocol, KeepAliveInterval, ReceiveBufferSize, useZeroMask: UseZeroMask));
        }