protected override async void OnClientConnected( IWebSocketContext context, System.Net.IPEndPoint localEndPoint, System.Net.IPEndPoint remoteEndPoint) { context.RequestRegexUrlParams("/ws/{token}").TryGetValue("token", out object token); bool valid = false; try { valid = await JWTService.Validate(token.ToString(), this.web.secret); } catch (Exception e) { this.api.Server.Logger.Warning(e.Message); } if (valid) { var response = new IServerAPIToJSON(api.Server); var msg = new WSMessage("Notification", "Websocket server started."); Send(context, msg.ToJSON()); return; } var err = new WSMessage("Error", "Unauthorized"); Send(context, err.ToJSON()); await context.WebSocket.CloseAsync(); }
public async Task <bool> GetServer() { if (this.HasRequestHeader("_auth")) { bool valid = false; valid = await JWTService.Validate(this.RequestHeader("_auth"), this.secret); if (valid) { var response = new IServerAPIToJSON(api.Server); return(await this.JsonResponseAsync(JsonConvert.SerializeObject(response))); } } return(await this.JsonResponseAsync("{\"error\": \"Unauthorized\", \"code\": 3}")); // Try setting status to ok to clear up warnings }