例如,我有一个房间
public class Room {
private int id;
private Set<User> users;
}
所以我希望它成为我的websocket应用程序的端点。但是可能有很多房间,我希望每个房间都有自己的URI(例如,房间/ 1,房间/ 2等)。
显然,@ ServerEnpoint annotaion只允许常量。那么,有什么方法可以做到吗?
答案 0 :(得分:3)
这样的事情:
AF_UNIX
仅向特定房间号/客户发送消息:
@ServerEndpoint(value = "/rooms/{roomnumber}")
public class....
static Map<String, Session> openSessions = ...
@OnOpen
public void onConnectionOpen(final Session session, @PathParam("roomnumber") final String roomnumber,
...
//store roomnumber in session
session.getUserProperties().put("roomnumber", roomnumber);
openSessions.put( String.valueOf(session.getId()), session )
当客户断开连接时:
// check if session corresponds to the roomnumber
for (Map.Entry<String, Session> entry : openSessions.entrySet()) {
Session s = entry.getValue();
if (s.isOpen() && s.getUserProperties().get("roomnumber").equals(roomnumber_you_want_to_address)) {
...
答案 1 :(得分:-2)
您可以使用此函数在同一个控制器中映射具有不同变量的请求
@RequestMapping(value = "/endpoint/{endpointVariable}", method = RequestMethod.GET)
public ReturnDTO getReturnDTO(<params>){
// Here the variable, endpointVariable, will be accessible
// In my experiences its always been an integer, but I'm sure a string
// would be possible. check with debugger
}