Springboot2.X + WebSocket实时推送

Springboot2.X + WebSocket实时推送

  • pom加入依赖
    • WebSocketConfig
    • WebSocketInterceptor拦截器
    • MyWebSocketHandler处理器
    • MessageAlarmPush消息推送
    • 注意:

pom加入依赖

		
 		
			org.springframework.boot
			spring-boot-starter-websocket
		

WebSocketConfig

添加配置类:

package com.szhq.iemp.reservation.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import com.szhq.iemp.reservation.intercptor.WebSocketInterceptor;
import com.szhq.iemp.reservation.listener.MyWebSocketHandler;
/**
 * 首先注入一个ServerEndpointExporterBean,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint
 * @author wanghao
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer{
	
	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(new MyWebSocketHandler(), "/websocket/{ID}").setAllowedOrigins("*").addInterceptors(new WebSocketInterceptor());
	}
}

WebSocketInterceptor拦截器

package com.szhq.iemp.reservation.intercptor;

import java.util.Map;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class WebSocketInterceptor implements HandshakeInterceptor{

	//在握手之前执行该方法, 继续握手返回true, 中断握手返回false. 通过attributes参数设置WebSocketSession的属性
	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception {
		if (request instanceof ServletServerHttpRequest) {
			String ID = request.getURI().toString().split("ID=")[1];
			log.info("current session id is:"+ID);
			attributes.put("WEBSOCKET_USERID",ID);
		}
		return true;
	}

	@Override
	public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
		log.info("coming webSocketInterceptor afterHandshake method...");
	}

}

MyWebSocketHandler处理器

package com.szhq.iemp.reservation.listener;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class MyWebSocketHandler implements WebSocketHandler{

	//在线用户列表
	private static final Map users;

	static {
		users = new HashMap<>();
	}

	@Override
	public void afterConnectionEstablished(WebSocketSession session) throws Exception {
		log.info("connect websocket successful!");
		String ID = session.getUri().toString().split("ID=")[1];
		log.info(ID);
		if (ID != null) {
			users.put(ID, session);
			session.sendMessage(new TextMessage("{\"message\":\"socket successful connection!\"}"));
			log.info("id:" + ID + ",session:" + session + "");
		}
		log.info("current user number is:"+users.size());
	}

	@Override
	public void handleMessage(WebSocketSession session, WebSocketMessage message) throws Exception {
		try{
			JSONObject jsonobject = JSONObject.parseObject((String)message.getPayload());
			log.info("receive message:" + jsonobject);
//			log.info(jsonobject.get("message")+":来自"+(String)session.getAttributes().get("WEBSOCKET_USERID")+"的消息");
			//jsonobject.get("id")
			sendMessageToUser(2+"", new TextMessage("{\"message\":\"server received message,hello!\"}"));
		}catch (Exception e) {
			log.error("e",e);
		}
	}

	@Override
	public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
		if (session.isOpen()) {
			session.close();
		}
		log.error("connect error", exception);
		users.remove(getClientId(session));
	}

	@Override
	public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
		log.error("connection closed: " + closeStatus);
		users.remove(getClientId(session));
	}

	@Override
	public boolean supportsPartialMessages() {
		return false;
	}

	/**
	 * 发送信息给指定用户
	 * @param clientId
	 * @param message
	 * @return
	 */
	public boolean sendMessageToUser(String clientId, TextMessage message) {
		if (users.get(clientId) == null) return false;
		WebSocketSession session = users.get(clientId);
		log.info("sendMessage:" + message);
		if (!session.isOpen()) return false;
		try {
			session.sendMessage(message);
		} catch (IOException e) {
			log.error("e", e);
			return false;
		}
		return true;
	}

	/**
	 * 广播信息
	 * @param message
	 * @return
	 */
	public boolean sendMessageToAllUsers(TextMessage message) {
		boolean allSendSuccess = true;
		Set clientIds = users.keySet();
		WebSocketSession session = null;
		for (String clientId : clientIds) {
			try {
				session = users.get(clientId);
				if (session.isOpen()) {
					session.sendMessage(message);
				}
			} catch (IOException e) {
				log.error("e", e);
				allSendSuccess = false;
			}
		}
		return allSendSuccess;
	}

	/**
	 * 获取用户标识
	 * @param session
	 * @return
	 */
	private String getClientId(WebSocketSession session) {
		try {
			String clientId = (String) session.getAttributes().get("WEBSOCKET_USERID");
			return clientId;
		} catch (Exception e) {
			log.error("e", e);
			return null;
		}
	}
	/**
	 * 获取在线人数
	 * @return
	 */
    public synchronized int getOnlineNum(){
        return users.size();
    }
}

实现了WebSocketHandler接口,并实现了关键的几个方法。

① afterConnectionEstablished(接口提供的):建立新的socket连接后回调的方法。主要逻辑是:将成功建立连接的webSocketSssion放到定义好的常量[private static final Map users;]中去。这里也截取客户端访问的URL的字符串,拿到标识,以键值对的形式讲每一个webSocketSession存到users里,以记录每个Socket。

② handleMessage(接口提供的):接收客户端发送的Socket。主要逻辑是:获取客户端发送的信息。这里之所以可以获取本次Socket的ID,是因为客户端在第一次进行连接时,拦截器进行拦截后,设置好ID,这样也说明,双方在相互通讯的时候,只是对第一次建立好的socket持续进行操作。

③ sendMessageToUser(自己定义的):发送给指定用户信息。主要逻辑是:根据用户ID从常量users(记录每一个Socket)中,获取Socket,往该Socket里发送消息,只要客户端还在线,就能收到该消息。

④sendMessageToAllUsers (自己定义的):这个广播消息,发送信息给所有socket。主要逻辑是:跟③类型,只不过是遍历整个users获取每一个socket,给每一个socket发送消息即可完广播发送

⑤handleTransportError(接口提供的):连接出错时,回调的方法。主要逻辑是:一旦有连接出错的Socket,就从users里进行移除,有提供该Socket的参数,可直接获取ID,进行移除。这个在客户端没有正常关闭连接时,会进来,所以在开发客户端时,记得关闭连接

⑥afterConnectionClosed(接口提供的):连接关闭时,回调的方法。主要逻辑:一旦客户端/服务器主动关闭连接时,将个socket从users里移除,有提供该Socket的参数,可直接获取ID,进行移除。

MessageAlarmPush消息推送

根据自己业务自行处理

package com.szhq.iemp.reservation.service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;

import com.szhq.iemp.reservation.listener.MyWebSocketHandler;
import com.szhq.iemp.reservation.util.RedisUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class MessageAlarmPush{

	@Resource(name = "primaryRedisUtil")
	private RedisUtil redisUtil;
	@Autowired
	private MyWebSocketHandler handler;

	@PostConstruct
	public void consumeAlarmData() {
		log.info("start websocket alarm thread...");
		Consume consume = new Consume();
		consume.start();
	}
	
	
	public class Consume extends Thread {
		@Override
		public void run() {
			while(true) {
				log.info("redis list size:" + redisUtil.lsize("alarm-rt-data"));
				try {
					String data = (String)redisUtil.brpop("alarm-rt-data");
					boolean isSuccess = handler.sendMessageToAllUsers(new TextMessage(data));
					log.info("websocket-alarm-data: " + data + ",result: " + isSuccess);
				} catch (InterruptedException e) {
					log.error("e", e);
				}
			}
		}
	}
}

注意:

nginx需要做一些配置,支持websocket通信。

   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";

前台代码忽略,效果图如下:
Springboot2.X + WebSocket实时推送_第1张图片

你可能感兴趣的:(java,springboot)