下面的代码尝试通过Java websocket发送消息;但是,它偶尔会产生错误(如下面的错误堆栈所示)。任何人都可以提出问题的解决方案吗?
代码:
if(currSession != null && currSession.isOpen()) {
try {
currSession.sendMessage(new BinaryMessage(flowTable.getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
错误堆栈:
java.lang.IllegalStateException: The remote endpoint was in state [BINARY_PARTIAL_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.binaryPartialStart(WsRemoteEndpointImplBase.java:963)
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendPartialBytes(WsRemoteEndpointImplBase.java:140)
at org.apache.tomcat.websocket.WsRemoteEndpointBasic.sendBinary(WsRemoteEndpointBasic.java:56)
at org.springframework.web.socket.adapter.standard.StandardWebSocketSession.sendBinaryMessage(StandardWebSocketSession.java:202)
at org.springframework.web.socket.adapter.AbstractWebSocketSession.sendMessage(AbstractWebSocketSession.java:107)
at com.hp.fucms.impl.TopoRestController.setFlowTable(TopoRestController.java:147)
答案 0 :(得分:3)
使用同步方法包装代码,并通过此新方法汇集所有调用。看来tomcat web套接字无法处理同时放在同一websocket会话中的多条消息。我的代码在Glassfish下运行得很完美,当我搬到Tomcat时,它立即崩溃了。然后我按照上面的解释改变了我的代码,我的所有问题都消失了......而且有很多欢乐。