public async Task ProcessRequestAsync(HttpContext context) { string[] parts = context.Request.Path.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 1) { string channel = parts[0]; if (context.Request.IsWebSocketRequest) { // accept the web socket and process it await context.AcceptWebSocketRequestAsync(new MonitorClient(this, channel)); } else { if ((context.Request.Method == "POST") && (parts.Length == 1)) { JsonValue json = await context.Request.ReadAsJsonAsync(); if (SendMessage(channel, json)) { context.Response.StatusCode = 200; } else { context.Response.StatusCode = 404; } } } } }
public async Task ProcessRequestAsync(HttpContext context) { if (context.Request.IsWebSocketRequest) { // accept the web socket and process it await context.AcceptWebSocketRequestAsync(new EchoClient()); } }
//public WebSocketServerChannel(HttpListenerWebSocketContext context, WebSocketConfig config, CancellationToken token) //{ // Id = "ws-" + Guid.NewGuid().ToString(); // this.token = token; // this.IsEncrypted = context.RequestUri.Scheme == "wss"; // this.IsAuthenticated = HttpHelper.HttpContext.User.Identity.IsAuthenticated; // this.handler = new WebSocketHandler(config, token); // this.handler.OnReceive += Handler_OnReceive; // this.handler.OnError += Handler_OnError; // this.handler.OnOpen += Handler_OnOpen; // this.handler.OnClose += Handler_OnClose; //} public WebSocketServerChannel(HttpContext context, WebSocketConfig config, CancellationToken token) { Id = "ws-" + Guid.NewGuid().ToString(); this.config = config; this.token = token; this.IsEncrypted = context.Request.Scheme == "wss"; this.IsAuthenticated = context.User.Identity.IsAuthenticated; this.handler = new WebSocketHandler(config, token); this.handler.OnReceive += Handler_OnReceive; this.handler.OnError += Handler_OnError; this.handler.OnOpen += Handler_OnOpen; this.handler.OnClose += Handler_OnClose; Task task = Task.Factory.StartNew(async() => { await Task.Delay(100); this.socket = await context.AcceptWebSocketRequestAsync(this.handler); }); Task.WhenAll(task); }
public override async Task ProcessRequestAsync(HttpContext context) { string[] parts = context.Request.Path.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries); long id = 0; // WS /[id] monitor a user messages if ((context.Request.IsWebSocketRequest) && (parts.Length == 1)) { Rights.EnsureCanMonitorUser(context, parts[0]); string user = parts[0]; MessageClient client = new MessageClient(this, user); await context.AcceptWebSocketRequestAsync(client); // } // GET /[id] get a message else if ((context.Request.Method == "GET") && (parts.Length == 1) && (long.TryParse(parts[0], out id))) { JsonValue json = GetMessage(id); context.Response.Headers["cache-control"] = "no-cache, must-revalidate"; if (json == null) { context.Response.StatusCode = 404; } else { Rights.EnsureCanReadMessage(context, json["origin"], json["destination"]); context.Response.StatusCode = 200; context.Response.Content = new JsonContent(json); } } // GET /?user=[user_id]&with=[user_id]&limit=[limit] else if ((context.Request.Method == "GET") && (context.Request.Path == "/") && context.Request.QueryString.ContainsKey("user")) { string user = context.Request.QueryString["user"]; string with = null; if (context.Request.QueryString.ContainsKey("with")) { with = context.Request.QueryString["with"]; } Rights.EnsureCanReadMessage(context, user, with); Rights.EnsureCanReadMessage(context, with, user); int limit = 1000; if (context.Request.QueryString.ContainsKey("limit")) { limit = Convert.ToInt32(context.Request.QueryString["limit"]); } context.Response.StatusCode = 200; context.Response.Headers["cache-control"] = "no-cache, must-revalidate"; context.Response.Content = new JsonContent(SearchMessages(user, with, limit)); } // POST /?destination=[destination]&origin=[origin] // content: { destination: [destination], origin: [origin], type: [type], content: [content] } else if ((context.Request.Method == "POST") && (context.Request.Path == "/")) { JsonValue json = context.Request.ReadAsJson(); if (context.Request.QueryString.ContainsKey("destination")) { json["destination"] = context.Request.QueryString["destination"]; } if (context.Request.QueryString.ContainsKey("origin")) { json["origin"] = context.Request.QueryString["origin"]; } if (!json.ContainsKey("type")) { json["type"] = "message"; } Rights.EnsureCanCreateMessage(context, json["origin"], json["destination"]); context.Response.StatusCode = 200; context.Response.Headers["cache-control"] = "no-cache, must-revalidate"; context.Response.Content = new JsonContent(SendMessage(json)); } // PUT /[id] else if ((context.Request.Method == "PUT") && (parts.Length == 1) && (long.TryParse(parts[0], out id))) { JsonValue json = GetMessage(id); Rights.EnsureCanUpdateMessage(context, json["origin"], json["destination"]); context.Response.Headers["cache-control"] = "no-cache, must-revalidate"; json = MarkSeenMessage(id); if (json == null) { context.Response.StatusCode = 404; } else { context.Response.StatusCode = 200; context.Response.Content = new JsonContent(json); } } // DELETE /[id] delete a message else if ((context.Request.Method == "DELETE") && (parts.Length == 1) && (long.TryParse(parts[0], out id))) { JsonValue json = GetMessage(id); Rights.EnsureCanDeleteMessage(context, json["origin"], json["destination"]); context.Response.Headers["cache-control"] = "no-cache, must-revalidate"; if (DeleteMessage(id)) { context.Response.StatusCode = 200; } else { context.Response.StatusCode = 404; } } }