private void sendMessage(final NanoWSD.WebSocket webSocket, final String message) { try { webSocket.send(message); } catch (IOException e) { e.printStackTrace(); } }
@Override public Response serve(final IHTTPSession session) { Map<String, String> headers = session.getHeaders(); if (isWebsocketRequested(session)) { if (!NanoWSD.HEADER_WEBSOCKET_VERSION_VALUE.equalsIgnoreCase(headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION))) { return newFixedLengthResponse(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Invalid Websocket-Version " + headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION)); } if (!headers.containsKey(NanoWSD.HEADER_WEBSOCKET_KEY)) { return newFixedLengthResponse(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Missing Websocket-Key"); } WebSocket webSocket = openWebSocket(session); Response handshakeResponse = webSocket.getHandshakeResponse(); try { handshakeResponse.addHeader(NanoWSD.HEADER_WEBSOCKET_ACCEPT, makeAcceptKey(headers.get(NanoWSD.HEADER_WEBSOCKET_KEY))); } catch (NoSuchAlgorithmException e) { return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "The SHA-1 Algorithm required for websockets is not available on the server."); } if (headers.containsKey(NanoWSD.HEADER_WEBSOCKET_PROTOCOL)) { handshakeResponse.addHeader(NanoWSD.HEADER_WEBSOCKET_PROTOCOL, headers.get(NanoWSD.HEADER_WEBSOCKET_PROTOCOL).split(",")[0]); } return handshakeResponse; } else { return serveHttp(session); } }
private void readWebsocket() { try { while (this.state == State.OPEN) { handleWebsocketFrame(WebSocketFrame.read(this.in)); } } catch (CharacterCodingException e) { onException(e); doClose(CloseCode.InvalidFramePayloadData, e.toString(), false); } catch (IOException e) { onException(e); if (e instanceof WebSocketException) { doClose(((WebSocketException) e).getCode(), ((WebSocketException) e).getReason(), false); } } finally { doClose(CloseCode.InternalServerError, "Handler terminated without closing the connection.", false); } }
private void handleWebsocketFrame(WebSocketFrame frame) throws IOException { debugFrameReceived(frame); if (frame.getOpCode() == OpCode.Close) { handleCloseFrame(frame); } else if (frame.getOpCode() == OpCode.Ping) { sendFrame(new WebSocketFrame(OpCode.Pong, true, frame.getBinaryPayload())); } else if (frame.getOpCode() == OpCode.Pong) { onPong(frame); } else if (!frame.isFin() || frame.getOpCode() == OpCode.Continuation) { handleFrameFragment(frame); } else if (this.continuousOpCode != null) { throw new WebSocketException(CloseCode.ProtocolError, "Continuous frame sequence not completed."); } else if (frame.getOpCode() == OpCode.Text || frame.getOpCode() == OpCode.Binary) { onMessage(frame); } else { throw new WebSocketException(CloseCode.ProtocolError, "Non control or continuous frame expected."); } }
private void handleCloseFrame(WebSocketFrame frame) throws IOException { CloseCode code = CloseCode.NormalClosure; String reason = ""; if (frame instanceof CloseFrame) { code = ((CloseFrame) frame).getCloseCode(); reason = ((CloseFrame) frame).getCloseReason(); } if (this.state == State.CLOSING) { // Answer for my requested close doClose(code, reason, false); } else { close(code, reason, true); } }
private void sendMessage(final NanoWSD.WebSocket webSocket, final String message) { try { webSocket.send(message); } catch (IOException e) { e.printStackTrace(); } }
public synchronized void sendFrame(WebSocketFrame frame) throws IOException { debugFrameSent(frame); frame.write(this.out); } }