有没有办法用Java创建动态@ServerEndpoint地址?
例如,我有一个房间有没有办法用Java创建动态@ServerEndpoint地址?,java,websocket,server,endpoint,Java,Websocket,Server,Endpoint,例如,我有一个房间 public class Room { private int id; private Set<User> users; } 公共教室{ 私有int-id; 私人用户; } 所以我希望它成为我的websocket应用程序的端点。但是可能有很多房间,我希望每个房间都有自己的URI(例如,rooms/1、rooms/2等) 显然,@ServerEnpoint注释只允许常量。那么,有没有办法做到这一点呢?类似这样的事情: @ServerEndpoint(
public class Room {
private int id;
private Set<User> users;
}
公共教室{
私有int-id;
私人用户;
}
所以我希望它成为我的websocket应用程序的端点。但是可能有很多房间,我希望每个房间都有自己的URI(例如,rooms/1、rooms/2等)
显然,@ServerEnpoint注释只允许常量。那么,有没有办法做到这一点呢?类似这样的事情:
@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 )
您可以使用此per函数在同一控制器中映射具有不同变量的请求
@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
}
@RequestMapping(value=“/endpoint/{endpointVariable}”,method=RequestMethod.GET)
public ReturnDTO getReturnDTO(){
//在这里,可以访问变量endpointVariable
//根据我的经验,它总是一个整数,但我确信它是一个字符串
//可能。请使用调试器检查
}
我不确定这是否是我需要的东西,我的意思是我必须在运行时将端点与实例关联起来。例如,我创建了房间的新实例,它映射到必要的地址。我还需要服务器只向那些连接到此URI的客户端发送消息。如果我像你提供的那样做,我真的无法想象我怎么做,这取决于你有多少关系。如果你改变将打开的连接保存到将会话映射到相应的roomnumber的结构,也许你可以加快速度…比如对每个roomnumber使用单独的映射..问题是关于WebSocket而不是Spring MVC
@ServerEndpoint
是javax.websocket.server
package.True的注释。。。我提供了一些没有完全回答这个问题的东西。
@OnClose
public void onConnectionClose(Session session) {
openSessions.remove(session.getId());
}
@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
}