如何在Jetty 9中访问带注释的@WebSocket类中的HttpSession对象?
我发现了如何使用@ServerEndpoint注释来实现它,如以下所示:HttpSession from @ServerEndpoint
使用@WebSocket注释,就像在下面的类中一样,我怎么做呢?
@WebSocket
public class AuctionWebSocket {
// NEED TO ACCESS HttpSession OBJECT INSIDE THESE METHODS:
@OnWebSocketConnect
public void onConnect(Session session) {
System.out.println("onConnect...");
}
@OnWebSocketMessage
public void onMessage(String message) {
System.out.println("Message: " + message);
}
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("onClose...");
}
@OnWebSocketError
public void onError(Throwable t) {
System.out.println("onError...");
}
}在方法onConnect(Session session)中,我尝试调用session.getUpgradeRequest().getSession(),它总是返回null。
为了获得信息,下面是我如何启动嵌入式Jetty 9:
public class Main {
public static void main(String[] args) throws Exception {
String webPort = System.getenv("PORT");
if (webPort == null || webPort.isEmpty()) {
webPort = "8080";
}
Server server = new Server(Integer.parseInt(webPort));
ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration");
WebAppContext wac = new WebAppContext();
String webappDirLocation = "./src/main/webapp/";
wac.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/classes/.*");
wac.setDescriptor(webappDirLocation + "/WEB-INF/web.xml");
wac.setBaseResource(new ResourceCollection(new String[]{webappDirLocation, "./target"}));
wac.setResourceAlias("/WEB-INF/classes/", "/classes/");
wac.setContextPath("/");
wac.setParentLoaderPriority(true);
/*
* WebSocket handler.
*/
WebSocketHandler wsh = new WebSocketHandler() {
@Override
public void configure(WebSocketServletFactory wssf) {
wssf.register(AuctionWebSocket.class);
}
};
ContextHandler wsc = new ContextHandler();
wsc.setContextPath("/auction-notifications");
wsc.setHandler(wsh);
ContextHandlerCollection chc = new ContextHandlerCollection();
chc.setHandlers(new Handler[]{wac, wsc});
server.setHandler(chc);
server.start();
server.join();
}
}如果你需要更多的信息,请告诉我。
任何帮助都将不胜感激。
提前谢谢。
发布于 2014-12-29 19:31:20
您将需要使用WebSocketCreator概念。
首先,在您在WebSocketCreator中配置的WebSocketServletFactory中设置您选择的WebSocketServletFactory。
public class MySessionSocketServlet extends WebSocketServlet
{
@Override
public void configure(WebSocketServletFactory factory)
{
factory.getPolicy().setIdleTimeout(30000);
factory.setCreator(new MySessionSocketCreator());
}
}接下来,您将希望在升级期间获取HttpSession,并将其传递到正在创建的WebSocket对象中。
public class MySessionSocketCreator implements WebSocketCreator
{
@Override
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
{
HttpSession httpSession = req.getSession();
return new MySessionSocket(httpSession);
}
}最后,只需在您自己的HttpSession中跟踪这个WebSocket。
@WebSocket
public class MySessionSocket
{
private HttpSession httpSession;
private Session wsSession;
public MySessionSocket(HttpSession httpSession)
{
this.httpSession = httpSession;
}
@OnWebSocketConnect
public void onOpen(Session wsSession)
{
this.wsSession = wsSession;
}
}注意:HttpSession可以过期,并在WebSocket处于活动状态时被清除和清除。此外,此时的HttpSession内容不能保证与其他web操作的更改保持同步(这主要取决于您在服务器端使用的会话存储/缓存技术)。
还有一个注意事项:抵制在套接字实例中存储/跟踪ServletUpgradeRequest对象的冲动,因为这个对象是由Jetty固有回收和清理的。
https://stackoverflow.com/questions/27671162
复制相似问题