Java WebSockets:远程端点处于 [TEXT_FULL_WRITING] 状态 [英] Java WebSockets: The remote endpoint was in state [TEXT_FULL_WRITING]

查看:22
本文介绍了Java WebSockets:远程端点处于 [TEXT_FULL_WRITING] 状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一些基于 websockets 的应用程序,它将与 JS 客户端进行非常密集的通信.

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 目前没有丰富的此类异常,在此问题上解决了几个小时后,仍然没有解决方案.

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,在 Tomcat 7.0.52 和 Tomcat 8.0.3 上测试.

Java 7.0.21, tested on Tomcat 7.0.52 and Tomcat 8.0.3.

任何答案都非常感谢!提前致谢.

Any answer is highly appreciated! Thanks in advance.

更新 3/11/2014:我使用 Jetty 9.1 测试了我的应用程序,但没有发生此异常.我假设这是Tomcat实现错误.

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.

推荐答案

好的,这不是 Tomcat 的问题,而是我的错.

OK, this is not a Tomcat issue but my fault.

我的 onMessage 函数返回了一个字符串,这意味着我回显了消息.结果,那部分代码没有同步.

My onMessage function returned a string, what means that I echoed the message back. As result, those part of code was not synced.

不好:

@OnMessage
public String onMessage(String message, Session session) {
   ...
   return message;
}

好:

@OnMessage
public void onMessage(String message, Session session) {
   ...
}

这篇关于Java WebSockets:远程端点处于 [TEXT_FULL_WRITING] 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆