首先在PC端用手机模拟器是没有问题的。
然后第一次安装到手机上使用时,new WebSocket("ws://url");还报了302状态。因为后台是shiro框架,我直接把socket服务取消登陆拦截了。但是还是会在登陆验证时保存session对象。
@Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
Pager pager = new Pager();
String username = (String) token.getPrincipal();
// 获取用户
User user = userService.getUserByUsername(username);
//将用户信息添加到session
Session session = subject.getSession();
session.setAttribute("user",user);
System.out.println(session.getId());
.........
然后问题又来了,在WebSocket服务类中获取到session后没有user对象,并且查看sessionId也不一致
@OnOpen
public void onOpen(Session session,EndpointConfig config) {
httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
//TODO:PDA访问时Attributes中没有user对象
User user = (User)httpSession.getAttribute("user");
this.session = session;
this.userId = user.getId();
System.out.println("有用户连接了:"+userId);
webSocketMap.put(user.getId(), this);
........