Atmosphere + Jersey: How do I have multiple broadcasters?

后端 未结 2 596
误落风尘
误落风尘 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:24

    Just inject Broadcaster using the @PathParam annotation:

    private
    @PathParam("topic")
    Broadcaster topic;
    

    You can also use the @Context annotation. Hope that help.

    -- Jeanfrancois

    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题