Unable to broadcast to single connection using Atmosphere runtime

后端 未结 1 1572
礼貌的吻别
礼貌的吻别 2020-12-28 10:53

I am using Atmosphere runtime 0.6 Snapshot. Tomcat 7 is logging correctly that I am using the Http11 Nio connector and there is no warning that BlockingIO will be used.

相关标签:
1条回答
  • 2020-12-28 11:55

    Ok, I figured out how this can be achieved with Atmosphere runtime. First, I upgraded to 0.7 SNAPSHOT, but I think the same logic would work with 0.6 as well.

    So, to create a broadcaster for a single user:

    In GET request,

      // Use one Broadcaster per AtmosphereResource             
    try {       
    atmoResource.setBroadcaster(BroadcasterFactory.getDefault().get());     
    
    } catch (Throwable t) {
                    throw new IOException(t);
                }
    
                // Create a Broadcaster based on this session id.
                selfBroadcaster = atmoResource.getBroadcaster();
                // add to the selfBroadcaster
                selfBroadcaster.addAtmosphereResource(atmoResource);
    
                atmoResource.suspend();   
    

    When login action is invoked,

    //Get this broadcaster from session and add it to BroadcasterFactory.
    
    Broadcaster selfBroadcaster = (Broadcaster) session.getAttribute(sessionId);
    
    BroadcasterFactory.getDefault().add(selfBroadcaster, name);
    
    Now the global broadcaster. The logic here is, you create a broadcaster from the first resource and then add each resource as they log in.
    
    Broadcaster globalBroadcaster;
    
    globalBroadcaster = BroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class, GLOBAL_TOKEN, false);
                    if (globalBroadcaster == null) {
                      globalBroadcaster = selfBroadcaster;
    
                        } else {
                            BroadcasterFactory.getDefault().remove(
                                    globalBroadcaster, GLOBAL_TOKEN);
                            AtmosphereResource r = (AtmosphereResource) session
                                    .getAttribute("atmoResource");
                            globalBroadcaster.addAtmosphereResource(r);
    
                        }
                        BroadcasterFactory.getDefault().add(globalBroadcaster,
                                GLOBAL_TOKEN);
    

    Finally, you can broadcast to Single connection or Globally to all connections as follows:

    // Single Connection/Session
    Broadcaster singleBroadcaster= BroadcasterFactory.getDefault().lookup(
                                DefaultBroadcaster.class, name);
    singleBroadcaster.broadcast("Only for you");
    
    // Global 
    Broadcaster globalBroadcaster = BroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class,GLOBAL_TOKEN, false);
    globalBroadcaster.broadcast("Global message to all");
    

    To send message to partner, just lookup the broadcaster for the partner and do the same as above for single connection.

    Hope this helps someone who tries to achieve the same. There may be better ways of doing this. I think I will have to use this approach until someone suggests a better solution.

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