泽西大气广播没有交付

时间:2014-04-09 00:58:33

标签: java websocket jersey tomcat7 atmosphere

我是Atmosphere的新手,所以我一定要错过一些简单的东西。我做了一个样本,可以让@Broadcast工作,但不能使用BroadcasterFactory。我可以跟踪onMessage(通过监听器)但我没有看到onBroadcast发生,除非指定了@Broadcast。另外,除非我指定@Broadcast,否则我不会看到传递给客户端的消息。我的WebSocket代码非常基础:

@Path("/chat")
@AtmosphereService(
        interceptors = {AtmosphereResourceLifecycleInterceptor.class, 
                        TrackMessageSizeInterceptor.class/*,
                        BroadcastOnPostAtmosphereInterceptor.class*/}//,
        )
public class HelloWorldWS {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public SuspendResponse<String> connect(@Context AtmosphereResource res) 
    {
        // eventually this will be done via subscription in the post area
        BroadcasterFactory.getDefault().get().addAtmosphereResource(res);

        return new SuspendResponse.SuspendResponseBuilder<String>()
                //.broadcaster(BroadcasterFactory.getDefault().get())
                .outputComments(true)
                .addListener(new ResourceListener())
                .build();
    }

    //@Broadcast(writeEntity = false)
    @POST
    public void broadcast(String message) {
        final String msg = String.format("{'author':'fdsa','message':'%s'}", "Override proof");
        // Send to all
        BroadcasterFactory.getDefault().get().broadcast(msg);
        // No Really, send to all
        Collection<Broadcaster> broadcasters = BroadcasterFactory.getDefault().lookupAll();
        for (Broadcaster b : broadcasters)
        {
            System.out.println("Broadcaster: " + b.toString() + "; Count: " + b.getAtmosphereResources().size());
            for (AtmosphereResource r : b.getAtmosphereResources())
            {
                System.out.println("Notifying Resource: " + r.toString());
                r.write(msg);
            }
        }
    }
}

当我启用@ Broadcast / BroadcastOnPostAtmosphereInterceptor时,消息被中继到客户端(我的代码在&#34;广播&#34;函数什么都不做)。当我注释掉@ Broadcast / BroadcastOnPostAtmosphereInterceptor时,我希望我的广播代码能够传递信息。

使用@Broadcast我会显示以下ResourceListener回调:onPreSuspend,onSuspend,onConnect,onMessage,onBroadcast(客户端获取)

当@Broadcast注释掉时,我会显示以下ResourceListener回调:onPreSuspend,onSuspend,onConnect,onMessage(客户端永远不会收到消息)

我做错了什么?

我使用气氛2.1.2与Jersey 1.17

1 个答案:

答案 0 :(得分:-1)

摆脱AtmosphereResourceLifecycleInterceptor是否使用SuspendResponse API,因为它可能会发生冲突。