Atmosphere + Jersey: How do I have multiple broadcasters?

后端 未结 2 622
误落风尘
误落风尘 2021-01-03 08:05

I have a working Jersey/Atmosphere/Guice application which has two Atmosphere Resources. The first is pretty much a clone of the example chat application:



        
2条回答
  •  滥情空心
    2021-01-03 08:27

    In the resource, suspend using the channel you want (the 'true' parameter to lookup() forces the channel to be created if it doesn't exist):

    @Suspend( contentType = MediaType.APPLICATION_JSON, period = MAX_SUSPEND_MSEC )
    @GET
    public Broadcastable suspend( @Context final BroadcasterFactory factory )
    {
        return new Broadcastable( factory.lookup( MY_CHANNEL, true ) );
    }
    

    In the other code, which can be pretty much anywhere, broadcast to that channel:

    Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup( MY_CHANNEL );
    if( broadcaster != null ) {
        broadcaster.broadcast( message );
    }
    

    If you're going to be broadcasting from a resource method, you can annotate it instead (as shown in ChatResource's broadcast() method).

提交回复
热议问题