use of org.springframework.web.socket.WebSocketSession in project spring-framework by spring-projects.
the class StandardWebSocketClient method doHandshakeInternal.
@Override
protected ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler webSocketHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) {
int port = getPort(uri);
InetSocketAddress localAddress = new InetSocketAddress(getLocalHost(), port);
InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), port);
final StandardWebSocketSession session = new StandardWebSocketSession(headers, attributes, localAddress, remoteAddress);
final ClientEndpointConfig endpointConfig = ClientEndpointConfig.Builder.create().configurator(new StandardWebSocketClientConfigurator(headers)).preferredSubprotocols(protocols).extensions(adaptExtensions(extensions)).build();
endpointConfig.getUserProperties().putAll(getUserProperties());
final Endpoint endpoint = new StandardWebSocketHandlerAdapter(webSocketHandler, session);
Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() {
@Override
public WebSocketSession call() throws Exception {
webSocketContainer.connectToServer(endpoint, endpointConfig, uri);
return session;
}
};
if (this.taskExecutor != null) {
return this.taskExecutor.submitListenable(connectTask);
} else {
ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
task.run();
return task;
}
}
use of org.springframework.web.socket.WebSocketSession in project spring-framework by spring-projects.
the class AbstractSockJsIntegrationTests method testEcho.
private void testEcho(int messageCount, Transport transport, WebSocketHttpHeaders headers) throws Exception {
List<TextMessage> messages = new ArrayList<>();
for (int i = 0; i < messageCount; i++) {
messages.add(new TextMessage("m" + i));
}
TestClientHandler handler = new TestClientHandler();
initSockJsClient(transport);
URI url = new URI(this.baseUrl + "/echo");
WebSocketSession session = this.sockJsClient.doHandshake(handler, headers, url).get();
for (TextMessage message : messages) {
session.sendMessage(message);
}
handler.awaitMessageCount(messageCount, 5000);
for (TextMessage message : messages) {
assertTrue("Message not received: " + message, handler.receivedMessages.remove(message));
}
assertEquals("Remaining messages: " + handler.receivedMessages, 0, handler.receivedMessages.size());
session.close();
}
use of org.springframework.web.socket.WebSocketSession 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 e : extensions) {
request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(e));
}
for (String header : headers.keySet()) {
request.setHeader(header, headers.get(header));
}
Principal user = getUser();
final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user);
final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession);
Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() {
@Override
public WebSocketSession call() throws Exception {
Future<Session> future = client.connect(listener, uri, request);
future.get();
return wsSession;
}
};
if (this.taskExecutor != null) {
return this.taskExecutor.submitListenable(connectTask);
} else {
ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
task.run();
return task;
}
}
use of org.springframework.web.socket.WebSocketSession in project spring-framework by spring-projects.
the class StandardWebSocketClientTests method testGetLocalAddress.
@Test
public void testGetLocalAddress() throws Exception {
URI uri = new URI("ws://localhost/abc");
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
assertNotNull(session.getLocalAddress());
assertEquals(80, session.getLocalAddress().getPort());
}
use of org.springframework.web.socket.WebSocketSession in project spring-framework by spring-projects.
the class StandardWebSocketClientTests method taskExecutor.
@Test
public void taskExecutor() throws Exception {
URI uri = new URI("ws://localhost/abc");
this.wsClient.setTaskExecutor(new SimpleAsyncTaskExecutor());
WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
assertNotNull(session);
}
Aggregations