org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator Java Examples

The following examples show how to use org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator. 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: ChatWebSocketHandler.java    From MyBlog with Apache License 2.0 6 votes vote down vote up
public void sendAllUser(String msg) {
    String username = StringUtils.EMPTY;
    try {
        for (Map.Entry<String, ConcurrentWebSocketSessionDecorator> entry : sessionMap.entrySet()) {
            username = entry.getKey();
            ConcurrentWebSocketSessionDecorator session = entry.getValue();
            if (!session.isOpen()) {
                log.info("session close when send all user, username:[{}]", username);
                continue;
            }
            session.sendMessage(new TextMessage(msg));
        }
    } catch (Exception e) {
        log.error("send all user error, username:[{}]", username, e);
    }
}
 
Example #2
Source File: SubProtocolWebSocketHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void emptySubProtocol() throws Exception {
	this.session.setAcceptedProtocol("");
	this.webSocketHandler.setDefaultProtocolHandler(this.defaultHandler);
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.defaultHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
	verify(this.stompHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
	verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
}
 
Example #3
Source File: SubProtocolWebSocketHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void noSubProtocolOneHandler() throws Exception {
	this.webSocketHandler.setProtocolHandlers(Arrays.asList(stompHandler));
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.stompHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
}
 
Example #4
Source File: SubProtocolWebSocketHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void emptySubProtocol() throws Exception {
	this.session.setAcceptedProtocol("");
	this.webSocketHandler.setDefaultProtocolHandler(this.defaultHandler);
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.defaultHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
	verify(this.stompHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
	verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
}
 
Example #5
Source File: SubProtocolWebSocketHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void nullSubProtocol() throws Exception {
	this.webSocketHandler.setDefaultProtocolHandler(defaultHandler);
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.defaultHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
	verify(this.stompHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
	verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
}
 
Example #6
Source File: SubProtocolWebSocketHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void subProtocolDefaultHandlerOnly() throws Exception {
	this.webSocketHandler.setDefaultProtocolHandler(stompHandler);
	this.session.setAcceptedProtocol("v12.sToMp");
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.stompHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
}
 
Example #7
Source File: SubProtocolWebSocketHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void subProtocolMatch() throws Exception {
	this.webSocketHandler.setProtocolHandlers(Arrays.asList(stompHandler, mqttHandler));
	this.session.setAcceptedProtocol("v12.sToMp");
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.stompHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
	verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
}
 
Example #8
Source File: ChatWebSocketHandler.java    From MyBlog with Apache License 2.0 5 votes vote down vote up
public boolean sendToUser(String username, TextMessage msg) {
    try {
        ConcurrentWebSocketSessionDecorator session = sessionMap.get(username);
        if (session == null || !session.isOpen()) {
            return false;
        }
        session.sendMessage(msg);
        return true;
    } catch (Exception e) {
        log.error("send to user error, username:[{}]", username, e);
        return false;
    }
}
 
Example #9
Source File: ChatWebSocketHandler.java    From MyBlog with Apache License 2.0 5 votes vote down vote up
@Override
public void afterConnectionEstablished(WebSocketSession session) throws IOException {
       String username = (String) session.getAttributes().get("username");
       ConcurrentWebSocketSessionDecorator sessionDecorator = new ConcurrentWebSocketSessionDecorator(session, 1000, 5120);
       sessionMap.put(username, sessionDecorator);
       log.info("connect succ, username:[{}]", username);
       JSONObject jsonObject = new JSONObject();
       jsonObject.put("action", "connect");
       jsonObject.put("username", username);
       sessionDecorator.sendMessage(new TextMessage(jsonObject.toJSONString()));
   }
 
Example #10
Source File: SubProtocolWebSocketHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void noSubProtocolOneHandler() throws Exception {
	this.webSocketHandler.setProtocolHandlers(Arrays.asList(stompHandler));
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.stompHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
}
 
Example #11
Source File: SubProtocolWebSocketHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void nullSubProtocol() throws Exception {
	this.webSocketHandler.setDefaultProtocolHandler(defaultHandler);
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.defaultHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
	verify(this.stompHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
	verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
}
 
Example #12
Source File: SubProtocolWebSocketHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void subProtocolDefaultHandlerOnly() throws Exception {
	this.webSocketHandler.setDefaultProtocolHandler(stompHandler);
	this.session.setAcceptedProtocol("v12.sToMp");
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.stompHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
}
 
Example #13
Source File: SubProtocolWebSocketHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void subProtocolMatch() throws Exception {
	this.webSocketHandler.setProtocolHandlers(Arrays.asList(stompHandler, mqttHandler));
	this.session.setAcceptedProtocol("v12.sToMp");
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.stompHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
	verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
}
 
Example #14
Source File: SubProtocolWebSocketHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void noSubProtocolOneHandler() throws Exception {
	this.webSocketHandler.setProtocolHandlers(Arrays.asList(stompHandler));
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.stompHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
}
 
Example #15
Source File: SubProtocolWebSocketHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void emptySubProtocol() throws Exception {
	this.session.setAcceptedProtocol("");
	this.webSocketHandler.setDefaultProtocolHandler(this.defaultHandler);
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.defaultHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
	verify(this.stompHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
	verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
}
 
Example #16
Source File: SubProtocolWebSocketHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void nullSubProtocol() throws Exception {
	this.webSocketHandler.setDefaultProtocolHandler(defaultHandler);
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.defaultHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
	verify(this.stompHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
	verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
}
 
Example #17
Source File: SubProtocolWebSocketHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void subProtocolDefaultHandlerOnly() throws Exception {
	this.webSocketHandler.setDefaultProtocolHandler(stompHandler);
	this.session.setAcceptedProtocol("v12.sToMp");
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.stompHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
}
 
Example #18
Source File: SubProtocolWebSocketHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void subProtocolMatch() throws Exception {
	this.webSocketHandler.setProtocolHandlers(Arrays.asList(stompHandler, mqttHandler));
	this.session.setAcceptedProtocol("v12.sToMp");
	this.webSocketHandler.afterConnectionEstablished(session);

	verify(this.stompHandler).afterSessionStarted(
			isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel));
	verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel);
}
 
Example #19
Source File: SubProtocolWebSocketHandler.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Decorate the given {@link WebSocketSession}, if desired.
 * <p>The default implementation builds a {@link ConcurrentWebSocketSessionDecorator}
 * with the configured {@link #getSendTimeLimit() send-time limit} and
 * {@link #getSendBufferSizeLimit() buffer-size limit}.
 * @param session the original {@code WebSocketSession}
 * @return the decorated {@code WebSocketSession}, or potentially the given session as-is
 * @since 4.3.13
 */
protected WebSocketSession decorateSession(WebSocketSession session) {
	return new ConcurrentWebSocketSessionDecorator(session, getSendTimeLimit(), getSendBufferSizeLimit());
}
 
Example #20
Source File: SubProtocolWebSocketHandler.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Decorate the given {@link WebSocketSession}, if desired.
 * <p>The default implementation builds a {@link ConcurrentWebSocketSessionDecorator}
 * with the configured {@link #getSendTimeLimit() send-time limit} and
 * {@link #getSendBufferSizeLimit() buffer-size limit}.
 * @param session the original {@code WebSocketSession}
 * @return the decorated {@code WebSocketSession}, or potentially the given session as-is
 * @since 4.3.13
 */
protected WebSocketSession decorateSession(WebSocketSession session) {
	return new ConcurrentWebSocketSessionDecorator(session, getSendTimeLimit(), getSendBufferSizeLimit());
}