org.springframework.web.socket.adapter.standard.StandardWebSocketSession Java Examples

The following examples show how to use org.springframework.web.socket.adapter.standard.StandardWebSocketSession. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractStandardUpgradeStrategy.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response,
		String selectedProtocol, List<WebSocketExtension> selectedExtensions, Principal user,
		WebSocketHandler wsHandler, Map<String, Object> attrs) throws HandshakeFailureException {

	HttpHeaders headers = request.getHeaders();
	InetSocketAddress localAddr = request.getLocalAddress();
	InetSocketAddress remoteAddr = request.getRemoteAddress();

	StandardWebSocketSession session = new StandardWebSocketSession(headers, attrs, localAddr, remoteAddr, user);
	StandardWebSocketHandlerAdapter endpoint = new StandardWebSocketHandlerAdapter(wsHandler, session);

	List<Extension> extensions = new ArrayList<Extension>();
	for (WebSocketExtension extension : selectedExtensions) {
		extensions.add(new WebSocketToStandardExtensionAdapter(extension));
	}

	upgradeInternal(request, response, selectedProtocol, extensions, endpoint);
}
 
Example #2
Source File: StandardWebSocketClient.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 = () -> {
		this.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;
	}
}
 
Example #3
Source File: StandardWebSocketClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 = () -> {
		this.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;
	}
}
 
Example #4
Source File: StandardWebSocketClient.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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<WebSocketSession>(connectTask);
		task.run();
		return task;
	}
}