org.springframework.web.socket.WebSocketHttpHeaders.setSecWebSocketProtocol()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(221)

本文整理了Java中org.springframework.web.socket.WebSocketHttpHeaders.setSecWebSocketProtocol()方法的一些代码示例,展示了WebSocketHttpHeaders.setSecWebSocketProtocol()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebSocketHttpHeaders.setSecWebSocketProtocol()方法的具体详情如下:
包路径:org.springframework.web.socket.WebSocketHttpHeaders
类名称:WebSocketHttpHeaders
方法名:setSecWebSocketProtocol

WebSocketHttpHeaders.setSecWebSocketProtocol介绍

[英]Sets the (new) value of the Sec-WebSocket-Protocol header.
[中]设置Sec WebSocket协议头的(新)值。

代码示例

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * Set the sub-protocols to use. If configured, specified sub-protocols will be
  3. * requested in the handshake through the {@code Sec-WebSocket-Protocol} header. The
  4. * resulting WebSocket session will contain the protocol accepted by the server, if
  5. * any.
  6. */
  7. public void setSubProtocols(List<String> protocols) {
  8. this.headers.setSecWebSocketProtocol(protocols);
  9. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void clientEndpointConfig() throws Exception {
  3. URI uri = new URI("ws://localhost/abc");
  4. List<String> protocols = Collections.singletonList("abc");
  5. this.headers.setSecWebSocketProtocol(protocols);
  6. this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
  7. ArgumentCaptor<ClientEndpointConfig> captor = ArgumentCaptor.forClass(ClientEndpointConfig.class);
  8. verify(this.wsContainer).connectToServer(any(Endpoint.class), captor.capture(), any(URI.class));
  9. ClientEndpointConfig endpointConfig = captor.getValue();
  10. assertEquals(protocols, endpointConfig.getPreferredSubprotocols());
  11. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void doHandshake() throws Exception {
  3. WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
  4. headers.setSecWebSocketProtocol(Arrays.asList("echo"));
  5. this.wsSession = this.client.doHandshake(new TextWebSocketHandler(), headers, new URI(this.wsUrl)).get();
  6. assertEquals(this.wsUrl, this.wsSession.getUri().toString());
  7. assertEquals("echo", this.wsSession.getAcceptedProtocol());
  8. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void doHandshakeWithTaskExecutor() throws Exception {
  3. WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
  4. headers.setSecWebSocketProtocol(Arrays.asList("echo"));
  5. this.client.setTaskExecutor(new SimpleAsyncTaskExecutor());
  6. this.wsSession = this.client.doHandshake(new TextWebSocketHandler(), headers, new URI(this.wsUrl)).get();
  7. assertEquals(this.wsUrl, this.wsSession.getUri().toString());
  8. assertEquals("echo", this.wsSession.getAcceptedProtocol());
  9. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void handshakeHeaders() throws Exception {
  3. URI uri = new URI("ws://localhost/abc");
  4. List<String> protocols = Collections.singletonList("abc");
  5. this.headers.setSecWebSocketProtocol(protocols);
  6. this.headers.add("foo", "bar");
  7. WebSocketSession session = this.wsClient.doHandshake(this.wsHandler, this.headers, uri).get();
  8. assertEquals(1, session.getHandshakeHeaders().size());
  9. assertEquals("bar", session.getHandshakeHeaders().getFirst("foo"));
  10. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void openConnection() throws Exception {
  3. List<String> subprotocols = Arrays.asList("abc");
  4. TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
  5. WebSocketHandler handler = new TextWebSocketHandler();
  6. WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler , "/path/{id}", "123");
  7. manager.setSubProtocols(subprotocols);
  8. manager.openConnection();
  9. WebSocketHttpHeaders expectedHeaders = new WebSocketHttpHeaders();
  10. expectedHeaders.setSecWebSocketProtocol(subprotocols);
  11. assertEquals(expectedHeaders, client.headers);
  12. assertEquals(new URI("/path/123"), client.uri);
  13. WebSocketHandlerDecorator loggingHandler = (WebSocketHandlerDecorator) client.webSocketHandler;
  14. assertEquals(LoggingWebSocketHandlerDecorator.class, loggingHandler.getClass());
  15. assertSame(handler, loggingHandler.getDelegate());
  16. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void subProtocolNegotiation() throws Exception {
  3. WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
  4. headers.setSecWebSocketProtocol("foo");
  5. URI url = new URI(getWsBaseUrl() + "/ws");
  6. WebSocketSession session = this.webSocketClient.doHandshake(new TextWebSocketHandler(), headers, url).get();
  7. assertEquals("foo", session.getAcceptedProtocol());
  8. session.close();
  9. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void subProtocolCapableHandlerNoMatch() {
  3. given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
  4. this.servletRequest.setMethod("GET");
  5. WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
  6. headers.setUpgrade("WebSocket");
  7. headers.setConnection("Upgrade");
  8. headers.setSecWebSocketVersion("13");
  9. headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
  10. headers.setSecWebSocketProtocol("v10.stomp");
  11. WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
  12. Map<String, Object> attributes = Collections.<String, Object>emptyMap();
  13. this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
  14. verify(this.upgradeStrategy).upgrade(this.request, this.response, null,
  15. Collections.emptyList(), null, handler, attributes);
  16. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void subProtocolCapableHandler() {
  3. given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
  4. this.servletRequest.setMethod("GET");
  5. WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
  6. headers.setUpgrade("WebSocket");
  7. headers.setConnection("Upgrade");
  8. headers.setSecWebSocketVersion("13");
  9. headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
  10. headers.setSecWebSocketProtocol("v11.stomp");
  11. WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
  12. Map<String, Object> attributes = Collections.<String, Object>emptyMap();
  13. this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
  14. verify(this.upgradeStrategy).upgrade(this.request, this.response, "v11.stomp",
  15. Collections.emptyList(), null, handler, attributes);
  16. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void supportedSubProtocols() {
  3. this.handshakeHandler.setSupportedProtocols("stomp", "mqtt");
  4. given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
  5. this.servletRequest.setMethod("GET");
  6. WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
  7. headers.setUpgrade("WebSocket");
  8. headers.setConnection("Upgrade");
  9. headers.setSecWebSocketVersion("13");
  10. headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
  11. headers.setSecWebSocketProtocol("STOMP");
  12. WebSocketHandler handler = new TextWebSocketHandler();
  13. Map<String, Object> attributes = Collections.emptyMap();
  14. this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
  15. verify(this.upgradeStrategy).upgrade(this.request, this.response, "STOMP",
  16. Collections.emptyList(), null, handler, attributes);
  17. }

代码示例来源:origin: spring-projects/spring-integration

  1. @Override
  2. protected void openConnection() {
  3. logger.info("Connecting to WebSocket at " + getUri());
  4. ClientWebSocketContainer.this.headers.setSecWebSocketProtocol(getSubProtocols());
  5. ListenableFuture<WebSocketSession> future =
  6. this.client.doHandshake(ClientWebSocketContainer.this.webSocketHandler,
  7. ClientWebSocketContainer.this.headers, getUri());
  8. future.addCallback(new ListenableFutureCallback<WebSocketSession>() {
  9. @Override
  10. public void onSuccess(WebSocketSession session) {
  11. ClientWebSocketContainer.this.clientSession = session;
  12. logger.info("Successfully connected");
  13. ClientWebSocketContainer.this.connectionLatch.countDown();
  14. }
  15. @Override
  16. public void onFailure(Throwable t) {
  17. logger.error("Failed to connect", t);
  18. ClientWebSocketContainer.this.openConnectionException = t;
  19. ClientWebSocketContainer.this.connectionLatch.countDown();
  20. }
  21. });
  22. }

代码示例来源:origin: org.springframework/spring-websocket

  1. /**
  2. * Set the sub-protocols to use. If configured, specified sub-protocols will be
  3. * requested in the handshake through the {@code Sec-WebSocket-Protocol} header. The
  4. * resulting WebSocket session will contain the protocol accepted by the server, if
  5. * any.
  6. */
  7. public void setSubProtocols(List<String> protocols) {
  8. this.headers.setSecWebSocketProtocol(protocols);
  9. }

代码示例来源:origin: apache/servicemix-bundles

  1. /**
  2. * Set the sub-protocols to use. If configured, specified sub-protocols will be
  3. * requested in the handshake through the {@code Sec-WebSocket-Protocol} header. The
  4. * resulting WebSocket session will contain the protocol accepted by the server, if
  5. * any.
  6. */
  7. public void setSubProtocols(List<String> protocols) {
  8. this.headers.setSecWebSocketProtocol(protocols);
  9. }

代码示例来源:origin: org.springframework.integration/spring-integration-websocket

  1. @Override
  2. protected void openConnection() {
  3. logger.info("Connecting to WebSocket at " + getUri());
  4. ClientWebSocketContainer.this.headers.setSecWebSocketProtocol(getSubProtocols());
  5. ListenableFuture<WebSocketSession> future =
  6. this.client.doHandshake(ClientWebSocketContainer.this.webSocketHandler,
  7. ClientWebSocketContainer.this.headers, getUri());
  8. future.addCallback(new ListenableFutureCallback<WebSocketSession>() {
  9. @Override
  10. public void onSuccess(WebSocketSession session) {
  11. ClientWebSocketContainer.this.clientSession = session;
  12. logger.info("Successfully connected");
  13. ClientWebSocketContainer.this.connectionLatch.countDown();
  14. }
  15. @Override
  16. public void onFailure(Throwable t) {
  17. logger.error("Failed to connect", t);
  18. ClientWebSocketContainer.this.openConnectionException = t;
  19. ClientWebSocketContainer.this.connectionLatch.countDown();
  20. }
  21. });
  22. }

相关文章