Grails Atmosphere Plugin向不同的客户广播

时间:2012-01-11 23:02:53

标签: grails atmosphere

我正在尝试扩展Groovy Mag Atmosphere示例(https://github.com/rbramley/GroovyMagJMS)以向不同的客户端广播。 (比如Broadcasting to a subset of subscribers in Atmosphere

客户端与网址http://localhost:8080/GrailsTest/atmosphere/messages/?id=1相关联。 id将传递给服务器。新添加的lookupBroadcaster方法使用id创建一个新的Broadcaster对象。当我想要广播消息时,客户端不会收到结果。

有人可以帮助我,也许可以尝试一下吗?

我将氛围0.8.2库添加到BuildConfig.groovy以使用'/ 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()
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

萨吕。

这应该基于该代码段工作。广播时是否调用onStateChange()方法?由于您正在恢复,第一次广播将起作用,但之后AtmosphereResource将从其关联的广播公司中删除,因此不再更新。

由于

- Jeanfrancois