Session#getOpenSessions
据说 Java WebSocket API 可以
Return a copy of the Set of all the open web socket sessions that represent connections to the same endpoint to which this session represents a connection.
但是,这是不明确的,因为“端点” 可以指代此Session
表示的唯一端点实例,也可以指代端点类(更多恰好是该类的实例集)。每个新连接都会创建该类的一个新实例,因此始终只有一个 Session
与每个实例相关联(据我所知)。鉴于此,人们会认为第一种解释是错误的。
Danny Coward,上述链接文档和 Java WebSocket Programming 的作者给出了刚刚提到的书中的一个例子,说明如何使用支持上述假设的方法:
As a reminder, each client that is connected to this server endpoint is associated with a unique WebSocket connection, each of which in turn is represented by a unique Session instance. So this method is a very convenient way of listing all the connected clients to this endpoint.
人们期望它的工作方式确实是它通常的工作方式,但 Tomcat 7.0.63 改变了这一点。这里有一些关于此问题的问题,例如 this one和 this one , 在那里它被当作一个错误来对待。这是reported as a bug在 BugZilla 中也是如此,但被驳斥为预期行为。阅读changelog for Tomcat 7.0.63我们看到以下内容:
Session.getOpenSessions should return all sessions associated with a given endpoint instance, rather than all sessions from the endpoint class.
这引起了很多困惑。
因此我的问题是:
- 这是 Tomcat 团队的错误吗?
- 现在每个实例只有一个
Session
,getOpenSessions
有什么用? - 是否有解决此问题的方法,还是必须远离 >= 7.0.63 的 Tomcat 版本?
最佳答案
是的,这是 Tomcat 团队的错误。 (我是 WebSocket 规范的前维护负责人)。 Session#getOpenSessions应将所有打开的 session 返回到相同的端点类。
关于java - Tomcat对javax.websocket.Session#getOpenSessions的解读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47612057/