我試圖擴展Groovy Mag氣氛示例(https://github.com/rbramley/GroovyMagJMS)以廣播到不同的客戶端。 (如Broadcasting to a subset of subscribers in Atmosphere)Grails Atmosphere Plugin廣播給不同的客戶端
客戶端連接網址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()
}
}
}
}