Broadcasting to a subset of subscribers in Atmosphere

社会主义新天地 提交于 2019-12-06 09:32:09

问题


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.


回答1:


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.




回答2:


There is a misunderstaning of the concept of comet. Its just another publish/subscribe implementation. If you have multiple chat-rooms, then you need to have multiple "topics", i.e. multiple channels the user can register to. E.g.:

broadcaster['/atmosphere/chatRoom1'].broadcast('Hello world!')

broadcaster['/atmosphere/chatRoom2'].broadcast('Hello world!')

So I would advance you to creaet multiple channels and do not filter manually the set of users, which should retrieve messages (which is definitely not the way it should be done). You do not need to create anything on the server side on this, since the user will just register for a specific channel and receive messages, which anyone is putting into it.




回答3:


I would recommend you create an AtmosphereHandler for one URL like /atmosphere/chat-room and then use the AtmosphereResource and bind an BroadcastFilter with it, lets say name it ChatRoomBroadcastFilter.

Whenever a user subscribes to a new chat room, a message would be sent to the server (from the client) telling the server about the subscription. Once subscribed, maintain the list of users <> chat room bindings somewhere on the server.

Whenever a message is broadcasted, broadcast it with the chat room id with it. The in the ChatRoomBroadcastFilter (You probably need to make this a PerRequestBroacastFilter) propagate the message to the user only if the user subscribed to the chat room. I am not sure if this clears it out. If you need code example please mention in the comments. I'll put that but that needs some time so ain't putting it right now ;).



来源:https://stackoverflow.com/questions/7199164/broadcasting-to-a-subset-of-subscribers-in-atmosphere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!