use of org.springframework.web.socket.adapter.jetty.JettyWebSocketSession in project spring-framework by spring-projects.
the class JettyRequestUpgradeStrategy method upgrade.
@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, @Nullable String selectedProtocol, List<WebSocketExtension> selectedExtensions, @Nullable Principal user, WebSocketHandler handler, Map<String, Object> attributes) throws HandshakeFailureException {
Assert.isInstanceOf(ServletServerHttpRequest.class, request, "ServletServerHttpRequest required");
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
ServletContext servletContext = servletRequest.getServletContext();
Assert.isInstanceOf(ServletServerHttpResponse.class, response, "ServletServerHttpResponse required");
HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse();
JettyWebSocketSession session = new JettyWebSocketSession(attributes, user);
JettyWebSocketHandlerAdapter handlerAdapter = new JettyWebSocketHandlerAdapter(handler, session);
JettyWebSocketCreator webSocketCreator = (upgradeRequest, upgradeResponse) -> {
if (selectedProtocol != null) {
upgradeResponse.setAcceptedSubProtocol(selectedProtocol);
}
return handlerAdapter;
};
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(servletContext);
try {
container.upgrade(webSocketCreator, servletRequest, servletResponse);
} catch (UndeclaredThrowableException ex) {
throw new HandshakeFailureException("Failed to upgrade", ex.getUndeclaredThrowable());
} catch (Exception ex) {
throw new HandshakeFailureException("Failed to upgrade", ex);
}
}
use of org.springframework.web.socket.adapter.jetty.JettyWebSocketSession in project ma-modules-public by infiniteautomation.
the class MangoV2WebSocketHandler method sendStringMessage.
/**
* TODO Mango 3.4 remove this and use sendStringMessageAsync() instead.
* @param session
* @param message
* @throws Exception
*/
protected void sendStringMessage(WebSocketSession session, String message) {
// WebSocketSession.sendMessage() is blocking and will throw exceptions, use aysnc RemoteEndpoint.sendXXXByFuture() methods instead
JettyWebSocketSession jettySession = (JettyWebSocketSession) session;
jettySession.getNativeSession().getRemote().sendStringByFuture(message);
}
use of org.springframework.web.socket.adapter.jetty.JettyWebSocketSession in project ma-core-public by infiniteautomation.
the class MangoWebSocketPublisher method sendStringMessageAsync.
/**
* WebSocketSession.sendMessage() is blocking and will throw exceptions on concurrent sends, this method uses the aysnc RemoteEndpoint.sendStringByFuture() method instead
*
* @param session
* @param message
* @throws IOException
*/
protected void sendStringMessageAsync(WebSocketSession session, String message) {
JettyWebSocketSession jettySession = (JettyWebSocketSession) session;
jettySession.getNativeSession().getRemote().sendStringByFuture(message);
}
use of org.springframework.web.socket.adapter.jetty.JettyWebSocketSession in project spring-framework by spring-projects.
the class JettyWebSocketClient method doHandshakeInternal.
@Override
public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
final ClientUpgradeRequest request = new ClientUpgradeRequest();
request.setSubProtocols(protocols);
for (WebSocketExtension extension : extensions) {
request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(extension));
}
request.setHeaders(headers);
Principal user = getUser();
JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user);
Callable<WebSocketSession> connectTask = () -> {
JettyWebSocketHandlerAdapter adapter = new JettyWebSocketHandlerAdapter(wsHandler, wsSession);
Future<Session> future = this.client.connect(adapter, uri, request);
future.get(this.client.getConnectTimeout() + 2000, TimeUnit.MILLISECONDS);
return wsSession;
};
if (this.taskExecutor != null) {
return this.taskExecutor.submitListenable(connectTask);
} else {
ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
task.run();
return task;
}
}
Aggregations