求助, 如何在一个被@ServerEndpoint注释的类中获取项目的request?

sasgsc 2014-07-23 11:58:57
最近项目需要做一个websocket.

@ServerEndpoint(value = "/websocket/chat", configurator = SpringConfigurator.class)
public class WSRightChatting{

@Autowired
private HttpServletRequest request;

private static final Set<WSRightChatting> connections = new CopyOnWriteArraySet<WSRightChatting>();
private Session session;
private static final AtomicInteger connectionIds = new AtomicInteger(0);
private String nickname="guest";

public WSRightChatting() {
nickname = nickname + connectionIds.getAndIncrement();
}


@OnOpen
public void start(Session session)
{
this.session = session;
WSRightChatting.connections.add(this);

Admin ad=(Admin) request.getSession().getAttribute("SESSION_KEY_OP");

System.out.println(ad.getAid());
}
}

代码是这样的, 但是无法获取request.

请问有什么办法能获取request参数吗? 谢谢
...全文
417 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
给stat构造两个参数。

@OnOpen
    public void start(Session session, EndpointConfig config) {
        this.session = session;//这个session是websocket
         (HttpSession) config.getUserProperties()
                .get(HttpSession.class.getName());//这里的HttpSession是javax.servlet.http下的。
    }

不过你要重写configurator = SpringConfigurator.class了,自己写一个,继承ServerEndpointConfig.Configurator,并重写

@Override
    public void modifyHandshake(ServerEndpointConfig config, 
                                HandshakeRequest request, 
                                HandshakeResponse response)
    {
        HttpSession httpSession = (HttpSession)request.getHttpSession();
        config.getUserProperties().put(HttpSession.class.getName(),httpSession);
    }
握手阶段用的是http协议,可以在这一阶段把HandshakeRequest强转为HttpSession。然后放到config.getUserProperties()里,在stat函数里获取就可以了。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧