相对于HTTP这种非持久的协议来说,Websocket是一个持久化的协议(注H5新特性,java7开始才有的JSR-356:Java API for WebSocket规范)。
已处理并发登录为例:
使用websocket open拿到httpsession:
package com.chinasofti.config;
import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerEndpointConfig.Configurator;
public class HttpSessionConfigurator extends Configurator{
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
HttpSession httpSession = (HttpSession) request.getHttpSession();
if(httpSession != null){
sec.getUserProperties().put(HttpSession.class.getName(), httpSession);
}
}
}
从httpsession中获取登录账户,并添加到websocket中,登录时查看httpsession中是否已存在账户:
package com.chinasofti.server;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpSession;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import com.alibaba.fastjson.JSON;
import com.chinasofti.config.HttpSessionConfigurator;
import com.chinasofti.model.Account;
import com.chinasofti.pageModel.base.Msg;
@ServerEndpoint(value = "/ws/websocket", configurator = HttpSessionConfigurator.class)
public class WebSocketServer {
private Account account;
private HttpSession httpSession;
public static Map<String, Session> sessionMap = new HashMap<String, Session>();
public static Map<String, HttpSession> httpSessionMap = new HashMap<String, HttpSession>();
@OnOpen
public void start(Session session, EndpointConfig config) {
httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
account = (Account) httpSession.getAttribute("account");
httpSessionMap.put(account.getAccountid(), httpSession);
sessionMap.put(account.getAccountid(), session);
}
@OnClose
public void end() {
try {
this.sessionInvalidate();
} catch (Exception e) {
} finally {
httpSessionMap.remove(account.getAccountid());
}
sessionMap.remove(account.getAccountid());
}
@OnError
public void onError(Throwable t) throws Throwable {
System.out.println("异常:" + t.getMessage());
}
public static void sendMessage(String userid, Msg msg) {
Session session = sessionMap.get(userid);
try {
session.getBasicRemote().sendText(JSON.toJSONString(msg));
} catch (IOException e) {
System.out.println("sendMessage异常:" + e.getMessage());
}
}
public static void sendToAllUsersMessage(Msg msg) {
Set<String> set = sessionMap.keySet();
for (String key : set) {
Session session = sessionMap.get(key);
try {
session.getBasicRemote().sendText(JSON.toJSONString(msg));
} catch (IOException e) {
System.out.println("sendToAllUsersMessage异常:" + e.getMessage());
}
}
}
private void sessionInvalidate() throws Exception {
new Thread() {
@Override
public void run() {
try {
Thread.sleep(3000);
if (httpSessionMap.get(account.getAccountid()) == null) {
httpSession.invalidate();
}
} catch (Exception e) {
}
}
}.start();
}
// 判断某个账户是否在线
public static boolean isOnline(String userid) {
if (httpSessionMap.get(userid) != null) {
return true;
}
return false;
}
}
HttpSession mapSession=WebSocketServer.httpSessionMap.get(account.getAccountid());