Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket The remote endpoint was in state [TEXT_PARTIAL_WRITING]

Tags:

java

spring

I'm using tomcat 8.0.15, spring 4.1.5.

I implemented 3 mandatory functions for using websocket like below. It's very simple.

private Map<String, WebSocketSession> map_users = new ConcurrentHashMap<>();
private Map<String, String> map_id = new ConcurrentHashMap<>();

public void afterConnectionEstablished(WebSocketSession wss) throws Exception {
    map_users.put(wss.getId(), wss);
}

public void afterConnectionClosed(WebSocketSession wss, CloseStatus cs) throws Exception {
    map_users.remove(wss.getId());

    // remove user
    String username = map_id.get(wss.getId());
    if (username != null) {
        map_id.remove(wss.getId());
        map_id.remove(username);
    }
}

public void handleTextMessage(WebSocketSession wss, TextMessage tm) throws Exception {
    String str = tm.getPayload();
    String username = ...;

    // regist user
    if (!map_id.get(wss.getId())) {
        map_id.put(wss.getId(), username);
        map_id.put(username, wss.getId());
    }

    for (WebSocketSession w: map_users.values()) {
        w.sendMessage(new TextMessage(wss.getId() + " send to " + w.getId() + ", msg:" + tm.getPayload()));
    }
}

Some client sends a message and other clients get the message by the handleTextMessage.

In my case, without the handleTextMessage function, server program wants to send a text message to clients. (for this I saved a WebSocketSession's Id and username into map_id)

String websocketsesssion_id = map_id.get(username);
WebSocketSession wss = map_users.get(websocketsesssion_id);
wss.sendMessage(new TextMessage(new java.util.Date()));

Above code works very well. But when some client's WebSocketSession is in use and try to use simultaneously, it makes error. It means 1. some client send message --> handleTextMessage is called --> the client's WebSocketSession is using 2. server program wants to send message to that client --> get the client's WebSocketSession from map --> try to send message with the same WebSocketSession

Stacktrace:] with root cause
java.lang.IllegalStateException: The remote endpoint was in state [TEXT_PARTIAL_WRITING] which is an invalid state for called method
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase$StateMachine.checkState(WsRemoteEndpointImplBase.java:1092)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase$StateMachine.textPartialStart(WsRemoteEndpointImplBase.java:1050)
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendPartialString(WsRemoteEndpointImplBase.java:218)
    at org.apache.tomcat.websocket.WsRemoteEndpointBasic.sendText(WsRemoteEndpointBasic.java:49)
    at org.springframework.web.socket.adapter.standard.StandardWebSocketSession.sendTextMessage(StandardWebSocketSession.java:197)
    at org.springframework.web.socket.adapter.AbstractWebSocketSession.sendMessage(AbstractWebSocketSession.java:105)
    at org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession.writeFrameInternal(WebSocketServerSockJsSession.java:222)
    at org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession.writeFrame(AbstractSockJsSession.java:325)
    at org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession.sendMessageInternal(WebSocketServerSockJsSession.java:212)
    at org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession.sendMessage(AbstractSockJsSession.java:161)

As the result, WebSocketSession is closed and client has to open new WebSocketSession again.

So, my question is:

Can I check whether the WebSocketSession is in use or not? (outside the handleTextMessage function)

like image 261
mc j Avatar asked Mar 12 '15 04:03

mc j


1 Answers

The problem is that several thread try to write in same time on the socket so :

try :

String websocketsesssion_id = map_id.get(username);
WebSocketSession wss = map_users.get(websocketsesssion_id);
synchronized(wss) {
 wss.sendMessage(new TextMessage(new java.util.Date()));
}
like image 69
user3179515 Avatar answered Oct 02 '22 12:10

user3179515