0

I'm trying to extend the Groovy Mag Atmosphere Example (https://github.com/rbramley/GroovyMagJMS) to broadcast to different clients. (Like in Broadcasting to a subset of subscribers in Atmosphere)

A client connects with url http://localhost:8080/GrailsTest/atmosphere/messages/?id=1. An id will be passed to the server. The new added lookupBroadcaster Method creates a new Broadcaster Object with the id. When I wanna broadcast a message, the client does not receive the result.

Can somebody help me and maybe try it out?

I'm added the atmosphere 0.8.2 library to BuildConfig.groovy to use mappings like '/atmosphere/messages/*'.

dependencies {  
    runtime 'org.atmosphere:atmosphere-runtime:0.8.2'
}


class AtmosphereService {

static transactional = false

static atmosphere = [mapping: '/atmosphere/messages/*']

static exposes = ['jms']

@Subscriber(topic='msgevent')
def onEvent(msg) {
    println 'onevent'
    def payload = msg
    if(msg instanceof Map) {
        // convert map messages to JSON
        payload = msg.encodeAsJSON()
    }

    Broadcaster b = lookupBroadcaster(msg["id"], false);
    b.broadcast(payload)

    return null
}

Broadcaster lookupBroadcaster(String id, Boolean createBroadcast) {
    return BroadcasterFactory.getDefault().lookup(id, createBroadcast)
}

def onRequest = { event ->

    def req = event.request
    def id = req.getParameter("id")

    Broadcaster b = lookupBroadcaster(id, true);
    event.setBroadcaster(b);
    b.addAtmosphereResource(event)

    event.suspend()

}

def onStateChange = { event ->
    if (event.message) {
        log.info "onStateChange, message: ${event.message}"

        if (event.isSuspended()) {
            event.resource.response.writer.with {
                write "<script>parent.callback('${event.message}');</script>"
                flush()
            }
            event.resume()
        }
    }
}

}

Community
  • 1
  • 1
user1117605
  • 111
  • 2
  • 11

1 Answers1

0

That should work based on that code snippet. Is the onStateChange() method invoked when you broadcast? Since you are resuming, the first broadcast will works but after that the AtmosphereResource will be removed from its associated Broadcaster, hence no more update.

halfer
  • 19,824
  • 17
  • 99
  • 186
jfarcand
  • 1,705
  • 9
  • 6