Atmosphere Grails 单用户广播

发布于 2024-11-27 02:57:51 字数 1199 浏览 3 评论 0原文

我正在创建一个 Grails 应用程序,它利用 Atmosphere 插件将数据推送到浏览器。但是,我在为单个用户(会话的用户)创建广播频道时遇到了麻烦。我的代码如下:

服务:

static atmosphere = [mapping: '/atmosphere/recommend']

def onRequest = { event ->
    def request = event.request
    def response = event.response

    event.suspend()     

    def broadcaster = event.broadcaster
    request.session.broadcaster = broadcaster
    broadcaster.broadcasterConfig.addFilter(new XSSHtmlFilter())     
}

def onStateChange = { event ->

    if (!event.message) return

    event.resource.response.writer.with {
        write "<script>parent.callback('${event.message}');</script>"
        flush()
    }
}

控制器:

def builder = new JSONBuilder()
    def jsonResult = builder.build{
                            artist = artistInstance
                            location = {
                                lat = eventInstance.location.lat
                                lng = eventInstance.location.lng
                            }
                        }

session.broadcaster.broadcast(jsonResult)

该解决方案将 jsonResult 广播给每个用户。我想要实现的是只为当前用户广播。 有什么想法吗? 如果您需要更多详细信息,请告诉我。

谢谢

I'm creating a Grails application which makes use of the Atmosphere plugin to push data to the browser. However I'm having trouble in creating a broadcast channel to a single user (the session's user). My code is has follows:

Service:

static atmosphere = [mapping: '/atmosphere/recommend']

def onRequest = { event ->
    def request = event.request
    def response = event.response

    event.suspend()     

    def broadcaster = event.broadcaster
    request.session.broadcaster = broadcaster
    broadcaster.broadcasterConfig.addFilter(new XSSHtmlFilter())     
}

def onStateChange = { event ->

    if (!event.message) return

    event.resource.response.writer.with {
        write "<script>parent.callback('${event.message}');</script>"
        flush()
    }
}

Controller:

def builder = new JSONBuilder()
    def jsonResult = builder.build{
                            artist = artistInstance
                            location = {
                                lat = eventInstance.location.lat
                                lng = eventInstance.location.lng
                            }
                        }

session.broadcaster.broadcast(jsonResult)

This solution broadcasts the jsonResult to every user. What I want to achieve is to broadcast only for the current user.
Any ideas?
If you need more details just let me know.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

财迷小姐 2024-12-04 02:57:51

我认为你可以使用会话来共享 onRequest 的事件。

request.session[userId]=event

然后在控制器中:

broadcast(jsonResult,session[userId])

I thinks you can use session to share the onRequest's event.

request.session[userId]=event

then in controller :

broadcast(jsonResult,session[userId])
简美 2024-12-04 02:57:51

当您在 grails 中定义事件时,您可以使用 browserFilter 闭包过滤掉事件。

When you define your events in grails, you can filter out cients by using a browserFilter closure.

飘然心甜 2024-12-04 02:57:51

嘿,您可以使用分配给每个大气资源的 uuid
要根据大气请求检索暂停的 uuid,您可以执行以下操作:
<代码>
字符串暂停UUID = (String)req.getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);

Hey you can make use of the uuid which is assigned to each Atmosphere Resource.
To retrieve the suspended uuid based on an Atmosphere Request you can do:

String suspendedUUID = (String)req.getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);

困倦 2024-12-04 02:57:51

您可以通过会话

session.broadcaster.broadcast(jsonResult, AtmosphereResource)

查看此API

(我是Atmosphere 的创造者)。

You can pass the session

session.broadcaster.broadcast(jsonResult, AtmosphereResource)

See this API.

(I am Atmosphere's creator).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文