I am using tomcat 8.0.15, spring 4.1.5.
I have implemented 3 required functions for using websocket, as shown below. It is 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());
Some clients send a message, while other clients receive a message using handleTextMessage.
In my case, without the handleTextMessage function, the server program wants to send a text message to clients. (for this, I saved the WebSocketSession id and username in map_id)
String websocketsesssion_id = map_id.get(username); WebSocketSession wss = map_users.get(websocketsesssion_id); wss.sendMessage(new TextMessage(new java.util.Date()));
It works very well on code. But when some WebSocketSession client is used and trying to use at the same time, it makes an error. This means 1. some message is called to send the client β handleTextMessage β the WebSocketSession client uses 2. the server program wants to send a message to this client β receive the client WebSocketSession from the card β try to send a 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 a result, WebSocketSession is closed, and the client must open a new WebSocketSession again.
So my question is:
Can I check if WebSocketSession is used or not? (outside handleTextMessage function)
java spring
mc j
source share