I am trying to implement some application based on websockets which will communicate with JS clients quite intensively.
The code to send the message is quite primitive:
synchronized (session) {
if (session.isOpen()) {
session.getBasicRemote().sendText(message);
}
}
For rare sending it works just fine, but when few threads are trying to send some messages by the same session (socket), next exception is thrown (please note that it is not multithreading issue because code block is synchronized by session):
java.lang.IllegalStateException: The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called method
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase$StateMachine.checkState(WsRemoteEndpointImplBase.java:1015)
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase$StateMachine.textStart(WsRemoteEndpointImplBase.java:978)
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendString(WsRemoteEndpointImplBase.java:161)
at org.apache.tomcat.websocket.WsRemoteEndpointBasic.sendText(WsRemoteEndpointBasic.java:37)
Google is not rich of such type of exceptions at the moment and after beating few hours on this issue, still no solution.
Java 7.0.21, tested on Tomcat 7.0.52 and Tomcat 8.0.3.
Any answer is highly appreciated! Thanks in advance.
UPDATE 3/11/2014: I tested my application with Jetty 9.1 and this exception didn't occur. I assume that this is Tomcat implementation bug.
OK, this is not a Tomcat issue but my fault.
My onMessage function returned a string, what means that I echoed the message back. As result, those part of code was not synced.
Bad:
@OnMessage
public String onMessage(String message, Session session) {
...
return message;
}
Good:
@OnMessage
public void onMessage(String message, Session session) {
...
}