protected SockJSSession getSession(RoutingContext rc, long timeout, long heartbeatInterval, String sessionID, Handler<SockJSSocket> sockHandler) { SockJSSession session = sessions.computeIfAbsent(sessionID, s -> new SockJSSession(vertx, sessions, rc, s, timeout, heartbeatInterval, sockHandler)); return session; }
if (closed) { writeClosed(lst); writeClosed(lst, 2010, "Another connection still open"); cancelTimer(); writeOpen(lst); sockHandler.handle(this); handleCalled = true; if (closed) { writeClosed(lst); listener = null; lst.close(); } else { if (!pendingWrites.isEmpty()) { writePendingMessages();
public void sessionClosed() { session.writeClosed(this); closed = true; // Asynchronously close the websocket to fix a bug in the SockJS TCK // due to the WebSocket client that skip some frames (bug) session.context().runOnContext(v -> ws.close()); }
synchronized boolean handleMessages(String messages) { String[] msgArr = parseMessageString(messages); if (msgArr == null) { return false; } handleMessages(msgArr); return true; }
private void handleMessages(String msgs) { if (!session.isClosed()) { if (msgs.equals("") || 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(); } } }
public void close() { if (!closed) { try { session.resetListener(); rc.response().end(); rc.response().close(); } catch (IllegalStateException e) { // Underlying connection might already be closed - that's fine } closed = true; } }
private void setTimer() { if (timeout != -1) { cancelTimer(); timeoutTimerID = vertx.setTimer(timeout, id1 -> { vertx.cancelTimer(heartbeatID); if (listener == null) { shutdown(); } if (listener != null) { listener.close(); } }); } }
WebSocketListener(ServerWebSocket ws, SockJSSession session) { this.ws = ws; this.session = session; ws.textMessageHandler(this::handleMessages); ws.closeHandler(v -> { closed = true; session.shutdown(); }); ws.exceptionHandler(t -> { closed = true; session.shutdown(); session.handleException(t); }); }
WebSocketTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options, Handler<SockJSSocket> sockHandler) { super(vertx, sessions, options); String wsRE = COMMON_PATH_ELEMENT_RE + "websocket"; router.getWithRegex(wsRE).handler(rc -> { HttpServerRequest req = rc.request(); String connectionHeader = req.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION); if (connectionHeader == null || !connectionHeader.toLowerCase().contains("upgrade")) { rc.response().setStatusCode(400); rc.response().end("Can \"Upgrade\" only to \"WebSocket\"."); } else { ServerWebSocket ws = rc.request().upgrade(); if (log.isTraceEnabled()) log.trace("WS, handler"); SockJSSession session = new SockJSSession(vertx, sessions, rc, options.getHeartbeatInterval(), sockHandler); session.register(req, new WebSocketListener(ws, session)); } }); router.getWithRegex(wsRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("WS, get: " + rc.request().uri()); rc.response().setStatusCode(400); rc.response().end("Can \"Upgrade\" only to \"WebSocket\"."); }); router.routeWithRegex(wsRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("WS, all: " + rc.request().uri()); rc.response().putHeader("Allow", "GET").setStatusCode(405).end(); }); }
String sessionID = req.params().get("param0"); SockJSSession session = getSession(rc, options.getSessionTimeout(), options.getHeartbeatInterval(), sessionID, sockHandler); session.register(req, new JsonPListener(rc, session, callback)); }); String sessionID = rc.request().getParam("param0"); final SockJSSession session = sessions.get(sessionID); if (session != null && !session.isClosed()) { handleSend(rc, session); } else {
HtmlFileTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options, Handler<SockJSSocket> sockHandler) { super(vertx, sessions, options); String htmlFileRE = COMMON_PATH_ELEMENT_RE + "htmlfile.*"; router.getWithRegex(htmlFileRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("HtmlFile, get: " + rc.request().uri()); String callback = rc.request().getParam("callback"); if (callback == null) { callback = rc.request().getParam("c"); if (callback == null) { rc.response().setStatusCode(500).end("\"callback\" parameter required\n"); return; } } if (CALLBACK_VALIDATION.matcher(callback).find()) { rc.response().setStatusCode(500); rc.response().end("invalid \"callback\" parameter\n"); return; } HttpServerRequest req = rc.request(); String sessionID = req.params().get("param0"); SockJSSession session = getSession(rc, options.getSessionTimeout(), options.getHeartbeatInterval(), sessionID, sockHandler); session.register(req, new HtmlFileListener(options.getMaxBytesStreaming(), rc, callback, session)); }); }
private synchronized void handleMessages(String[] messages) { if (context == Vertx.currentContext()) { for (String msg : messages) { pendingReads.write(buffer(msg)); } } else { context.runOnContext(v -> { handleMessages(messages); }); } }
@Override public void close() { Handler<Void> eh; synchronized (this) { eh = endHandler; closed = true; doClose(); } if (eh != null) { eh.handle(null); } }
XhrTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options, Handler<SockJSSocket> sockHandler) { super(vertx, sessions, options); String xhrBase = COMMON_PATH_ELEMENT_RE; String xhrRE = xhrBase + "xhr"; String xhrStreamRE = xhrBase + "xhr_streaming"; Handler<RoutingContext> xhrOptionsHandler = createCORSOptionsHandler(options, "OPTIONS, POST"); router.optionsWithRegex(xhrRE).handler(xhrOptionsHandler); router.optionsWithRegex(xhrStreamRE).handler(xhrOptionsHandler); registerHandler(router, sockHandler, xhrRE, false, options); registerHandler(router, sockHandler, xhrStreamRE, true, options); String xhrSendRE = COMMON_PATH_ELEMENT_RE + "xhr_send"; router.optionsWithRegex(xhrSendRE).handler(xhrOptionsHandler); router.postWithRegex(xhrSendRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("XHR send, post, " + rc.request().uri()); String sessionID = rc.request().getParam("param0"); final SockJSSession session = sessions.get(sessionID); if (session != null && !session.isClosed()) { handleSend(rc, session); } else { rc.response().setStatusCode(404); setJSESSIONID(options, rc); rc.response().end(); } }); }
public void close() { if (!closed) { try { session.resetListener(); rc.response().end(); rc.response().close(); closed = true; } catch (IllegalStateException e) { // Underlying connection might already be closed - that's fine } } } }
synchronized boolean handleMessages(String messages) { String[] msgArr = parseMessageString(messages); if (msgArr == null) { return false; } handleMessages(msgArr); return true; }
private void handleMessages(String msgs) { if (!session.isClosed()) { if (msgs.equals("") || 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(); } } }
private void setTimer() { if (timeout != -1) { cancelTimer(); timeoutTimerID = vertx.setTimer(timeout, id1 -> { vertx.cancelTimer(heartbeatID); if (listener == null) { shutdown(); } if (listener != null) { listener.close(); } }); } }
WebSocketListener(ServerWebSocket ws, SockJSSession session) { this.ws = ws; this.session = session; ws.textMessageHandler(this::handleMessages); ws.closeHandler(v -> { closed = true; session.shutdown(); }); ws.exceptionHandler(t -> { closed = true; session.shutdown(); session.handleException(t); }); }
WebSocketTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options, Handler<SockJSSocket> sockHandler) { super(vertx, sessions, options); String wsRE = COMMON_PATH_ELEMENT_RE + "websocket"; router.getWithRegex(wsRE).handler(rc -> { HttpServerRequest req = rc.request(); String connectionHeader = req.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION); if (connectionHeader == null || !connectionHeader.toLowerCase().contains("upgrade")) { rc.response().setStatusCode(400); rc.response().end("Can \"Upgrade\" only to \"WebSocket\"."); } else { ServerWebSocket ws = rc.request().upgrade(); if (log.isTraceEnabled()) log.trace("WS, handler"); SockJSSession session = new SockJSSession(vertx, sessions, rc, options.getHeartbeatInterval(), sockHandler); session.register(req, new WebSocketListener(ws, session)); } }); router.getWithRegex(wsRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("WS, get: " + rc.request().uri()); rc.response().setStatusCode(400); rc.response().end("Can \"Upgrade\" only to \"WebSocket\"."); }); router.routeWithRegex(wsRE).handler(rc -> { if (log.isTraceEnabled()) log.trace("WS, all: " + rc.request().uri()); rc.response().putHeader("Allow", "GET").setStatusCode(405).end(); }); }