如何在Play Framework上将路径路由到Atmosphere中的ManagedService

时间:2016-12-08 13:27:50

标签: atmosphere playframework-2.5

我正在使用Play Framework中的氛围播放插件以及基本的氛围运行时。见https://github.com/Atmosphere/atmosphere-play

我尝试使用ManagedService注释设置一个类,如教程中所示,但无法弄清楚如何通过Play路由器文件映射路径并且无法使用找到其他人做过这件事。文档完全跳过了这一步,当我尝试通过大气客户端脚本连接到服务器时,我遇到了404错误。

示例代码:

@ManagedService(path = "/poll")
public class PostPoller {
  ...
}

客户端:

var socket = $.atmosphere;
var subSocket;
var transport = 'websocket';

// We are now ready to cut the request
var request = { url: '/poll',
    contentType : "application/json",
    trackMessageLength : true,
    shared : true,
    transport : transport ,
    fallbackTransport: 'long-polling'};

如何在Play路由器中设置到ManagedService的路由?

1 个答案:

答案 0 :(得分:0)

如果其他人正在寻找这个,我可以通过将以下内容添加到主应用程序控制器中来使其在Atmosphere-Play 2.3.0中运行:

import static org.atmosphere.play.AtmosphereCoordinator.instance;

public class Application extends Controller {

    @Inject
    public Application(ApplicationLifecycle lifecycle) {

        // replace PostPoller with the ManagedService class you are using
        instance.discover(PostPoller.class).ready();

        lifecycle.addStopHook(() -> {
            instance().shutdown();
            return CompletableFuture.completedFuture(null);
        });

        ...
    }

    ...
}

https://github.com/Atmosphere/atmosphere-play/issues/39找到答案。