广播到Atmosphere中的订阅者子集 [英] Broadcasting to a subset of subscribers in Atmosphere
问题描述
我想要做的是:
能够让用户订阅许多不同的聊天室,并使用反向AJAX /慧星将聊天室中的消息发送给登录该聊天室的所有人。 (稍微复杂一些,但这是一个类似的用例)。
我在做什么:
在JMS和Atmosphere中使用Grails。当发送消息时,我使用JMS发送由Grails服务接收的消息对象,然后将其广播到大气URL(即大气/消息)。
显然,JMS在这里有点多余,但我尽管可以用它来帮助我筛选应该检索邮件的人,尽管这看起来不会起作用(鉴于订阅者基本上是单件服务...... )。
无论如何,我需要做的只是发送一条消息给正在收听大气/消息的人群。一个REST式的网址在这里是完美的(例如atmosphere / messages / *,其中*是房间ID),但我不知道如何用Atmosphere做到这一点。
任何关于我如何能够实现我想要的想法/建议?这里没有什么具体的东西可以随意提出几乎任何东西。我甚至一直在想(基于对另一个问题的回答),例如,如果我可以做一些事情,比如发送消息到Node.js服务器,并且处理反向的AJAX / comet部分。
如果我正确理解您的要求,以下内容应该可以工作(jax-rs + scala代码):
1)每个想要从聊天室收到消息的人都注册了它:
@GET
@Path(选择/ a / path)
def register(@QueryParam(chatroomId)chatroomId:Broadcaster){
//或者,可以使用Suspend注释
new SuspendResponse.SuspendResponseBuilder [String]()
.resumeOnBroadcast(false).broadcaster(chatroomId).scope(SCOPE.REQUEST)
.period(suspendTimeout,TimeUnit.MINUTES)
.addListener(new AtmosphereEventsLogger() ).build
}
<2>要为所有注册用户广播消息,请致电该以下方法:
@POST
@Broadcast
@Path(选择/ a / path / {chatroomId })
def broadcast(@PathParam(chatroomId)id:String){
//首先使用BroadcasterFactory
查找广播者BroadcasterFactory.getDefault()。lookupAll()//或也许有一个ID找到?
broadcaster = ...
broadcaster.broadcast(< your message>)
}
我也建议您阅读气氛白皮书,看看邮件列表和 Jeanfrancois Arcand的博客。
希望有所帮助。
What I'm trying to do: Be able to have users subscribed to a number of different 'chat rooms' and use reverse AJAX / comet to send messages from a chat room to everyone logged into that room. (a bit more complicated but this is a similar use case).
What I'm doing: Using Grails with JMS and Atmosphere. When a message is sent, I'm using JMS to send the message object which is received by a Grails service which is then broadcasted to the atmosphere URL (i.e. atmosphere/messages).
Obviously JMS is a bit redundant there but I though I could use it to help me filter who should retrieve the message although that doesn't really look it'll work (given that the subscriber is basically a singleton service...).
Anyway, what I need to be able to do is only send out a message to the correct subset of people listening to atmosphere/messages. A RESTful-type URL will be perfect here (i.e. atmosphere/messages/* where * is the room ID) however I have no idea how to do that with Atmosphere.
Any ideas / suggestions on how I can achieve what I want? Nothing is concrete at all here so feel free to suggest almost anything. I've even been thinking (based on the response to another question), for example, if I could do something like send out messages to a Node.js server and have that handle the reverse AJAX / comet part.
If I understand your requirements correctly the following should work (jax-rs + scala code):
1) Everyone who wants to get messages from a chat room registers for it:
@GET
@Path(choose/a/path)
def register(@QueryParam("chatroomId") chatroomId: Broadcaster) {
// alternatively, the Suspend annotation can be used
new SuspendResponse.SuspendResponseBuilder[String]()
.resumeOnBroadcast(false).broadcaster(chatroomId).scope(SCOPE.REQUEST)
.period(suspendTimeout, TimeUnit.MINUTES)
.addListener(new AtmosphereEventsLogger()).build
}
2) To broadcast a message for all the registered users, call the following method:
@POST
@Broadcast
@Path(choose/a/path/{chatroomId})
def broadcast(@PathParam("chatroomId") id: String) {
// first find your broadcaster with the BroadcasterFactory
BroadcasterFactory.getDefault().lookupAll() // or maybe there is a find by id?
broadcaster = ...
broadcaster.broadcast(<your message>)
}
I also recommend reading the atmosphere whitepaper, have a look at the mailing list and at Jeanfrancois Arcand's blog.
Hope that helps.
这篇关于广播到Atmosphere中的订阅者子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!