websocket存储session(适合聊天,转发,⼴播,服务器主动
发送消息)
此篇⽂章主要分享的是websocket存储session信息,在⽹上经常看到有websocket的例⼦,此篇⽂章只是将websocket中的session提取并存储起来,便于⽤户之间的互动与查,其实就是解耦合
此实现采⽤的是springboot的spring封装的websocket框架,原有的tomcat其实也可以完成
⾸先,spring框架封装的websocket有⼏种对应
onopen对应afterConnectionEstablished
onclose对应afterConnectionClosed
onMessage对应handleTextMessage
以下是处理消息类
@Component
public class DoubleTeachWSHandler extends TextWebSocketHandler {
private static final Logger LOG = LoggerFactory
.getLogger(DoubleTeachWSHandler.class);
/**
* 创建连接
*
* @param session ⽤户session
* @throws Exception
* @version v1.0
* @autho 张瑞
* @date 2017年12⽉18⽇下午3:48:34
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) {
System.out.println("afterConnectionEstablished");
try {
//获取⽤户信息
SocketUser socketUser = DoubleTeachUserService
.getSocketUser(session);
if (null == socketUser) {
return;
}
//加⼊redis
DoubleTeachUserService.addRedis(socketUser);
//创建房间并加⼊session
} catch (Exception e) {
<(e.getMessage(), e);
}
}
/**
* 关闭链接
*
* @param session ⽤户session
* @param status 状态
* @throws Exception
* @version v1.0
* @autho 张瑞
* @date 2017年12⽉18⽇下午3:49:04
*/
@Override
@Override
public void afterConnectionClosed(WebSocketSession session,
websocket和socketCloseStatus status) {
System.out.println("afterConnectionClosed");
try {
//获取⽤户信息
SocketUser socketUser = DoubleTeachUserService
.getSocketUser(session);
if (null == socketUser) {
return;
}
//删除缓存
DoubleTeachUserService.deleteRedis(socketUser);
//退出房间
/
/是否删除房间
DoubleTeachSocketRoom.RoomID());
} catch (Exception e) {
<(e.getMessage(), e);
}
}
/**
* 处理消息
*
* @param session ⽤户session
* @param message message消息
* @throws Exception
* @version v1.0
* @autho 张瑞
* @date 2017年12⽉18⽇下午3:55:18
*/
@Override
protected void handleTextMessage(WebSocketSession session,
TextMessage message) {
try {
//获取⽤户信息
SocketUser socketUser = DoubleTeachUserService
.
getSocketUser(session);
if (null == socketUser) {
return;
}
String str = Payload();
if (StringUtils.isNull(str)) {
//空消息不做操作
return;
}
//进⾏解析
SocketMessage socketMessage = JSONUtils.parse2Bean(str,
SocketMessage.class);
DoubleTeachSendMsg.mainSendMessage(socketMessage,socketUser);
} catch (Exception e) {
<(e.getMessage(), e);
}
}
}
此处是重点,将session统⼀存储到此类中,⽤map存储起来,并⾃定义key值,value即存储websocket使⽤的session
@Component
public class DoubleTeachSocketRoom {
private static final Logger LOG = LoggerFactory
private static final Logger LOG = LoggerFactory
.getLogger(DoubleTeachSocketRoom.class);
//整体管理room的map
private static ConcurrentHashMap<String, SocketRoom> roomMap = new ConcurrentHashMap<String, SocketRoom>( 10000);
/**
* 获取⽤户session
*
* @param socketUser ⽤户实体
* @return ⽤户session
* @version v1.0
* @author 张瑞
* @date 2018年1⽉24⽇上午10:51:11
*/
public static WebSocketSession getUserSession(SocketUser socketUser) {
try {
if (null == socketUser) {
return null;
}
SocketRoom socketRoom = (RoomID());
if (null == socketRoom) {
return null;
}
WebSocketSession session = SessionMap()
.SessionID());
return session;
} catch (Exception e) {
<(e.getMessage(), e);
return null;
}
}
/**
* 移除⽤户session
*
* @param socketUser ⽤户实体
* @version v1.0
* @author 张瑞
* @date 2018年1⽉24⽇上午10:51:40
*/
public static void removeUserSession(SocketUser socketUser) {
try {
if (null == socketUser) {
return;
}
SocketRoom socketRoom = (RoomID());
if (null == socketRoom) {
//不需要做操作
return;
}
} catch (Exception e) {
<(e.getMessage(), e);
}
}
/**
* 获取当前房间⽤户总体
*
* @param roomID 房间ID
* @return 房间实体
* @version v1.0
* @author 张瑞
* @date 2018年1⽉24⽇上午10:52:24
* @date 2018年1⽉24⽇上午10:52:24
*/
public static SocketRoom getSocketRoom(String roomID) {
try {
if (StringUtils.isNull(roomID)) {
return null;
}
SocketRoom socketRoom = (roomID);
return socketRoom;
} catch (Exception e) {
<(e.getMessage(), e);
return null;
}
}
/**
* 添加⽤户进⼊房间并是否创建房间
*
* @param session ⽤户session
* @param socketUser ⽤户
* @version v1.0
* @author 张瑞
* @date 2017年12⽉19⽇下午3:44:27
*/
public static synchronized void creatRoom(WebSocketSession session, SocketUser socketUser) {
SocketRoom socketRoom = (RoomID()); if (null == socketRoom) {
socketRoom = new SocketRoom();
roomMap.RoomID(), socketRoom);
}
}
/**
* 删除房间
*
* @param roomID
* @version v1.0
* @author 张瑞
* @date 2017年12⽉18⽇下午3:54:13
*/
public static synchronized void deleteRoom(String roomID) {
SocketRoom socketRoom = (roomID);
if (null == socketRoom) {
return;
}
if (SessionMap().size() == 0) {
}
}
}
聊天室专⽤的存储表
public class SocketRoom implements Serializable {
private ConcurrentHashMap<String, WebSocketSession> sessionMap;
public SocketRoom() {
sessionMap = new ConcurrentHashMap<String, WebSocketSession>(10000); }
public SocketRoom(int size) {
sessionMap = new ConcurrentHashMap<String, WebSocketSession>(size); }
public ConcurrentHashMap<String, WebSocketSession> getSessionMap() {
return sessionMap;
}
public void setSessionMap(
ConcurrentHashMap<String, WebSocketSession> sessionMap) {
this.sessionMap = sessionMap;
}
}
⽤户存储表
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论