Grails Atmosphere 插件向不同客户端广播

发布于 2024-12-26 11:00:45 字数 1987 浏览 2 评论 0原文

我正在尝试扩展 Groovy Mag Atmosphere 示例 (https://github.com/rbramley/GroovyMagJMS) 向不同的客户端广播。 (如向 Atmosphere 中的一部分订阅者广播

客户端使用 url 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()
        }
    }
}

}

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()
        }
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲喜皆因你 2025-01-02 11:00:45

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文