发送消息到所有连接的网络套接字客户端
问题描述:
我正在使用Jooby的MVC路由API。我还建立了一个websocket,有几个客户端连接到这个websocket。我试图做的是每当服务器收到一个特定的http请求时,向所有连接的websocket客户端发送消息。这是我的路线方法如下:发送消息到所有连接的网络套接字客户端
@Path("/player")
@Produces("application/json")
public class PlayerRoute {
@POST
public Result newPlayer(Request req, @Body Player player) {
//do some process here
//this is what I'm trying to achieve..
allWebsocketSessions.foreach(session ->
session.send("a new player has been created")
);
return Results.ok();
}
}
我读过jooby的文档,但无法弄清楚如何去做。
在此先感谢。
答
这似乎为“会话,你可以称之为‘设置’和‘获取’仅方法。方法‘发送’就可以喊出了‘回应’。
您可以订阅所有用户的主题,将消息发送到这个话题。 – user1516873