首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Atmosphere:单个HttpConnection上的多个订阅

Atmosphere:单个HttpConnection上的多个订阅
EN

Stack Overflow用户
提问于 2012-11-30 10:20:36
回答 1查看 3.1K关注 0票数 6

我在我的Spring MVC应用程序中使用Atmosphere来促进推送,使用的是streaming传输。

在我的应用程序的整个生命周期中,客户端将订阅和取消订阅许多不同的主题。

Atmosphere似乎每个订阅都使用一个http连接--也就是说,每次调用$.atmosphere.subscribe(request)都会创建一个新连接。这很快就耗尽了从浏览器到大气服务器所允许的连接数量。

我不想每次都创建一个新的资源,而是希望能够在最初创建之后向广播公司添加和删除AtmosphereResource

但是,由于AtmosphereResource是入站请求的一对一表示,每次客户端向服务器发送请求时,它都会到达一个新的AtomsphereResource,这意味着我无法引用原始资源,并将其附加到主题的Broadcaster中。

我已经尝试在从原始subscribe()调用返回的资源上同时使用$.atmosphere.subscribe(request)和调用atmosphereResource.push(request)。然而,这并没有什么不同。

解决这个问题的正确方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-14 03:55:21

下面是我是如何让它工作的:

首先,当客户端进行初始连接时,请确保浏览器在调用suspend()之前接受特定于are的标头

代码语言:javascript
复制
@RequestMapping("/subscribe")
public ResponseEntity<HttpStatus> connect(AtmosphereResource resource)
{
    resource.getResponse().setHeader("Access-Control-Expose-Headers", ATMOSPHERE_TRACKING_ID + "," + X_CACHE_DATE);
    resource.suspend();
}

然后,当客户端发送额外的subscribe请求时,尽管它们来自不同的resource,但它们包含原始资源的ATMOPSHERE_TRACKING_ID。这使您可以通过resourceFactory查找它

代码语言:javascript
复制
@RequestMapping(value="/subscribe", method=RequestMethod.POST)
public ResponseEntity<HttpStatus> addSubscription(AtmosphereResource resource, @RequestParam("topic") String topic)
{
    String atmosphereId = resource.getResponse().getHeader(ATMOSPHERE_TRACKING_ID);
    if (atmosphereId == null || atmosphereId.isEmpty())
    {
        log.error("Cannot add subscription, as the atmosphere tracking ID was not found");
        return new ResponseEntity<HttpStatus>(HttpStatus.BAD_REQUEST);
    }
    AtmosphereResource originalResource = resourceFactory.find(atmosphereId);
    if (originalResource == null)
    {
        log.error("The provided Atmosphere tracking ID is not associated to a known resource");
        return new ResponseEntity<HttpStatus>(HttpStatus.BAD_REQUEST);
    }

    Broadcaster broadcaster = broadcasterFactory.lookup(topic, true);
    broadcaster.addAtmosphereResource(originalResource);
    log.info("Added subscription to {} for atmosphere resource {}",topic, atmosphereId);

    return getOkResponse();
}
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13638108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档