Esempio n. 1
0
        private static async Task <WebSocket> AcceptWebSocketAsyncCore(
            RequestContext context,
            string subProtocol,
            int receiveBufferSize,
            TimeSpan keepAliveInterval,
            ArraySegment <byte> internalBuffer)
        {
            ValidateWebSocketRequest(context);

            var  subProtocols = context.Request.Headers.GetValues(HttpKnownHeaderNames.SecWebSocketProtocol);
            bool shouldSendSecWebSocketProtocolHeader = WebSocketHelpers.ProcessWebSocketProtocolHeader(subProtocols, subProtocol);

            if (shouldSendSecWebSocketProtocolHeader)
            {
                context.Response.Headers[HttpKnownHeaderNames.SecWebSocketProtocol] = subProtocol;
            }

            // negotiate the websocket key return value
            string secWebSocketKey    = context.Request.Headers[HttpKnownHeaderNames.SecWebSocketKey];
            string secWebSocketAccept = WebSocketHelpers.GetSecWebSocketAcceptString(secWebSocketKey);

            context.Response.Headers.Append(HttpKnownHeaderNames.Connection, HttpKnownHeaderNames.Upgrade);
            context.Response.Headers.Append(HttpKnownHeaderNames.Upgrade, WebSocketHelpers.WebSocketUpgradeToken);
            context.Response.Headers.Append(HttpKnownHeaderNames.SecWebSocketAccept, secWebSocketAccept);

            Stream opaqueStream = await context.UpgradeAsync();

            return(WebSocketHelpers.CreateServerWebSocket(
                       opaqueStream,
                       subProtocol,
                       receiveBufferSize,
                       keepAliveInterval,
                       internalBuffer));
        }
Esempio n. 2
0
        public Task Connect()
        {
            int ret = WebSocketConnect(this.instanceId);

            if (ret < 0)
            {
                throw WebSocketHelpers.GetErrorMessageFromCode(ret, null);
            }

            return(Task.CompletedTask);
        }
Esempio n. 3
0
 public Task <HttpListenerWebSocketContext> AcceptWebSocketAsync(string subProtocol,
                                                                 int receiveBufferSize,
                                                                 TimeSpan keepAliveInterval,
                                                                 ArraySegment <byte> internalBuffer)
 {
     return(WebSocketHelpers.AcceptWebSocketAsync(this,
                                                  subProtocol,
                                                  receiveBufferSize,
                                                  keepAliveInterval,
                                                  internalBuffer));
 }
Esempio n. 4
0
        public Task SendText(string message)
        {
            int ret = WebSocketSendText(this.instanceId, message);

            if (ret < 0)
            {
                throw WebSocketHelpers.GetErrorMessageFromCode(ret, null);
            }

            return(Task.CompletedTask);
        }
Esempio n. 5
0
        public Task Send(byte[] data)
        {
            int ret = WebSocketSend(this.instanceId, data, data.Length);

            if (ret < 0)
            {
                throw WebSocketHelpers.GetErrorMessageFromCode(ret, null);
            }

            return(Task.CompletedTask);
        }
Esempio n. 6
0
        public Task Close(WebSocketCloseCode code = WebSocketCloseCode.Normal, string reason = null)
        {
            int ret = WebSocketClose(this.instanceId, (int)code, reason);

            if (ret < 0)
            {
                throw WebSocketHelpers.GetErrorMessageFromCode(ret, null);
            }

            return(Task.CompletedTask);
        }
Esempio n. 7
0
        public Task <HttpListenerWebSocketContext> AcceptWebSocketAsync(string subProtocol,
                                                                        int receiveBufferSize,
                                                                        TimeSpan keepAliveInterval)
        {
            WebSocketHelpers.ValidateOptions(subProtocol, receiveBufferSize, WebSocketBuffer.MinSendBufferSize, keepAliveInterval);

            ArraySegment <byte> internalBuffer = WebSocketBuffer.CreateInternalBufferArraySegment(receiveBufferSize, WebSocketBuffer.MinSendBufferSize, true);

            return(this.AcceptWebSocketAsync(subProtocol,
                                             receiveBufferSize,
                                             keepAliveInterval,
                                             internalBuffer));
        }
Esempio n. 8
0
        public static bool IsWebSocketRequest(this RequestContext context)
        {
            if (!WebSocketHelpers.AreWebSocketsSupported)
            {
                return(false);
            }

            if (!context.IsUpgradableRequest)
            {
                return(false);
            }

            if (!string.Equals("GET", context.Request.Method, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // Connection: Upgrade (some odd clients send Upgrade,KeepAlive)
            string connection = context.Request.Headers[HttpKnownHeaderNames.Connection];

            if (connection == null || connection.IndexOf(HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase) < 0)
            {
                return(false);
            }

            // Upgrade: websocket
            string upgrade = context.Request.Headers[HttpKnownHeaderNames.Upgrade];

            if (!string.Equals(WebSocketHelpers.WebSocketUpgradeToken, upgrade, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // Sec-WebSocket-Version: 13
            string version = context.Request.Headers[HttpKnownHeaderNames.SecWebSocketVersion];

            if (!string.Equals(WebSocketConstants.SupportedProtocolVersion, version, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // Sec-WebSocket-Key: {base64string}
            string key = context.Request.Headers[HttpKnownHeaderNames.SecWebSocketKey];

            if (!WebSocketHelpers.IsValidWebSocketKey(key))
            {
                return(false);
            }

            return(true);
        }
    private void OnLeaveRoom(int code)
    {
        WebSocketCloseCode closeCode = WebSocketHelpers.ParseCloseCodeEnum(code);

        LSLog.Log(string.Format("ROOM: ON LEAVE =- Reason: {0} ({1})", closeCode, code));
        _pingThread.Abort();
        _pingThread = null;
        _room       = null;

        if (closeCode != WebSocketCloseCode.Normal && !string.IsNullOrEmpty(_lastRoomId))
        {
            JoinRoomId(_lastRoomId);
        }
    }
Esempio n. 10
0
        // Compare IsWebSocketRequest()
        private static void ValidateWebSocketRequest(RequestContext context)
        {
            if (!WebSocketHelpers.AreWebSocketsSupported)
            {
                throw new NotSupportedException("WebSockets are not supported on this platform.");
            }

            if (!context.IsUpgradableRequest)
            {
                throw new InvalidOperationException("This request is not a valid upgrade request.");
            }

            if (!string.Equals("GET", context.Request.Method, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("This request is not a valid upgrade request; invalid verb: " + context.Request.Method);
            }

            // Connection: Upgrade (some odd clients send Upgrade,KeepAlive)
            string connection = context.Request.Headers[HttpKnownHeaderNames.Connection];

            if (connection == null || connection.IndexOf(HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase) < 0)
            {
                throw new InvalidOperationException("The Connection header is invalid: " + connection);
            }

            // Upgrade: websocket
            string upgrade = context.Request.Headers[HttpKnownHeaderNames.Upgrade];

            if (!string.Equals(WebSocketHelpers.WebSocketUpgradeToken, upgrade, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("The Upgrade header is invalid: " + upgrade);
            }

            // Sec-WebSocket-Version: 13
            string version = context.Request.Headers[HttpKnownHeaderNames.SecWebSocketVersion];

            if (!string.Equals(WebSocketConstants.SupportedProtocolVersion, version, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("The Sec-WebSocket-Version header is invalid or not supported: " + version);
            }

            // Sec-WebSocket-Key: {base64string}
            string key = context.Request.Headers[HttpKnownHeaderNames.SecWebSocketKey];

            if (!WebSocketHelpers.IsValidWebSocketKey(key))
            {
                throw new InvalidOperationException("The Sec-WebSocket-Key header is invalid: " + upgrade);
            }
        }
Esempio n. 11
0
        public static Task <WebSocket> AcceptWebSocketAsync(
            this RequestContext context,
            string subProtocol,
            int receiveBufferSize,
            TimeSpan keepAliveInterval,
            ArraySegment <byte> internalBuffer)
        {
            if (!context.IsUpgradableRequest)
            {
                throw new InvalidOperationException("This request is cannot be upgraded.");
            }
            WebSocketHelpers.ValidateOptions(subProtocol, receiveBufferSize, WebSocketBuffer.MinSendBufferSize, keepAliveInterval);
            WebSocketHelpers.ValidateArraySegment <byte>(internalBuffer, "internalBuffer");
            WebSocketBuffer.Validate(internalBuffer.Count, receiveBufferSize, WebSocketBuffer.MinSendBufferSize, true);

            return(AcceptWebSocketAsyncCore(context, subProtocol, receiveBufferSize, keepAliveInterval, internalBuffer));
        }
Esempio n. 12
0
 public void DelegateOnCloseEvent(int closeCode)
 {
     this.OnClose?.Invoke(WebSocketHelpers.ParseCloseCodeEnum(closeCode));
 }