|
@@ -0,0 +1,203 @@
|
|
|
|
+package org.jeecg.modules.largeScreen.component;
|
|
|
|
+
|
|
|
|
+import org.jeecg.modules.itdmOpenDaping.entity.ItdmDaping;
|
|
|
|
+import org.jeecg.modules.itdmOpenDaping.service.IItdmDapingService;
|
|
|
|
+import org.jeecg.modules.largeScreen.config.CronTaskRegistrar;
|
|
|
|
+import org.jeecg.modules.largeScreen.task.SchedulingRunnable;
|
|
|
|
+import org.jeecg.modules.largeScreen.util.EncoderClassListVo;
|
|
|
|
+import org.jeecg.modules.largeScreen.util.SpringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import javax.websocket.*;
|
|
|
|
+import javax.websocket.server.ServerEndpoint;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+import java.util.concurrent.CopyOnWriteArraySet;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @ServerEndpoint 注解是一个类层次的注解,它的功能主要是将目前的类定义成一个websocket服务器端,
|
|
|
|
+ * 注解的值将被用于监听用户连接的终端访问URL地址,客户端可以通过这个URL来连接到WebSocket服务器端
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+@ServerEndpoint(value = "/itdmWebsocket/list3", encoders = {EncoderClassListVo.class})
|
|
|
|
+public class WebSocketServer3 {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IItdmDapingService dapingService;
|
|
|
|
+
|
|
|
|
+ //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
|
|
|
|
+ private static int onlineCount = 0;
|
|
|
|
+
|
|
|
|
+ //concurrent包的线程安全Set,用来存放每个客户端对应的WebSocketServer对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
|
|
|
|
+ private static CopyOnWriteArraySet<WebSocketServer3> webSocketSet = new CopyOnWriteArraySet<WebSocketServer3>();
|
|
|
|
+
|
|
|
|
+ private static ConcurrentHashMap<String, SchedulingRunnable> webSocketSet1 = new ConcurrentHashMap<String, SchedulingRunnable>();
|
|
|
|
+ // private static String cabid = "";
|
|
|
|
+ //与某个客户端的连接会话,需要通过它来给客户端发送数据
|
|
|
|
+ private Session session;
|
|
|
|
+
|
|
|
|
+ //改的地方
|
|
|
|
+// public DemoTask3 getDemoTask3() {
|
|
|
|
+// return SpringUtils.getBean(DemoTask3.class);
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ public CronTaskRegistrar getCronTaskRegistrar() {
|
|
|
|
+ return SpringUtils.getBean(CronTaskRegistrar.class);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 连接建立成功调用的方法
|
|
|
|
+ *
|
|
|
|
+ * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据
|
|
|
|
+ */
|
|
|
|
+ @OnOpen
|
|
|
|
+ public void onOpen(Session session) {
|
|
|
|
+ this.session = session;
|
|
|
|
+ webSocketSet.add(this);
|
|
|
|
+//
|
|
|
|
+// sendUser(this.getDemoTask3().dakaiwangye(), session);
|
|
|
|
+// SchedulingRunnable task = new SchedulingRunnable("demoTask3", "dakaidaping", session);
|
|
|
|
+//
|
|
|
|
+// addOnlineCount();//在线数加1
|
|
|
|
+// this.getCronTaskRegistrar().addCronTask(task, "0/100 * * * * ?");//30秒传一次数据
|
|
|
|
+// webSocketSet1.put(session.getId(), task);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 连接关闭调用的方法
|
|
|
|
+ */
|
|
|
|
+ @OnClose
|
|
|
|
+ public void onClose(Session session) {
|
|
|
|
+ webSocketSet.remove(this);
|
|
|
|
+ subOnlineCount();
|
|
|
|
+ this.getCronTaskRegistrar().removeCronTask(webSocketSet1.get(session.getId()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 收到客户端消息后调用的方法
|
|
|
|
+ *
|
|
|
|
+ * @param message 客户端发送过来的消息
|
|
|
|
+ */
|
|
|
|
+ @OnMessage
|
|
|
|
+ public void onMessage(String message, Session session) throws IOException, EncodeException {
|
|
|
|
+
|
|
|
|
+ System.out.println("WebSocketServer3来自客户端的消息:" + message);
|
|
|
|
+ if(message.equals("打开大屏")){
|
|
|
|
+
|
|
|
|
+ List<Map<String,Object>> list = new ArrayList<>();//清空原来的数据
|
|
|
|
+
|
|
|
|
+ ItdmDaping daping = new ItdmDaping();
|
|
|
|
+ daping.setId(1);
|
|
|
|
+ daping.setDakaidaping(1);
|
|
|
|
+
|
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
|
+ map.put("data",daping.getDakaidaping()); //打开大屏
|
|
|
|
+ map.put("name","打开大屏");
|
|
|
|
+ list.add(map);
|
|
|
|
+
|
|
|
|
+ sendAll(list);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 发生错误时调用
|
|
|
|
+ *
|
|
|
|
+ * @param session
|
|
|
|
+ * @param error
|
|
|
|
+ */
|
|
|
|
+ @OnError
|
|
|
|
+ public void onError(Session session, Throwable error) {
|
|
|
|
+ System.out.println("发生错误");
|
|
|
|
+ error.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 这个方法与上面几个方法不一样。没有用注解,是根据自己需要添加的方法。
|
|
|
|
+ *
|
|
|
|
+ * @param message
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public void sendMessage(Object message) throws EncodeException, IOException {
|
|
|
|
+ this.session.getBasicRemote().sendObject(message);
|
|
|
|
+ //this.session.getAsyncRemote().sendText(message);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 暴露给外部的群发
|
|
|
|
+ *
|
|
|
|
+ * @param message
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ public void sendInfo(Object message) throws IOException {
|
|
|
|
+
|
|
|
|
+ sendAll(message);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 群发
|
|
|
|
+ *
|
|
|
|
+ * @param message
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public void sendUser(Object message, Session session) {
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ session.getBasicRemote().sendObject(message);
|
|
|
|
+ } catch (EncodeException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ this.getCronTaskRegistrar().removeCronTask(webSocketSet1.get(session.getId()));
|
|
|
|
+
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ this.getCronTaskRegistrar().removeCronTask(webSocketSet1.get(session.getId()));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private static void sendAll(Object message) {
|
|
|
|
+
|
|
|
|
+ Arrays.asList(webSocketSet.toArray()).forEach(item -> {
|
|
|
|
+
|
|
|
|
+ WebSocketServer3 WebsocketServer = (WebSocketServer3) item;
|
|
|
|
+
|
|
|
|
+ //群发
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+
|
|
|
|
+ WebsocketServer.sendMessage(message);
|
|
|
|
+
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+
|
|
|
|
+ } catch (EncodeException e) {
|
|
|
|
+
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static synchronized int getOnlineCount() {
|
|
|
|
+ return onlineCount;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static synchronized void addOnlineCount() {
|
|
|
|
+ WebSocketServer3.onlineCount++;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static synchronized void subOnlineCount() {
|
|
|
|
+ WebSocketServer3.onlineCount--;
|
|
|
|
+ }
|
|
|
|
+}
|