func writeBadRequest(errString string, ws *websocket.Conn) (fatal bool) { logger.Log(errString) var out Response out.Message = errString data, err := json.Marshal(out) if err != nil { data = []byte(`{"Success":false}`) } _, err = ws.Write(data) return err != nil }
func WebsocketHandler(ws *websocket.Conn) { for { data := make([]byte, readSize) n, err := ws.Read(data) if err != nil { logger.Log("READ ERROR", err) break } data = data[:n] var in Request if err = json.Unmarshal(data, &in); err != nil { if writeBadRequest("INVALID DATA "+err.Error(), ws) { return } continue } out, err := Call(in) if err != nil { if writeBadRequest("SERVER ERROR "+err.Error(), ws) { return } continue } data, err = json.Marshal(out) if err != nil { if writeBadRequest("SERVER ERROR "+err.Error(), ws) { return } continue } _, err = ws.Write(data) if err != nil { logger.Log("WRITE ERROR", err) break } } }