本文整理匯總了Java中org.springframework.web.socket.WebSocketHandler類的典型用法代碼示例。如果您正苦於以下問題:Java WebSocketHandler類的具體用法?Java WebSocketHandler怎麽用?Java WebSocketHandler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
WebSocketHandler類屬於org.springframework.web.socket包,在下文中一共展示了WebSocketHandler類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: beforeHandshake
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
System.out.println("before handshake");
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
HttpSession session = servletRequest.getServletRequest().getSession(false);
Long uid = (Long)session.getAttribute("uid");
if(uid == null){
return false;
}
//如果uid==null,afterConnectionEstablished不執行,協議轉換不成功,報下麵的錯誤
//websocket.html?uid=1990:56 WebSocket connection to 'ws://localhost:8080/ws?uid=1990' failed: Error during WebSocket handshake: Unexpected response code: 500
attributes.put("uid", uid);
return true;
// 如果這裏返回false,瀏覽器會報下麵這個錯誤
// WebSocket connection to 'ws://localhost:8080/ws' failed: Error during WebSocket handshake: Unexpected response code: 200
}
示例2: selectProtocol
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Override
protected String selectProtocol(List<String> requestedProtocols,
WebSocketHandler webSocketHandler) {
if (requestedProtocols.contains(WampSubProtocolHandler.MSGPACK_PROTOCOL)) {
return WampSubProtocolHandler.MSGPACK_PROTOCOL;
}
if (requestedProtocols.contains(WampSubProtocolHandler.SMILE_PROTOCOL)) {
return WampSubProtocolHandler.SMILE_PROTOCOL;
}
if (requestedProtocols.contains(WampSubProtocolHandler.CBOR_PROTOCOL)) {
return WampSubProtocolHandler.CBOR_PROTOCOL;
}
return super.selectProtocol(requestedProtocols, webSocketHandler);
}
示例3: minimal
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Test
public void minimal() {
WebSocketHandler handler = new TextWebSocketHandler();
this.registration.addHandler(handler, "/foo", "/bar");
List<Mapping> mappings = this.registration.getMappings();
assertEquals(2, mappings.size());
Mapping m1 = mappings.get(0);
assertEquals(handler, m1.webSocketHandler);
assertEquals("/foo", m1.path);
assertEquals(1, m1.interceptors.length);
assertEquals(OriginHandshakeInterceptor.class, m1.interceptors[0].getClass());
Mapping m2 = mappings.get(1);
assertEquals(handler, m2.webSocketHandler);
assertEquals("/bar", m2.path);
assertEquals(1, m2.interceptors.length);
assertEquals(OriginHandshakeInterceptor.class, m2.interceptors[0].getClass());
}
示例4: handshakeHandlerPassedToSockJsRegistration
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Test
public void handshakeHandlerPassedToSockJsRegistration() {
WebSocketHandler handler = new TextWebSocketHandler();
HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();
List<Mapping> mappings = this.registration.getMappings();
assertEquals(1, mappings.size());
Mapping mapping = mappings.get(0);
assertEquals(handler, mapping.webSocketHandler);
assertEquals("/foo/**", mapping.path);
assertNotNull(mapping.sockJsService);
WebSocketTransportHandler transportHandler =
(WebSocketTransportHandler) mapping.sockJsService.getTransportHandlers().get(TransportType.WEBSOCKET);
assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
}
示例5: delegateMessageException
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Test
public void delegateMessageException() throws Exception {
StubSockJsServiceConfig sockJsConfig = new StubSockJsServiceConfig();
this.servletRequest.setContent("[\"x\"]".getBytes("UTF-8"));
WebSocketHandler wsHandler = mock(WebSocketHandler.class);
TestHttpSockJsSession session = new TestHttpSockJsSession("1", sockJsConfig, wsHandler, null);
session.delegateConnectionEstablished();
willThrow(new Exception()).given(wsHandler).handleMessage(session, new TextMessage("x"));
try {
XhrReceivingTransportHandler transportHandler = new XhrReceivingTransportHandler();
transportHandler.initialize(sockJsConfig);
transportHandler.handleRequest(this.request, this.response, wsHandler, session);
fail("Expected exception");
}
catch (SockJsMessageDeliveryException ex) {
assertNull(session.getCloseStatus());
}
}
示例6: handleWebSocketMessageSplitAcrossTwoMessage
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void handleWebSocketMessageSplitAcrossTwoMessage() throws Exception {
WebSocketHandler webSocketHandler = connect();
String part1 = "SEND\na:alpha\n\nMessage";
webSocketHandler.handleMessage(this.webSocketSession, new TextMessage(part1));
verifyNoMoreInteractions(this.stompSession);
String part2 = " payload\0";
webSocketHandler.handleMessage(this.webSocketSession, new TextMessage(part2));
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
verify(this.stompSession).handleMessage(captor.capture());
Message<byte[]> message = captor.getValue();
assertNotNull(message);
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
StompHeaders headers = StompHeaders.readOnlyStompHeaders(accessor.toNativeHeaderMap());
assertEquals(StompCommand.SEND, accessor.getCommand());
assertEquals("alpha", headers.getFirst("a"));
assertEquals("Message payload", new String(message.getPayload(), UTF_8));
}
示例7: interceptorsWithAllowedOrigins
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Test
public void interceptorsWithAllowedOrigins() {
WebSocketHandler handler = new TextWebSocketHandler();
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins("http://mydomain1.com");
List<Mapping> mappings = this.registration.getMappings();
assertEquals(1, mappings.size());
Mapping mapping = mappings.get(0);
assertEquals(handler, mapping.webSocketHandler);
assertEquals("/foo", mapping.path);
assertEquals(2, mapping.interceptors.length);
assertEquals(interceptor, mapping.interceptors[0]);
assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
示例8: beforeHandshake
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
HttpSession session = getSession(request);
if (session != null) {
if (isCopyHttpSessionId()) {
attributes.put(HTTP_SESSION_ID_ATTR_NAME, session.getId());
}
Enumeration<String> names = session.getAttributeNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
if (isCopyAllAttributes() || getAttributeNames().contains(name)) {
attributes.put(name, session.getAttribute(name));
}
}
}
return true;
}
示例9: supportedExtensions
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Test
public void supportedExtensions() throws Exception {
WebSocketExtension extension1 = new WebSocketExtension("ext1");
WebSocketExtension extension2 = new WebSocketExtension("ext2");
given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
given(this.upgradeStrategy.getSupportedExtensions(this.request)).willReturn(Arrays.asList(extension1));
this.servletRequest.setMethod("GET");
WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
headers.setUpgrade("WebSocket");
headers.setConnection("Upgrade");
headers.setSecWebSocketVersion("13");
headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
headers.setSecWebSocketExtensions(Arrays.asList(extension1, extension2));
WebSocketHandler handler = new TextWebSocketHandler();
Map<String, Object> attributes = Collections.<String, Object>emptyMap();
this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
verify(this.upgradeStrategy).upgrade(this.request, this.response, null, Arrays.asList(extension1),
null, handler, attributes);
}
示例10: AbstractSockJsSession
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
/**
* Create a new instance.
* @param id the session ID
* @param config SockJS service configuration options
* @param handler the recipient of SockJS messages
* @param attributes attributes from the HTTP handshake to associate with the WebSocket
* session; the provided attributes are copied, the original map is not used.
*/
public AbstractSockJsSession(String id, SockJsServiceConfig config, WebSocketHandler handler,
Map<String, Object> attributes) {
Assert.notNull(id, "SessionId must not be null");
Assert.notNull(config, "SockJsConfig must not be null");
Assert.notNull(handler, "WebSocketHandler must not be null");
this.id = id;
this.config = config;
this.handler = handler;
if (attributes != null) {
this.attributes.putAll(attributes);
}
}
示例11: constructorWithAttributeNames
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Test
public void constructorWithAttributeNames() throws Exception {
Map<String, Object> attributes = new HashMap<String, Object>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.setSession(new MockHttpSession(null, "123"));
this.servletRequest.getSession().setAttribute("foo", "bar");
this.servletRequest.getSession().setAttribute("bar", "baz");
Set<String> names = Collections.singleton("foo");
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor(names);
interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
assertEquals(2, attributes.size());
assertEquals("bar", attributes.get("foo"));
assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
}
示例12: subProtocolCapableHandlerNoMatch
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Test
public void subProtocolCapableHandlerNoMatch() throws Exception {
given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[]{"13"});
this.servletRequest.setMethod("GET");
WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
headers.setUpgrade("WebSocket");
headers.setConnection("Upgrade");
headers.setSecWebSocketVersion("13");
headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
headers.setSecWebSocketProtocol("v10.stomp");
WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
Map<String, Object> attributes = Collections.<String, Object>emptyMap();
this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
verify(this.upgradeStrategy).upgrade(this.request, this.response,
null, Collections.<WebSocketExtension>emptyList(), null, handler, attributes);
}
示例13: emptyAllowedOrigin
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Test
public void emptyAllowedOrigin() {
WebSocketHandler handler = new TextWebSocketHandler();
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
this.registration.addHandler(handler, "/foo").addInterceptors(interceptor).setAllowedOrigins();
List<Mapping> mappings = this.registration.getMappings();
assertEquals(1, mappings.size());
Mapping mapping = mappings.get(0);
assertEquals(handler, mapping.webSocketHandler);
assertEquals("/foo", mapping.path);
assertEquals(2, mapping.interceptors.length);
assertEquals(interceptor, mapping.interceptors[0]);
assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
}
示例14: subProtocolCapableHandler
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Test
public void subProtocolCapableHandler() throws Exception {
given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[]{"13"});
this.servletRequest.setMethod("GET");
WebSocketHttpHeaders headers = new WebSocketHttpHeaders(this.request.getHeaders());
headers.setUpgrade("WebSocket");
headers.setConnection("Upgrade");
headers.setSecWebSocketVersion("13");
headers.setSecWebSocketKey("82/ZS2YHjEnUN97HLL8tbw==");
headers.setSecWebSocketProtocol("v11.stomp");
WebSocketHandler handler = new SubProtocolCapableHandler("v12.stomp", "v11.stomp");
Map<String, Object> attributes = Collections.<String, Object>emptyMap();
this.handshakeHandler.doHandshake(this.request, this.response, handler, attributes);
verify(this.upgradeStrategy).upgrade(this.request, this.response,
"v11.stomp", Collections.<WebSocketExtension>emptyList(), null, handler, attributes);
}
示例15: interceptorsPassedToSockJsRegistration
import org.springframework.web.socket.WebSocketHandler; //導入依賴的package包/類
@Test
public void interceptorsPassedToSockJsRegistration() {
WebSocketHandler handler = new TextWebSocketHandler();
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
this.registration.addHandler(handler, "/foo").addInterceptors(interceptor)
.setAllowedOrigins("http://mydomain1.com").withSockJS();
List<Mapping> mappings = this.registration.getMappings();
assertEquals(1, mappings.size());
Mapping mapping = mappings.get(0);
assertEquals(handler, mapping.webSocketHandler);
assertEquals("/foo/**", mapping.path);
assertNotNull(mapping.sockJsService);
assertTrue(mapping.sockJsService.getAllowedOrigins().contains("http://mydomain1.com"));
List<HandshakeInterceptor> interceptors = mapping.sockJsService.getHandshakeInterceptors();
assertEquals(interceptor, interceptors.get(0));
assertEquals(OriginHandshakeInterceptor.class, interceptors.get(1).getClass());
}