Ste*_*e N 4 java jersey guice atmosphere
我有一个工作的Jersey/Atmosphere/Guice应用程序,它有两个Atmosphere Resources.第一个是示例聊天应用程序的克隆:
@Path("/chat")
@AtmosphereService(broadcaster = JerseyBroadcaster.class, path = "/chat")
public class ChatResource {
@Suspend(contentType = "application/json")
@GET
public String suspend() {
return "";
}
@Broadcast(writeEntity = false)
@POST
@Produces("application/json")
public Response broadcast(Message message) {
return new Response(message.author, message.message);
}
}
Run Code Online (Sandbox Code Playgroud)
第二个是测试通知资源,它将发送服务器端事件:
@Path("/notifications")
@AtmosphereService(broadcaster = JerseyBroadcaster.class, path = "/notifications")
public class NotificationsResource {
@Suspend(contentType = "application/json")
@GET
public String suspend() {
return "";
}
}
Run Code Online (Sandbox Code Playgroud)
一切都正确连线,工作正常.但是为了让我发送服务器端事件我发出:
MetaBroadcaster.getDefault().broadcastTo("/*", new Response(...));
Run Code Online (Sandbox Code Playgroud)
显然,这将向两个资源发送广播消息.我想要做的是仅将服务器端事件发送到通知资源:
MetaBroadcaster.getDefault().broadcastTo("/notifications", new NotificationResponse(...));
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.我总是收到以下错误:
org.atmosphere.cpr.MetaBroadcaster - No Broadcaster matches /notifications.
Run Code Online (Sandbox Code Playgroud)
那是因为只有一家广播公司注册; JerseyBroadcaster on/*.
问题是:如何使这两个资源具有不同的ID /名称的不同广播公司?
小智 7
在资源中,使用您想要的通道挂起(查找('true'参数)会强制创建通道(如果它不存在):
@Suspend( contentType = MediaType.APPLICATION_JSON, period = MAX_SUSPEND_MSEC )
@GET
public Broadcastable suspend( @Context final BroadcasterFactory factory )
{
return new Broadcastable( factory.lookup( MY_CHANNEL, true ) );
}
Run Code Online (Sandbox Code Playgroud)
在其他代码中,可以在任何地方,广播到该频道:
Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup( MY_CHANNEL );
if( broadcaster != null ) {
broadcaster.broadcast( message );
}
Run Code Online (Sandbox Code Playgroud)
如果您要从资源方法进行广播,则可以对其进行注释(如ChatResource的broadcast()方法中所示).
| 归档时间: |
|
| 查看次数: |
2962 次 |
| 最近记录: |