/// <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); }
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(); } }
public void SendResponse() { HttpSendResponse.SendAsync(this).Wait(); }
public Task SendResponseAsync() { return(HttpSendResponse.SendAsync(this)); }