|
|
@@ -0,0 +1,138 @@
|
|
|
+package org.springblade.client.message.socket;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.websocket.OnClose;
|
|
|
+import javax.websocket.OnMessage;
|
|
|
+import javax.websocket.OnOpen;
|
|
|
+import javax.websocket.Session;
|
|
|
+import javax.websocket.server.PathParam;
|
|
|
+import javax.websocket.server.ServerEndpoint;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Random;
|
|
|
+import java.util.concurrent.CopyOnWriteArraySet;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 消息信息推送socket
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+@ServerEndpoint("/websocket/{userId}")
|
|
|
+public class MessageWebSocket
|
|
|
+{
|
|
|
+
|
|
|
+ /**使用示例
|
|
|
+ * 在业务中操作websocket
|
|
|
+ * @Autowried webSocket webSocket;
|
|
|
+ * webSocket.sendAllMessage("你有一条新的推送");
|
|
|
+ *
|
|
|
+ * */
|
|
|
+
|
|
|
+
|
|
|
+ private Session session;
|
|
|
+
|
|
|
+ private static CopyOnWriteArraySet<MessageWebSocket> webSockets =new CopyOnWriteArraySet<>();
|
|
|
+ private static Map<Long, Session> sessionPool = new HashMap<Long, Session>();
|
|
|
+
|
|
|
+ @OnOpen
|
|
|
+ public void onOpen(Session session, @PathParam(value="userId")Long userId) {
|
|
|
+ try {
|
|
|
+ this.session = session;
|
|
|
+ webSockets.add(this);
|
|
|
+ sessionPool.put(userId, session);
|
|
|
+ log.info("【websocket消息】有新的连接,总数为:"+webSockets.size());
|
|
|
+ send(userId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClose
|
|
|
+ public void onClose() {
|
|
|
+ try {
|
|
|
+ webSockets.remove(this);
|
|
|
+ log.info("【websocket消息】连接断开,总数为:"+webSockets.size());
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnMessage
|
|
|
+ public void onMessage(String message) {
|
|
|
+ //todo 现在有个定时任务刷,应该去掉
|
|
|
+ log.debug("【websocket消息】收到客户端消息:"+message);
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ obj.put("cmd", "heartcheck");//业务类型
|
|
|
+ obj.put("msgTxt", "心跳响应");//消息内容
|
|
|
+ session.getAsyncRemote().sendText(obj.toJSONString());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 此为广播消息
|
|
|
+ public void sendAllMessage(String message) {
|
|
|
+ log.info("【websocket消息】广播消息:"+message);
|
|
|
+ for(MessageWebSocket messageWebSocket : webSockets) {
|
|
|
+ try {
|
|
|
+ if(messageWebSocket.session.isOpen()) {
|
|
|
+ messageWebSocket.session.getAsyncRemote().sendText(message);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 此为单点消息(单人)
|
|
|
+ public void sendOneMessage(Long userId, String message) {
|
|
|
+ Session session = sessionPool.get(userId);
|
|
|
+ if (session != null&&session.isOpen()) {
|
|
|
+ try {
|
|
|
+ log.info("【websocket消息】 单点消息:"+message);
|
|
|
+ session.getAsyncRemote().sendText(message);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 此为单点消息(多人)
|
|
|
+ public void sendMoreMessage(String[] userIds, String message) {
|
|
|
+ for(String userId:userIds) {
|
|
|
+ Session session = sessionPool.get(userId);
|
|
|
+ if (session != null&&session.isOpen()) {
|
|
|
+ try {
|
|
|
+ log.info("【websocket消息】 单点消息:"+message);
|
|
|
+ session.getAsyncRemote().sendText(message);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void send(Long userId)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ while(true)
|
|
|
+ {
|
|
|
+ int max=175;
|
|
|
+ int min=10;
|
|
|
+ Random random = new Random();
|
|
|
+ int x = random.nextInt(max) % (max - min + 1) + min;
|
|
|
+ int y = random.nextInt(max) % (max - min + 1) + min;
|
|
|
+ String mark="["+x+","+y+"]";
|
|
|
+ sendOneMessage(userId,mark);
|
|
|
+ Thread.sleep(6000);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (InterruptedException e)
|
|
|
+ {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|