【问题标题】:What is the correct way to add an EventListener to an AtmosphereResource?将 EventListener 添加到 AtmosphereResource 的正确方法是什么?
【发布时间】:2014-06-09 02:58:31
【问题描述】:

我正在使用 Atmosphere Framework 2.0.8。

我已经在我的应用程序中实现了一个 AtmosphereHandler,并且通过 WebSockets 和长轮询正确地进行了两种方式的通信。

我正在尝试为客户端断开连接时添加一些处理代码以清理特定于该客户端的某些资源(即,我要删除的表中有一个条目)。

我已阅读以下 wiki 条目:

OnDisconnect 技巧:https://github.com/Atmosphere/atmosphere/wiki/onDisconnect-tricks 配置氛围监听器:https://github.com/Atmosphere/atmosphere/wiki/Configuring-Atmosphere-Listener

我不清楚的是我应该在哪里添加调用

atmosphereResource.addEventListener( new AtmosphereResourceEventListenerAdapter() {} );

我最终在 AtmosphereHandler 的 JavaDoc 中找到了一些示例代码,该代码在 onRequest() 方法中注册了 EventListener。 http://atmosphere.github.io/atmosphere/apidocs/org/atmosphere/cpr/AtmosphereHandler.html

我想知道这是否是正确的方法?

据我了解,AtmosphereResource 代表客户端和服务器之间的连接,用于该连接的生命周期。通过来自同一客户端的 onRequest() 方法进行多次调用时,对象的 uuid 保持一致。因此,每次调用 onRequest 方法时,同一个 AtmosphereResource 对象都会添加 EventListener。

这似乎是错误的。这不会导致为每个 AtmosphereResource 注册数千个 EventListener 吗?

似乎每个 AtmosphereResource 的 EventLister 应该只注册一次。

我觉得我在这里缺少一些基本的东西。谁能解释一下?

【问题讨论】:

    标签: atmosphere


    【解决方案1】:

    这是一个使用MeteorServletexample,因此它看起来与您必须要做的不太一样,但它应该可以帮助您入门。我将侦听器添加到Meteor 实例,您将把您的侦听器添加到AtmosphereResource。每个资源只有一个监听器。

    重写的onDisconnect() 方法调用处理事件的this Grails service method。当然,你会想要调用一些东西来清理你的数据库资源。

    请注意,servlet 是使用these options 配置的。我想你可能需要org.atmosphere.interceptor.HeartbeatInterceptor,但距离我最初设置它已经很久了,我不记得是否有必要。

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 2010-11-07
      • 2016-11-09
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      • 2014-12-08
      • 1970-01-01
      相关资源
      最近更新 更多