spring websocket 模拟发送消息
2016-12-08 14:47
323 查看
实现以下功能:
1、各个客户端之间的消息互发;
2、客户端向后台发送消息;
3、后台向客户端发送消息
注:完整源代码下载地址 http://download.csdn.net/detail/u010994277/9705588
代码如下:
1)、配置处理器
/**
* WebScoket配置处理器
*
* @author www
* @Date 2016年12月8日
*/
@Component
@EnableWebSocket
@Configuration
@EnableWebMvc
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new MyWebSocketHandler(), "/portfolio2")// 添加一个处理器还有定义处理器的处理路径
.addInterceptors(new MyHandShakeInterceptor());
registry.addHandler(new MyWebSocketHandler(), "/sockjs/portfolio2")// 添加一个处理器还有定义处理器的处理路径
.addInterceptors(new MyHandShakeInterceptor()).withSockJS();
}
}2、配置拦截器
/**
* Socket建立连接(握手)和断开
*
* @author www
* @Date 2016年12月8日
*/
public class MyHandShakeInterceptor implements HandshakeInterceptor {
/**
* 握手之前,若返回false,则不建立链接
*/
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Map<String, Object> attributes) throws Exception {
// TODO Auto-generated method stub
System.out.println("beforeHandshake-->");
if (request instanceof ServletServerHttpRequest) {
try {
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
HttpSession session = servletRequest.getServletRequest().getSession(false);
if (session != null) {
String userId = (String) session.getAttribute(Constant.SESSION_USER_ID);
if(userId==null||userId.equals("")){
throw new Exception("用户ID为空");
}
attributes.put(Constant.SESSION_USER_ID,userId);
}else{
throw new Exception("session为空");
}
} catch (Exception e) {
attributes.put(Constant.SESSION_USER_ID,"test-user");
}
}
return true;
}
/**
* 握手之后
*/
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Exception exception) {
// TODO Auto-generated method stub
System.out.println("afterHandshake-->");
}
3、socket处理器
/**
* Socket处理器
*
* @author www
* @Date 2016年12月8日
*/
@Component
public class MyWebSocketHandler implements WebSocketHandler {
public static final Map<String, WebSocketSession> userSocketSessionMap;
static {
userSocketSessionMap = new HashMap<String, WebSocketSession>();
}
/**
* webscoket建立好链接之后的处理函数
*
* @param session
* 当前websocket的会话id,打开一个websocket通过都会生成唯一的一个会话,
* 可以通过该id进行发送消息到浏览器客户端
*/
@Override
public void afterConnectionEstablished(WebSocketSession session)
throws Exception {
// TODO Auto-generated method stub
String uid = (String) session.getAttributes().get(Constant.SESSION_USER_ID);
System.out.println("用户ID:" + uid + ";已建立连接");
// 每个链接来的客户端都把WebSocketSession保存进来
if (userSocketSessionMap.get(uid) == null) {
userSocketSessionMap.put(uid, session);
}else{
userSocketSessionMap.remove(uid);
userSocketSessionMap.put(uid, session);
}
}
/**
* 客户端发送服务器的消息时,的处理函数,在这里收到消息之后可以分发消息
*/
@Override
public void handleMessage(WebSocketSession session,
WebSocketMessage<?> message) throws Exception {
// TODO Auto-generated method stub
Iterator<Map.Entry<String, WebSocketSession>> entries = userSocketSessionMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, WebSocketSession> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
if(entry.getKey().equals(session.getAttributes().get("userId"))){
String msg = "后台已收到信息[" + message.getPayload().toString()+ "];用户ID为:" + entry.getKey();
System.out.println(msg);
message = new TextMessage(msg);
entry.getValue().sendMessage(message);
}
}
}
/**
* 消息传输过程中出现的异常处理函数
*/
@Override
public void handleTransportError(WebSocketSession session,
Throwable exception) throws Exception {
// TODO Auto-generated method stub
String userId=(String) session.getAttributes().get("userId");
userSocketSessionMap.remove(userId);
}
/**
* websocket链接关闭的回调
*/
@Override
public void afterConnectionClosed(WebSocketSession session,
CloseStatus closeStatus) throws Exception {
// TODO Auto-generated method stub
String userId=(String) session.getAttributes().get("userId");
userSocketSessionMap.remove(userId);
}
/**
* 是否支持处理拆分消息,返回true返回拆分消息
*/
@Override
public boolean supportsPartialMessages() {
// TODO Auto-generated method stub
return false;
}
/**
* 给所有在线用户发送消息
*
* @param message
* @throws IOException
*/
public void broadcast(final String msg) throws IOException {
final TextMessage message=new TextMessage(msg);
Iterator<Entry<String, WebSocketSession>> it = userSocketSessionMap.entrySet().iterator();
// 多线程群发
while (it.hasNext()) {
final Entry<String, WebSocketSession> entry = it.next();
if (entry.getValue().isOpen()) {
// entry.getValue().sendMessage(message);
new Thread(new Runnable() {
public void run() {
try {
if (entry.getValue().isOpen()) {
entry.getValue().sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
}
/**
* 给某个用户发送消息
*
* @param userName
* @param message
* @throws IOException
*/
public void sendMessageToUser(String uid, String msg)
throws IOException {
TextMessage message=new TextMessage(msg);
WebSocketSession session = userSocketSessionMap.get(uid);
if (session != null && session.isOpen()) {
session.sendMessage(message);
}
}
1、各个客户端之间的消息互发;
2、客户端向后台发送消息;
3、后台向客户端发送消息
注:完整源代码下载地址 http://download.csdn.net/detail/u010994277/9705588
代码如下:
1)、配置处理器
/**
* WebScoket配置处理器
*
* @author www
* @Date 2016年12月8日
*/
@Component
@EnableWebSocket
@Configuration
@EnableWebMvc
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new MyWebSocketHandler(), "/portfolio2")// 添加一个处理器还有定义处理器的处理路径
.addInterceptors(new MyHandShakeInterceptor());
registry.addHandler(new MyWebSocketHandler(), "/sockjs/portfolio2")// 添加一个处理器还有定义处理器的处理路径
.addInterceptors(new MyHandShakeInterceptor()).withSockJS();
}
}2、配置拦截器
/**
* Socket建立连接(握手)和断开
*
* @author www
* @Date 2016年12月8日
*/
public class MyHandShakeInterceptor implements HandshakeInterceptor {
/**
* 握手之前,若返回false,则不建立链接
*/
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Map<String, Object> attributes) throws Exception {
// TODO Auto-generated method stub
System.out.println("beforeHandshake-->");
if (request instanceof ServletServerHttpRequest) {
try {
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
HttpSession session = servletRequest.getServletRequest().getSession(false);
if (session != null) {
String userId = (String) session.getAttribute(Constant.SESSION_USER_ID);
if(userId==null||userId.equals("")){
throw new Exception("用户ID为空");
}
attributes.put(Constant.SESSION_USER_ID,userId);
}else{
throw new Exception("session为空");
}
} catch (Exception e) {
attributes.put(Constant.SESSION_USER_ID,"test-user");
}
}
return true;
}
/**
* 握手之后
*/
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Exception exception) {
// TODO Auto-generated method stub
System.out.println("afterHandshake-->");
}
3、socket处理器
/**
* Socket处理器
*
* @author www
* @Date 2016年12月8日
*/
@Component
public class MyWebSocketHandler implements WebSocketHandler {
public static final Map<String, WebSocketSession> userSocketSessionMap;
static {
userSocketSessionMap = new HashMap<String, WebSocketSession>();
}
/**
* webscoket建立好链接之后的处理函数
*
* @param session
* 当前websocket的会话id,打开一个websocket通过都会生成唯一的一个会话,
* 可以通过该id进行发送消息到浏览器客户端
*/
@Override
public void afterConnectionEstablished(WebSocketSession session)
throws Exception {
// TODO Auto-generated method stub
String uid = (String) session.getAttributes().get(Constant.SESSION_USER_ID);
System.out.println("用户ID:" + uid + ";已建立连接");
// 每个链接来的客户端都把WebSocketSession保存进来
if (userSocketSessionMap.get(uid) == null) {
userSocketSessionMap.put(uid, session);
}else{
userSocketSessionMap.remove(uid);
userSocketSessionMap.put(uid, session);
}
}
/**
* 客户端发送服务器的消息时,的处理函数,在这里收到消息之后可以分发消息
*/
@Override
public void handleMessage(WebSocketSession session,
WebSocketMessage<?> message) throws Exception {
// TODO Auto-generated method stub
Iterator<Map.Entry<String, WebSocketSession>> entries = userSocketSessionMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, WebSocketSession> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
if(entry.getKey().equals(session.getAttributes().get("userId"))){
String msg = "后台已收到信息[" + message.getPayload().toString()+ "];用户ID为:" + entry.getKey();
System.out.println(msg);
message = new TextMessage(msg);
entry.getValue().sendMessage(message);
}
}
}
/**
* 消息传输过程中出现的异常处理函数
*/
@Override
public void handleTransportError(WebSocketSession session,
Throwable exception) throws Exception {
// TODO Auto-generated method stub
String userId=(String) session.getAttributes().get("userId");
userSocketSessionMap.remove(userId);
}
/**
* websocket链接关闭的回调
*/
@Override
public void afterConnectionClosed(WebSocketSession session,
CloseStatus closeStatus) throws Exception {
// TODO Auto-generated method stub
String userId=(String) session.getAttributes().get("userId");
userSocketSessionMap.remove(userId);
}
/**
* 是否支持处理拆分消息,返回true返回拆分消息
*/
@Override
public boolean supportsPartialMessages() {
// TODO Auto-generated method stub
return false;
}
/**
* 给所有在线用户发送消息
*
* @param message
* @throws IOException
*/
public void broadcast(final String msg) throws IOException {
final TextMessage message=new TextMessage(msg);
Iterator<Entry<String, WebSocketSession>> it = userSocketSessionMap.entrySet().iterator();
// 多线程群发
while (it.hasNext()) {
final Entry<String, WebSocketSession> entry = it.next();
if (entry.getValue().isOpen()) {
// entry.getValue().sendMessage(message);
new Thread(new Runnable() {
public void run() {
try {
if (entry.getValue().isOpen()) {
entry.getValue().sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
}
/**
* 给某个用户发送消息
*
* @param userName
* @param message
* @throws IOException
*/
public void sendMessageToUser(String uid, String msg)
throws IOException {
TextMessage message=new TextMessage(msg);
WebSocketSession session = userSocketSessionMap.get(uid);
if (session != null && session.isOpen()) {
session.sendMessage(message);
}
}
相关文章推荐
- SpringBoot-WebSocket广播消息+单点消息(指定用户发送消息)
- SpringBoot 实现WebSocket进行消息发送(适用于SpringMVC)
- SpringBoot中的WebSocket点对点发送消息
- spring配置websocket并实现群发/单独发送消息
- spring配置websocket并实现群发/单独发送消息
- spring配置websocket并实现群发/单独发送消息
- spring-websocket 配置 websocket发送消息(群发)
- springboot 整合websocket 站内消息 (支持广播式和只给一人发送)单独信息发送 信息群发 统计连接数
- spring配置websocket并实现群发/单独发送消息
- SpringBoot webSocket实现发送广播、点对点消息和Android接收
- spring websocket 利用注解接收和发送消息
- 轻量级Spring 之 JmsTemplate 接收消息和发送消息
- Spring + ActiveMQ实现jms发送消息
- Java模拟发送键盘消息 shift组合键
- 轻量级Spring 之 JmsTemplate 接收消息和发送消息
- Java模拟异步消息的发送与回调(转)
- 使用MFC 发送消息模拟按钮按下事件
- ActiveMQ5.0实战:使用Spring发送,消费topic和queue消息
- android发送模拟按键消息,出现死锁,timeout的解决方法
- Spring 发送邮件--简单发送消息