Beispiel #1
0
        /// <summary>
        /// Accept network stream
        /// </summary>
        /// <param name="stream">Network stream</param>
        /// <returns>Finished task</returns>
        private async Task AcceptStream(Stream stream)
        {
            NWS.WebSocketHttpContext context = await this.socketServerFactory.ReadHttpHeaderFromStreamAsync(stream);

            if (context.IsWebSocketRequest)
            {
                string protocol = this.GetSubProtocol(context.WebSocketRequestedProtocols);

                NWS.WebSocketServerOptions options = new NWS.WebSocketServerOptions()
                {
                    KeepAliveInterval = this.KeepAliveInterval,
                    SubProtocol       = protocol
                };

                WebSocket webSocket = await this.socketServerFactory.AcceptWebSocketAsync(context, options);

                this.OnConnection?.Invoke(this, webSocket);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Accept web socket with options specified
        /// Call ReadHttpHeaderFromStreamAsync first to get WebSocketHttpContext
        /// </summary>
        /// <param name="context">The http context used to initiate this web socket request</param>
        /// <param name="options">The web socket options</param>
        /// <param name="token">The optional cancellation token</param>
        /// <returns>A connected web socket</returns>
        public async Task <WebSocket> AcceptWebSocketAsync(WebSocketHttpContext context, WebSocketServerOptions options, CancellationToken token = default(CancellationToken))
        {
            Guid guid = Guid.NewGuid();

            Events.Log.AcceptWebSocketStarted(guid);
            await PerformHandshakeAsync(guid, context.HttpHeader, options.SubProtocol, context.Stream, token);

            Events.Log.ServerHandshakeSuccess(guid);
            string secWebSocketExtensions = null;

            return(new WebSocketImplementation(guid, _bufferFactory, context.Stream, options.KeepAliveInterval, secWebSocketExtensions, options.IncludeExceptionInCloseResponse, false, options.SubProtocol));
        }