开发者

Atmosphere Grails single user broadcast

I'm creating a Grails application which makes use of the Atmosphere plugin to push data to the browser. However I'm having trouble in creating a broadcast channel to a single user (the session's user). My code is has follows:

Service:

static atmosphere = [mapping: '/atmosphere/recommend']

def onRequest = { event ->
    def request = event.request
    def response = event.respo开发者_Go百科nse

    event.suspend()     

    def broadcaster = event.broadcaster
    request.session.broadcaster = broadcaster
    broadcaster.broadcasterConfig.addFilter(new XSSHtmlFilter())     
}

def onStateChange = { event ->

    if (!event.message) return

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

Controller:

def builder = new JSONBuilder()
    def jsonResult = builder.build{
                            artist = artistInstance
                            location = {
                                lat = eventInstance.location.lat
                                lng = eventInstance.location.lng
                            }
                        }

session.broadcaster.broadcast(jsonResult)

This solution broadcasts the jsonResult to every user. What I want to achieve is to broadcast only for the current user. Any ideas? If you need more details just let me know.

Thanks


I thinks you can use session to share the onRequest's event.

request.session[userId]=event

then in controller :

broadcast(jsonResult,session[userId])


When you define your events in grails, you can filter out cients by using a browserFilter closure.


Hey you can make use of the uuid which is assigned to each Atmosphere Resource. To retrieve the suspended uuid based on an Atmosphere Request you can do:
String suspendedUUID = (String)req.getAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID);


You can pass the session

session.broadcaster.broadcast(jsonResult, AtmosphereResource)

See this API.

(I am Atmosphere's creator).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜