mercredi 11 mars 2015

WebSocket The remote endpoint was in state [TEXT_PARTIAL_WRITING]

I'm usiing 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)


Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire