WebSocketListener(final WebSocket ws, final Session session) { this.ws = ws; this.session = session; ws.dataHandler(new Handler<Buffer>() { public void handle(Buffer data) { if (!session.isClosed()) { String msgs = data.toString(); if (msgs.equals("")) { //Ignore empty frames } else if ((msgs.startsWith("[\"") && msgs.endsWith("\"]")) || (msgs.startsWith("\"") && msgs.endsWith("\""))) { session.handleMessages(msgs); } else { //Invalid JSON - we close the connection close(); } } } }); ws.closedHandler(new SimpleHandler() { public void handle() { closed = true; session.shutdown(); } }); }