Skip to content

Commit

Permalink
Maintain sockjs session while websocket handshake failed
Browse files Browse the repository at this point in the history
  • Loading branch information
yfei-z committed Aug 17, 2020
1 parent 1891f8a commit c31e530
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
Expand Up @@ -60,10 +60,11 @@ public interface TransportHandler {
* @param response the current response
* @param handler the target WebSocketHandler (never {@code null})
* @param session the SockJS session (never {@code null})
* @return whether the given session is still valid
* @throws SockJsException raised when request processing fails as
* explained in {@link SockJsService}
*/
void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
boolean handleRequest(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler handler, SockJsSession session) throws SockJsException;

}
Expand Up @@ -310,7 +310,9 @@ else if (transportType.supportsCors()) {
return;
}

transportHandler.handleRequest(request, response, handler, session);
if (!transportHandler.handleRequest(request, response, handler, session)) {
sessions.remove(sessionId);
}
chain.applyAfterHandshake(request, response, null);
}
catch (SockJsException ex) {
Expand Down
Expand Up @@ -45,13 +45,14 @@ public boolean checkSessionType(SockJsSession session) {
}

@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
public final boolean handleRequest(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

Assert.notNull(wsSession, "No session");
AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;

handleRequestInternal(request, response, wsHandler, sockJsSession);
return true;
}

protected void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
Expand Down
Expand Up @@ -52,7 +52,7 @@ public abstract class AbstractHttpSendingTransportHandler extends AbstractTransp


@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
public final boolean handleRequest(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;
Expand All @@ -64,6 +64,7 @@ public final void handleRequest(ServerHttpRequest request, ServerHttpResponse re
response.getHeaders().setContentType(getContentType());

handleRequestInternal(request, response, sockJsSession);
return true;
}

protected void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
Expand Down
Expand Up @@ -116,13 +116,13 @@ public AbstractSockJsSession createSession(String id, WebSocketHandler handler,
}

@Override
public void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
public boolean handleRequest(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession;
try {
wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession);
this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes());
return this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes());
}
catch (Exception ex) {
sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
Expand Down

0 comments on commit c31e530

Please sign in to comment.