Atmosphere Grails 单用户广播
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你可以使用会话来共享 onRequest 的事件。
然后在控制器中:
I thinks you can use session to share the onRequest's event.
then in controller :
当您在 grails 中定义事件时,您可以使用 browserFilter 闭包过滤掉事件。
When you define your events in grails, you can filter out cients by using a browserFilter closure.
嘿,您可以使用分配给每个大气资源的
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);
您可以通过会话
查看此API。
(我是Atmosphere 的创造者)。
You can pass the session
See this API.
(I am Atmosphere's creator).