JavaScript客户端未收到大气广播,正常的写工作正常

时间:2018-11-05 15:36:30

标签: spring spring-boot atmosphere

我在Spring应用程序中有一个运行服务器端的Atmosphere。 问题是我可以接收正常消息,但不能接收广播。

已通过配置将气氛“注入”到Spring引导中。

@Bean
public AtmosphereServlet atmosphereServletNative() {
    return new AtmosphereServlet();
}

@Bean
public ServletRegistrationBean atmosphereServlet() {
    return new ServletRegistrationBean(
            atmosphereServletNative(), "/atmosphere/*"
    );
}

@Bean
public AtmosphereFramework atmosphereFramework() {
    return atmosphereServletNative().framework();
}

@Bean
public AsyncNotificationService asyncNotificationService() {
    return new AsyncNotificationService();
}

这就是将其初始化为Spring Boot的方式,AtmosphereServlet处理/atmosphere/*上的请求,而Spring处理所有其他请求。

现在,我想向一些客户广播事件。我有一个处理程序。

@AtmosphereHandlerService(path = "/atmosphere/webAsync", interceptors = {
        AtmosphereResourceLifecycleInterceptor.class,
        BroadcastOnPostAtmosphereInterceptor.class,
        HeartbeatInterceptor.class,
})
public class AsyncNotificationService implements AtmosphereHandler  {

    Logger logger = LoggerFactory.getLogger(AsyncNotificationService.class);

    private @Autowired AtmosphereFramework atmosphereFramework;

    public void notifyReservationChanged() {
        atmosphereFramework.getBroadcasterFactory().lookupAll().forEach((broadcaster) -> {
            broadcaster.broadcast("RESERVATION_UPDATE");
        });
    }


    @Override
    public void onRequest(AtmosphereResource atmosphereResource) throws IOException {
        Broadcaster broadcaster = atmosphereFramework.getBroadcasterFactory().lookup("webClientBroadcaster", true);
        broadcaster.addAtmosphereResource(atmosphereResource);
        atmosphereResource.setBroadcaster(broadcaster);
        atmosphereResource.suspend();
    }

    @Override
    public void onStateChange(AtmosphereResourceEvent atmosphereResourceEvent) throws IOException {
        logger.debug(atmosphereResourceEvent.toString());
    }

    @Override
    public void destroy() {

    }
}

这样,客户端不会收到广播。但是,当我在notifyReservationChanged中这样做时,我确实会收到事件。

public void notifyReservationChanged() {
    atmosphereFramework.getBroadcasterFactory().lookupAll().forEach((broadcaster) -> {
        broadcaster.getAtmosphereResources().forEach((res) -> {
            res.write("UPDATE_RESERVATION");
        });
    });
}

可能是什么原因造成的?配置错误?

0 个答案:

没有答案