public Task <WebSocket> AcceptWebSocketAsync(TimeSpan keepAliveInterval)
 {
     // WebSocket protocol 10
     if (Request.Headers.ContainsKey("upgrade") && Request.Headers["upgrade"].ToLower() == "websocket")
     {
         return(WebSocket10.AcceptAsync(this, keepAliveInterval));
     }
     // polling emulated WebSocket
     else if (Request.QueryString.ContainsKey("socket") && (Request.QueryString["socket"] == "poll") &&
              Request.QueryString.ContainsKey("command") && (Request.QueryString["command"] == "open"))
     {
         return(EmuPollSocket.AcceptAsync(this, keepAliveInterval));
     }
     else
     {
         throw new Exception("Request is not a WebSocket");
     }
 }
Beispiel #2
0
        /// <summary>
        /// Takes care of the initial handshaking between the client and the server
        /// </summary>
        internal static async Task <WebSocket> AcceptAsync(HttpContext context, TimeSpan keepAliveInterval)
        {
            EmuPollSocket emuSocket = new EmuPollSocket(context, keepAliveInterval);
            JsonValue     json      = new JsonObject();

            json["status"]            = "open";
            json["id"]                = emuSocket.Id;
            json["keepAliveInterval"] = keepAliveInterval.TotalSeconds;

            context.Response.StatusCode = 200;
            context.Response.Headers["cache-control"] = "no-cache, must-revalidate";
            context.Response.Headers["connection"]    = "close";
            context.Response.Content = new JsonContent(json);
            await HttpSendResponse.SendAsync(context);

            context.Client.Close();
            context.WebSocket = emuSocket;
            return(context.WebSocket);
        }
Beispiel #3
0
        public async Task ProcessAsync()
        {
            try {
                while (KeepAliveCountdown >= 0)
                {
                    HttpServerRequest request;
                    try {
                        request = await ReadRequestAsync();
                    }
                    catch (IOException) {
                        break;
                    }
                    if (request == null)
                    {
                        break;
                    }

                    try {
                        // Process the request
                        Context = new HttpContext(this, request);
                        // test if it is an emulated socket
                        EmuPollSocket emuSocket = EmuPollSocket.GetEmuPollSocket(Context);

                        if (emuSocket != null)
                        {
                            await emuSocket.ProcessRequestAsync(Context);
                        }
                        else
                        {
                            await Server.ProcessRequestAsync(Context);
                        }

                        if ((webSocket == null) || (emuSocket != null))
                        {
                            // in case nobody send the response...
                            if (!Context.Response.Sent)
                            {
                                await HttpSendResponse.SendAsync(Context);
                            }
                        }
                        // process multiple request is not possible with websocket
                        else
                        {
                            break;
                        }
                    }
                    catch (IOException) {
                        break;
                    }
                    finally {
                        // free the response content
                        if ((Context.Response != null) && (Context.Response.Content != null))
                        {
                            Context.Response.Content.Dispose();
                        }
                    }
                    Interlocked.Increment(ref requestCounter);
                }
            }
            catch (SocketException) {
            }
            finally {
                stream.Close();
            }
        }