正在显示
9 个修改的文件
包含
464 行增加
和
7 行删除
@@ -32,6 +32,12 @@ | @@ -32,6 +32,12 @@ | ||
32 | <groupId>org.springframework.boot</groupId> | 32 | <groupId>org.springframework.boot</groupId> |
33 | <artifactId>spring-boot-starter-web</artifactId> | 33 | <artifactId>spring-boot-starter-web</artifactId> |
34 | </dependency> | 34 | </dependency> |
35 | + | ||
36 | + <!-- 引入websocket依赖 --> | ||
37 | + <dependency> | ||
38 | + <groupId>org.springframework.boot</groupId> | ||
39 | + <artifactId>spring-boot-starter-websocket</artifactId> | ||
40 | + </dependency> | ||
35 | <!-- SpringBoot end --> | 41 | <!-- SpringBoot end --> |
36 | 42 | ||
37 | <!-- Security start --> | 43 | <!-- Security start --> |
@@ -95,6 +101,12 @@ | @@ -95,6 +101,12 @@ | ||
95 | 101 | ||
96 | <!-- tools start --> | 102 | <!-- tools start --> |
97 | <dependency> | 103 | <dependency> |
104 | + <groupId>cn.hutool</groupId> | ||
105 | + <artifactId>hutool-all</artifactId> | ||
106 | + <version>4.0.12</version> | ||
107 | + </dependency> | ||
108 | + | ||
109 | + <dependency> | ||
98 | <groupId>org.projectlombok</groupId> | 110 | <groupId>org.projectlombok</groupId> |
99 | <artifactId>lombok</artifactId> | 111 | <artifactId>lombok</artifactId> |
100 | <optional>true</optional> | 112 | <optional>true</optional> |
1 | package com.sunyo.wlpt.cgonms.provide.controller; | 1 | package com.sunyo.wlpt.cgonms.provide.controller; |
2 | 2 | ||
3 | +import com.alibaba.fastjson.JSON; | ||
3 | import com.google.common.collect.Lists; | 4 | import com.google.common.collect.Lists; |
4 | 5 | ||
5 | import com.sunyo.wlpt.cgonms.provide.domain.*; | 6 | import com.sunyo.wlpt.cgonms.provide.domain.*; |
@@ -7,14 +8,19 @@ import com.sunyo.wlpt.cgonms.provide.feign.GetCgoAsmFeign; | @@ -7,14 +8,19 @@ import com.sunyo.wlpt.cgonms.provide.feign.GetCgoAsmFeign; | ||
7 | import com.sunyo.wlpt.cgonms.provide.feign.GetDataWareHouseFeign; | 8 | import com.sunyo.wlpt.cgonms.provide.feign.GetDataWareHouseFeign; |
8 | import com.sunyo.wlpt.cgonms.provide.feign.GetTransportFeign; | 9 | import com.sunyo.wlpt.cgonms.provide.feign.GetTransportFeign; |
9 | import com.sunyo.wlpt.cgonms.provide.response.ResultJson; | 10 | import com.sunyo.wlpt.cgonms.provide.response.ResultJson; |
11 | +import com.sunyo.wlpt.cgonms.provide.response.ResultWs; | ||
10 | import com.sunyo.wlpt.cgonms.provide.service.*; | 12 | import com.sunyo.wlpt.cgonms.provide.service.*; |
11 | import com.sunyo.wlpt.cgonms.provide.thread.ExitThreadPoolFactory; | 13 | import com.sunyo.wlpt.cgonms.provide.thread.ExitThreadPoolFactory; |
14 | +import com.sunyo.wlpt.cgonms.provide.websocket.WebSocketServer; | ||
12 | import lombok.extern.slf4j.Slf4j; | 15 | import lombok.extern.slf4j.Slf4j; |
13 | import org.springframework.beans.propertyeditors.CustomDateEditor; | 16 | import org.springframework.beans.propertyeditors.CustomDateEditor; |
14 | import org.springframework.web.bind.WebDataBinder; | 17 | import org.springframework.web.bind.WebDataBinder; |
15 | import org.springframework.web.bind.annotation.*; | 18 | import org.springframework.web.bind.annotation.*; |
19 | +import java.io.IOException; | ||
16 | 20 | ||
17 | import javax.annotation.Resource; | 21 | import javax.annotation.Resource; |
22 | +import javax.servlet.http.HttpServletRequest; | ||
23 | +import java.io.IOException; | ||
18 | import java.text.SimpleDateFormat; | 24 | import java.text.SimpleDateFormat; |
19 | import java.util.Date; | 25 | import java.util.Date; |
20 | import java.util.List; | 26 | import java.util.List; |
@@ -55,6 +61,7 @@ public class NmsController { | @@ -55,6 +61,7 @@ public class NmsController { | ||
55 | @Resource | 61 | @Resource |
56 | GetDataWareHouseFeign getDataWareHouseFeign; | 62 | GetDataWareHouseFeign getDataWareHouseFeign; |
57 | 63 | ||
64 | + | ||
58 | /** | 65 | /** |
59 | * 线程数量 | 66 | * 线程数量 |
60 | */ | 67 | */ |
@@ -81,10 +88,18 @@ public class NmsController { | @@ -81,10 +88,18 @@ public class NmsController { | ||
81 | @GetMapping("/getInfo") | 88 | @GetMapping("/getInfo") |
82 | public ResultJson getData( | 89 | public ResultJson getData( |
83 | @RequestParam(value = "flightDate", required = false) Date flightDate, | 90 | @RequestParam(value = "flightDate", required = false) Date flightDate, |
84 | - @RequestParam(value = "flightNo", required = false) String flightNo) { | 91 | + @RequestParam(value = "flightNo", required = false) String flightNo, |
92 | + HttpServletRequest request) { | ||
85 | 93 | ||
94 | + /** | ||
95 | + * 1.取token作爲websocket的sid | ||
96 | + * 2.每個可以返回給前端的消息通過websocket發送回去 | ||
97 | + */ | ||
98 | + String sid = request.getHeader("Authorization"); | ||
99 | + log.info("token的值:"+sid); | ||
86 | final String startTime = sdf.format(new Date()); | 100 | final String startTime = sdf.format(new Date()); |
87 | System.out.println("开始时间:" + startTime); | 101 | System.out.println("开始时间:" + startTime); |
102 | + sendMsgByWebsocket("开始时间:" + startTime,sid); | ||
88 | System.out.println("出港数据,开始获取"); | 103 | System.out.println("出港数据,开始获取"); |
89 | ResultJson resultJson = new ResultJson(); | 104 | ResultJson resultJson = new ResultJson(); |
90 | 105 | ||
@@ -110,6 +125,9 @@ public class NmsController { | @@ -110,6 +125,9 @@ public class NmsController { | ||
110 | for (i = 0; i < THREAD_ACCOUNT; i++) { | 125 | for (i = 0; i < THREAD_ACCOUNT; i++) { |
111 | ResultExitData result = threadJob(resultList.get(i + index), latch, threadPool); | 126 | ResultExitData result = threadJob(resultList.get(i + index), latch, threadPool); |
112 | resultList.set(i, result); | 127 | resultList.set(i, result); |
128 | + int temp=i+index; | ||
129 | + String resultJs=JSON.toJSONString(new ResultWs(sid,"获取数据,第"+temp+"条",result)); | ||
130 | + sendMsgByWebsocket(resultJs,sid); | ||
113 | } | 131 | } |
114 | index = index + i; | 132 | index = index + i; |
115 | //完成一次,就等待。每次减1,为0的时候往下执行 | 133 | //完成一次,就等待。每次减1,为0的时候往下执行 |
@@ -122,7 +140,9 @@ public class NmsController { | @@ -122,7 +140,9 @@ public class NmsController { | ||
122 | int i; | 140 | int i; |
123 | for (i = 0; i < resultList.size() - index; i++) { | 141 | for (i = 0; i < resultList.size() - index; i++) { |
124 | ResultExitData result = threadJob(resultList.get(i + index), latch, threadPool); | 142 | ResultExitData result = threadJob(resultList.get(i + index), latch, threadPool); |
125 | - resultList.set(i, result); | 143 | + int temp=i+index; |
144 | + String resultJs=JSON.toJSONString(new ResultWs(sid,"获取数据,第"+temp+"条",result)); | ||
145 | + sendMsgByWebsocket(resultJs,sid); | ||
126 | } | 146 | } |
127 | index = index + i; | 147 | index = index + i; |
128 | //完成一次,就等待。每次减1,为0的时候往下执行 | 148 | //完成一次,就等待。每次减1,为0的时候往下执行 |
@@ -137,6 +157,8 @@ public class NmsController { | @@ -137,6 +157,8 @@ public class NmsController { | ||
137 | for (int i = 0; i < resultList.size(); i++) { | 157 | for (int i = 0; i < resultList.size(); i++) { |
138 | ResultExitData result = threadJob(resultList.get(i), latch, threadPool); | 158 | ResultExitData result = threadJob(resultList.get(i), latch, threadPool); |
139 | resultList.set(i, result); | 159 | resultList.set(i, result); |
160 | + String resultJs=JSON.toJSONString(new ResultWs(sid,"获取数据,第"+i+"条",result)); | ||
161 | + sendMsgByWebsocket(resultJs,sid); | ||
140 | } | 162 | } |
141 | //完成一次,就等待。等所有的全部完成,再一起返回 | 163 | //完成一次,就等待。等所有的全部完成,再一起返回 |
142 | latch.await(); | 164 | latch.await(); |
@@ -146,10 +168,14 @@ public class NmsController { | @@ -146,10 +168,14 @@ public class NmsController { | ||
146 | log.error("获取目录文件出错", e); | 168 | log.error("获取目录文件出错", e); |
147 | } | 169 | } |
148 | 170 | ||
149 | - final String endTime = sdf.format(new Date()); | ||
150 | - System.out.println("结束时间:" + endTime); | 171 | + |
151 | System.out.println("出港信息,获取完毕"); | 172 | System.out.println("出港信息,获取完毕"); |
152 | System.out.println("index的值为:" + index); | 173 | System.out.println("index的值为:" + index); |
174 | + final String endTime = sdf.format(new Date()); | ||
175 | + | ||
176 | + System.out.println("结束时间:" + endTime); | ||
177 | + sendMsgByWebsocket("出港信息,获取完毕,结束时间:" + endTime,sid); | ||
178 | + | ||
153 | resultJson.setCode("200"); | 179 | resultJson.setCode("200"); |
154 | resultJson.setData(resultList); | 180 | resultJson.setData(resultList); |
155 | return resultJson; | 181 | return resultJson; |
@@ -278,4 +304,13 @@ public class NmsController { | @@ -278,4 +304,13 @@ public class NmsController { | ||
278 | dateFormat.setLenient(false); | 304 | dateFormat.setLenient(false); |
279 | binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); | 305 | binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); |
280 | } | 306 | } |
307 | + | ||
308 | + private void sendMsgByWebsocket(String msg,String sid){ | ||
309 | + try { | ||
310 | + WebSocketServer.sendInfo(msg,sid); | ||
311 | + } catch (IOException e) { | ||
312 | + e.printStackTrace(); | ||
313 | + | ||
314 | + } | ||
315 | + } | ||
281 | } | 316 | } |
1 | +package com.sunyo.wlpt.cgonms.provide.response; | ||
2 | + | ||
3 | +import lombok.Data; | ||
4 | + | ||
5 | +import java.io.Serializable; | ||
6 | + | ||
7 | +/** | ||
8 | + * @author 子诚 | ||
9 | + * Description: | ||
10 | + * 时间:2020/5/28 16:59 | ||
11 | + */ | ||
12 | +@Data | ||
13 | +public class ResultWs<T> implements Serializable { | ||
14 | + | ||
15 | + private static final long serialVersionUID = -1412519603409652504L; | ||
16 | + | ||
17 | + String sid; | ||
18 | + | ||
19 | + String message; | ||
20 | + | ||
21 | + T data; | ||
22 | + | ||
23 | + String status; | ||
24 | + | ||
25 | + public ResultWs() { | ||
26 | + } | ||
27 | + | ||
28 | + public ResultWs(String sid, T data) { | ||
29 | + this.sid = sid; | ||
30 | + this.data = data; | ||
31 | + } | ||
32 | + | ||
33 | + public ResultWs(String sid, String message, T data) { | ||
34 | + this.sid = sid; | ||
35 | + this.message = message; | ||
36 | + this.data = data; | ||
37 | + } | ||
38 | + | ||
39 | + public ResultWs(String sid, T data, String status) { | ||
40 | + this.sid = sid; | ||
41 | + this.data = data; | ||
42 | + this.status = status; | ||
43 | + } | ||
44 | + | ||
45 | + public ResultWs(String sid, String message, T data, String status) { | ||
46 | + this.sid = sid; | ||
47 | + this.message = message; | ||
48 | + this.data = data; | ||
49 | + this.status = status; | ||
50 | + } | ||
51 | +} |
1 | +package com.sunyo.wlpt.cgonms.provide.socket; | ||
2 | + | ||
3 | + | ||
4 | +import com.alibaba.fastjson.JSONObject; | ||
5 | +import lombok.extern.slf4j.Slf4j; | ||
6 | +import org.apache.ibatis.annotations.Param; | ||
7 | +import org.springframework.stereotype.Component; | ||
8 | +import org.springframework.web.bind.annotation.RequestBody; | ||
9 | + | ||
10 | +import javax.websocket.*; | ||
11 | +import javax.websocket.server.ServerEndpoint; | ||
12 | +import java.io.IOException; | ||
13 | +import java.util.concurrent.CopyOnWriteArraySet; | ||
14 | +import java.util.concurrent.atomic.AtomicInteger; | ||
15 | + | ||
16 | +/** | ||
17 | + * @author 子诚 | ||
18 | + * Description:WebSocket服务端 | ||
19 | + * 时间:2020/5/28 11:24 | ||
20 | + */ | ||
21 | +//@Component | ||
22 | +@Slf4j | ||
23 | +//@ServerEndpoint(value = "/websocket") | ||
24 | +public class WebSocketServer { | ||
25 | + /** | ||
26 | + * 在线数量 | ||
27 | + */ | ||
28 | + private static final AtomicInteger OnlineCount = new AtomicInteger(0); | ||
29 | + | ||
30 | + /** | ||
31 | + * concurrent包的线程安全Set,用来存放每个客户端对应的Session对象。 | ||
32 | + */ | ||
33 | + private static CopyOnWriteArraySet<Session> SessionSet = new CopyOnWriteArraySet<>(); | ||
34 | + | ||
35 | + | ||
36 | + /** | ||
37 | + * 连接建立成功调用的方法 | ||
38 | + */ | ||
39 | + @OnOpen | ||
40 | + public void onOpen(Session session) throws IOException { | ||
41 | + SessionSet.add(session); | ||
42 | + // 在线数量,加1 | ||
43 | + int cnt = OnlineCount.incrementAndGet(); | ||
44 | + log.info("有连接加入,当前连接数为:{}", cnt); | ||
45 | + SendMessage(session, "服务端回消息:连接成功"); | ||
46 | + } | ||
47 | + | ||
48 | + /** | ||
49 | + * 连接关闭调用的方法 | ||
50 | + */ | ||
51 | + @OnClose | ||
52 | + public void onClose(Session session) { | ||
53 | + SessionSet.remove(session); | ||
54 | + // 在线数量,减1 | ||
55 | + int cnt = OnlineCount.decrementAndGet(); | ||
56 | + log.info("有连接关闭,当前连接数为:{}", cnt); | ||
57 | + } | ||
58 | + | ||
59 | + /** | ||
60 | + * 收到客户端消息后调用的方法 | ||
61 | + * | ||
62 | + * @param message 客户端发送过来的消息 | ||
63 | + */ | ||
64 | + @OnMessage | ||
65 | + public void onMessage(String message, Session session) throws IOException { | ||
66 | + log.info("来自客户端的消息:{}", message); | ||
67 | + | ||
68 | +// JSONObject jsonObject = JSONObject.parseObject(message); | ||
69 | +// Object flightNo = jsonObject.get("flightNo"); | ||
70 | +// Object flightDate = jsonObject.get("flightDate"); | ||
71 | +// log.info("flight: {},flightDate: {}", flightNo,flightDate); | ||
72 | + SendMessage(session, "服务端收到消息,消息内容:" + message); | ||
73 | + } | ||
74 | + | ||
75 | + /** | ||
76 | + * 出现错误 | ||
77 | + */ | ||
78 | + @OnError | ||
79 | + public void onError(Session session, Throwable error) { | ||
80 | + log.error("发生错误:{},Session ID: {}", error.getMessage(), session.getId()); | ||
81 | + } | ||
82 | + | ||
83 | + /** | ||
84 | + * 发送消息,实践表明,每次浏览器刷新,session会发生变化。 | ||
85 | + * | ||
86 | + * @param session session | ||
87 | + * @param message 消息 | ||
88 | + */ | ||
89 | + private static void SendMessage(Session session, String message) throws IOException { | ||
90 | + | ||
91 | + session.getBasicRemote().sendText(String.format("%s (From Server,Session ID=%s)", message, session.getId())); | ||
92 | + | ||
93 | + } | ||
94 | + | ||
95 | + /** | ||
96 | + * 群发消息 | ||
97 | + * | ||
98 | + * @param message 消息 | ||
99 | + */ | ||
100 | + public static void BroadCastInfo(String message) throws IOException { | ||
101 | + for (Session session : SessionSet) { | ||
102 | + if (session.isOpen()) { | ||
103 | + SendMessage(session, message); | ||
104 | + } | ||
105 | + } | ||
106 | + } | ||
107 | + | ||
108 | + /** | ||
109 | + * 指定Session发送消息 | ||
110 | + * | ||
111 | + * @param sessionId sessionId | ||
112 | + * @param message 消息 | ||
113 | + */ | ||
114 | + public static void SendMessage(String sessionId, String message) throws IOException { | ||
115 | + Session session = null; | ||
116 | + for (Session s : SessionSet) { | ||
117 | + if (s.getId().equals(sessionId)) { | ||
118 | + session = s; | ||
119 | + break; | ||
120 | + } | ||
121 | + } | ||
122 | + if (session != null) { | ||
123 | + SendMessage(session, message); | ||
124 | + } else { | ||
125 | + log.warn("没有找到你指定ID的会话:{}", sessionId); | ||
126 | + } | ||
127 | + } | ||
128 | + | ||
129 | +} |
1 | +package com.sunyo.wlpt.cgonms.provide.websocket; | ||
2 | + | ||
3 | +import org.springframework.beans.BeansException; | ||
4 | +import org.springframework.beans.factory.BeanFactory; | ||
5 | +import org.springframework.context.ApplicationContext; | ||
6 | +import org.springframework.context.ApplicationContextAware; | ||
7 | + | ||
8 | +import javax.websocket.server.ServerEndpointConfig; | ||
9 | + | ||
10 | +/** | ||
11 | + * | ||
12 | + * @author XYH | ||
13 | + * @date 2019/12/24 | ||
14 | + */ | ||
15 | +public class MySpringConfigurator extends ServerEndpointConfig.Configurator implements ApplicationContextAware { | ||
16 | + private static volatile BeanFactory context; | ||
17 | + | ||
18 | + @Override | ||
19 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
20 | + MySpringConfigurator.context = applicationContext; | ||
21 | + } | ||
22 | + | ||
23 | + @Override | ||
24 | + public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException { | ||
25 | + return context.getBean(clazz); | ||
26 | + } | ||
27 | +} |
1 | +package com.sunyo.wlpt.cgonms.provide.websocket; | ||
2 | + | ||
3 | +import org.springframework.context.annotation.Bean; | ||
4 | +import org.springframework.context.annotation.Configuration; | ||
5 | +import org.springframework.web.socket.server.standard.ServerEndpointExporter; | ||
6 | + | ||
7 | +/** | ||
8 | + * | ||
9 | + * @author XYH | ||
10 | + * @date 2019/12/23 | ||
11 | + */ | ||
12 | +@Configuration | ||
13 | +public class WebSocketConfig { | ||
14 | + @Bean | ||
15 | + public ServerEndpointExporter serverEndpointExporter() { | ||
16 | + return new ServerEndpointExporter(); | ||
17 | + } | ||
18 | + @Bean | ||
19 | + public MySpringConfigurator mySpringConfigurator() { | ||
20 | + return new MySpringConfigurator(); | ||
21 | + } | ||
22 | +} |
1 | +package com.sunyo.wlpt.cgonms.provide.websocket; | ||
2 | + | ||
3 | + | ||
4 | +import com.sunyo.wlpt.cgonms.provide.response.ResultJson; | ||
5 | +import org.springframework.web.bind.annotation.*; | ||
6 | + | ||
7 | +import java.io.IOException; | ||
8 | + | ||
9 | +/** | ||
10 | + * | ||
11 | + * @author XYH | ||
12 | + * @date 2019/12/24 | ||
13 | + */ | ||
14 | +@RestController | ||
15 | +@RequestMapping("/checkcenter") | ||
16 | +public class WebSocketController { | ||
17 | + //页面请求 | ||
18 | + @GetMapping("/socket/{cid}") | ||
19 | + public ResultJson socket(@PathVariable String cid) { | ||
20 | + | ||
21 | + return new ResultJson("200","success",cid); | ||
22 | + } | ||
23 | + //推送数据接口 | ||
24 | + @ResponseBody | ||
25 | + @RequestMapping("/socket/push/{cid}/{message}") | ||
26 | + public ResultJson pushToWeb(@PathVariable String cid,@PathVariable String message) { | ||
27 | + try { | ||
28 | + WebSocketServer.sendInfo(message,cid); | ||
29 | + } catch (IOException e) { | ||
30 | + e.printStackTrace(); | ||
31 | + return new ResultJson("200","success",cid+"#"+e.getMessage()); | ||
32 | + } | ||
33 | + return new ResultJson("200","success",cid); | ||
34 | + } | ||
35 | +} |
1 | +package com.sunyo.wlpt.cgonms.provide.websocket; | ||
2 | + | ||
3 | + | ||
4 | +import cn.hutool.log.Log; | ||
5 | +import cn.hutool.log.LogFactory; | ||
6 | +import com.sunyo.wlpt.cgonms.provide.controller.NmsController; | ||
7 | +import org.springframework.stereotype.Component; | ||
8 | + | ||
9 | +import javax.websocket.*; | ||
10 | +import javax.websocket.server.PathParam; | ||
11 | +import javax.websocket.server.ServerEndpoint; | ||
12 | +import java.io.IOException; | ||
13 | +import java.util.concurrent.CopyOnWriteArraySet; | ||
14 | +import java.util.concurrent.atomic.AtomicInteger; | ||
15 | + | ||
16 | +/** | ||
17 | + * Created by XYH on 2019/12/23. | ||
18 | + */ | ||
19 | +@ServerEndpoint(value = "/websocket/{sid}", configurator = MySpringConfigurator.class) | ||
20 | +@Component | ||
21 | +public class WebSocketServer { | ||
22 | + | ||
23 | + static Log log = LogFactory.get(WebSocketServer.class); | ||
24 | + | ||
25 | + //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 | ||
26 | + private static int onlineCount = 0; | ||
27 | + | ||
28 | + //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。 | ||
29 | + private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>(); | ||
30 | + | ||
31 | + //与某个客户端的连接会话,需要通过它来给客户端发送数据 | ||
32 | + private Session session; | ||
33 | + | ||
34 | + //接收sid | ||
35 | + private String sid = ""; | ||
36 | + | ||
37 | + /** | ||
38 | + * 连接建立成功调用的方法 | ||
39 | + */ | ||
40 | + @OnOpen | ||
41 | + public void onOpen(Session session, @PathParam("sid") String sid) { | ||
42 | + this.session = session; | ||
43 | + webSocketSet.add(this); //加入set中 | ||
44 | + addOnlineCount(); //在线数加1 | ||
45 | + log.info("有新窗口开始监听:" + sid + ",当前在线人数为" + getOnlineCount()); | ||
46 | + this.sid = sid; | ||
47 | + try { | ||
48 | + String openMessage= "{\n" + | ||
49 | + " \"sid\":\""+sid+"\",\n" + | ||
50 | + " \"session\":\""+session+"\",\n" + | ||
51 | + " \"message\": \"连接成功\",\n" + | ||
52 | + " \"data\":{\"flightdate\":\"2015-02-05\",\"flightNo\":\"CV987\"}\n" + | ||
53 | + "}"; | ||
54 | + sendMessage(openMessage); | ||
55 | + } catch (IOException e) { | ||
56 | + log.error("websocket IO异常"); | ||
57 | + } | ||
58 | + } | ||
59 | + | ||
60 | + /** | ||
61 | + * 连接关闭调用的方法 | ||
62 | + */ | ||
63 | + @OnClose | ||
64 | + public void onClose() { | ||
65 | + webSocketSet.remove(this); //从set中删除 | ||
66 | + subOnlineCount(); //在线数减1 | ||
67 | + log.info("有一连接关闭!当前在线人数为" + getOnlineCount()); | ||
68 | + } | ||
69 | + | ||
70 | + /** | ||
71 | + * 收到客户端消息后调用的方法 | ||
72 | + * | ||
73 | + * @param message 客户端发送过来的消息,以json形式传递接收, | ||
74 | + * 如:{ | ||
75 | + * "sid":"aaa", | ||
76 | + * "session":"", | ||
77 | + * "message": "", | ||
78 | + * "data":{"flightdate":"2015-02-05","flightNo":"CV987"} | ||
79 | + * | ||
80 | + * | ||
81 | + * } | ||
82 | + */ | ||
83 | + @OnMessage | ||
84 | + public void onMessage(String message, Session session) { | ||
85 | + | ||
86 | + log.info("收到来自窗口" + sid + "的信息:" + message); | ||
87 | + //群发消息 | ||
88 | + for (WebSocketServer item : webSocketSet) { | ||
89 | + try { | ||
90 | + item.sendMessage(message); | ||
91 | + } catch (IOException e) { | ||
92 | + e.printStackTrace(); | ||
93 | + } | ||
94 | + } | ||
95 | + } | ||
96 | + | ||
97 | + /** | ||
98 | + * @param session | ||
99 | + * @param error | ||
100 | + */ | ||
101 | + @OnError | ||
102 | + public void onError(Session session, Throwable error) { | ||
103 | + log.error("发生错误"); | ||
104 | + error.printStackTrace(); | ||
105 | + } | ||
106 | + | ||
107 | + /** | ||
108 | + * 实现服务器主动推送 | ||
109 | + */ | ||
110 | + public void sendMessage(String message) throws IOException { | ||
111 | + this.session.getBasicRemote().sendText(message); | ||
112 | + } | ||
113 | + | ||
114 | + | ||
115 | + /** | ||
116 | + * 群发自定义消息 | ||
117 | + */ | ||
118 | + public static void sendInfo(String message, @PathParam("sid") String sid) throws IOException { | ||
119 | + //log.info("推送消息到窗口"+sid+",推送内容:"+message); | ||
120 | + for (WebSocketServer item : webSocketSet) { | ||
121 | + try { | ||
122 | + //这里可以设定只推送给这个sid的,为null则全部推送 | ||
123 | + if (sid == null) { | ||
124 | + item.sendMessage(message); | ||
125 | + } else if (item.sid.equals(sid)) { | ||
126 | + item.sendMessage(message); | ||
127 | + } | ||
128 | + } catch (IOException e) { | ||
129 | + continue; | ||
130 | + } | ||
131 | + } | ||
132 | + } | ||
133 | + | ||
134 | + public static synchronized int getOnlineCount() { | ||
135 | + return onlineCount; | ||
136 | + } | ||
137 | + | ||
138 | + public static synchronized void addOnlineCount() { | ||
139 | + WebSocketServer.onlineCount++; | ||
140 | + } | ||
141 | + | ||
142 | + public static synchronized void subOnlineCount() { | ||
143 | + WebSocketServer.onlineCount--; | ||
144 | + } | ||
145 | +} | ||
146 | + |
@@ -34,10 +34,10 @@ mybatis: | @@ -34,10 +34,10 @@ mybatis: | ||
34 | type-aliases-package: com.sunyo.wlpt.cgonms.provide.domain | 34 | type-aliases-package: com.sunyo.wlpt.cgonms.provide.domain |
35 | 35 | ||
36 | # \u65E5\u5FD7\u6253\u5370 | 36 | # \u65E5\u5FD7\u6253\u5370 |
37 | -#logging: | 37 | +logging: |
38 | # config: config/logback-dev.xml | 38 | # config: config/logback-dev.xml |
39 | -# level: | ||
40 | -# com.sunyo.wlpt.cgonms.provide.mapper: debug | 39 | + level: |
40 | + com.sunyo.wlpt.cgonms.provide.mapper: debug | ||
41 | #logback: | 41 | #logback: |
42 | # appname: cgonms-provide | 42 | # appname: cgonms-provide |
43 | # logdir: ./log | 43 | # logdir: ./log |
-
请 注册 或 登录 后发表评论