springboot1.5.9 实现websocket
⾸先,在springboot项⽬创建并配置成功的基础上对websocket进⾏整合。
1、在pom⽂件中添加对websocket的依赖
1 <!-- 引⼊ websocket 依赖类-->
2 <dependency>bind9
3 <groupId>org.springframework.boot</groupId>
4 <artifactId>spring-boot-starter-websocket</artifactId>
5 </dependency>
2、个⼈习惯是先从后端写到前端,先写websocket的配置类吧
1 import org.springframework.stereotype.Component;
2
3 import javax.websocket.*;
4 import javax.websocket.server.ServerEndpoint;
5 import java.io.IOException;
6 import urrent.CopyOnWriteArraySet;
7
8 /**
9 * @Name:WebSocket
10 * @Description:WebSocket配置
11 * @Version:V1.0.0
12 * @Author:mYunYu
13 * @Create Date:2018/11/15 14:45
14 */
15 @Component
16 @ServerEndpoint(value = "/ws/webSocket" , encoders = {EncoderClassVo.class})
17 public class WebSocket {
18 //每个客户端都会有相应的session,服务端可以发送相关消息 19 private Session session;
20
21 //J.U.C包下线程安全的类,主要⽤来存放每个客户端对应的webSocket连接 22 private static CopyOnWriteArraySet<WebSocket> copyOnWriteArraySet = new CopyOnWriteArraySet<WebSocket>();
23
24 /**
25 * @Name:onOpen
26 * @Description:打开连接。进⼊页⾯后会⾃动发请求到此进⾏连接
27 * @Author:mYunYu
28 * @Create Date:14:46 2018/11/15
29 * @Parameters:
30 * @Return:
31 */
32 @OnOpen
33 public void onOpen(Session session) {
34 this.session = session;
35 copyOnWriteArraySet.add(this);
36 System.out.println("websocket有新的连接, 总数:"+ copyOnWriteArraySet.size());
37
38 }
39
40 /**
41 * @Name:onClose
42 * @Description:⽤户关闭页⾯,即关闭连接
43 * @Author:mYunYu
44 * @Create Date:14:46 2018/11/15
45 * @Parameters:
46 * @Return:
47 */
48 @OnClose
49 public void onClose() {
50 ve(this);
50 ve(this);
51 System.out.println("websocket连接断开, 总数:"+ copyOnWriteArraySet.size());
52 }
53
54 /**
55 * @Name:onMessage
56 * @Description:测试客户端发送消息,测试是否联通
57 * @Author:mYunYu
58 * @Create Date:14:46 2018/11/15
59 * @Parameters:
60 * @Return:
61 */
日本生命公司破产
62 @OnMessage
63 public void onMessage(String message) {
64 System.out.println("websocket收到客户端发来的消息:"+message);
65 }
66
67 /**
68 * @Name:onError
69 * @Description:出现错误
70 * @Author:mYunYu
71 * @Create Date:14:46 2018/11/15
72 * @Parameters:
73 * @Return:
74 */
75 @OnError
76 public void onError(Session session, Throwable error) {
77 System.out.println("发⽣错误:" + Message() + "; sessionId:" + Id());
78 error.printStackTrace();
79 }
海顿c大调奏鸣曲
80
81 public void sendMessage(Object object){
82 //遍历客户端
83 for (WebSocket webSocket : copyOnWriteArraySet) {
84 System.out.println("websocket⼴播消息:" + String());
85 try {
86 //服务器主动推送
87 BasicRemote().sendObject(object) ;
88 } catch (Exception e) {
89 e.printStackTrace();
90 }
91 }
92 }
93
94 /**
95 * @Name:sendMessage
96 * @Description:⽤于发送给客户端消息(发)
97 * @Author:mYunYu
98 * @Create Date:14:46 2018/11/15
99 * @Parameters:
100 * @Return:
101 */
102 public void sendMessage(String message) {
103 //遍历客户端
104 for (WebSocket webSocket : copyOnWriteArraySet) {
105 System.out.println("websocket⼴播消息:" + message);
106 try {
107 //服务器主动推送
108 BasicRemote().sendText(message);
109 } catch (Exception e) {
110 e.printStackTrace();
111 }
112 }
113 }
114
115 /**
115 /**
116 * @Name:sendMessage
117 * @Description:⽤于发送给指定客户端消息
118 * @Author:mYunYu
119 * @Create Date:14:47 2018/11/15
120 * @Parameters:
121 * @Return:
122 */
123 public void sendMessage(String sessionId, String message) throws IOException {
124 Session session = null;
125 WebSocket tempWebSocket = null;
126 for (WebSocket webSocket : copyOnWriteArraySet) {
127 if (Id().equals(sessionId)) {
128 tempWebSocket = webSocket;
129 session = webSocket.session;
130 break;
131 }
132 }
133 if (session != null) {
134 BasicRemote().sendText(message);
135 } else {
136 System.out.println("没有到你指定ID的会话:{}"+ "; sessionId:" + sessionId);
137 }
138 }
139
140 /**
141 * 如果使⽤springboot内置tomcat,需要配置,否则不需要 142 *
143 * @return
peg6000
144 */
145 // @Bean
146 // public ServerEndpointExporter serverEndpointExporter() {
147 // return new ServerEndpointExporter();
148 // }
149
150
151 }
上⾯类中的红⾊注释的代码是只有当springboot项⽬使⽤内置tomcat时需要配置(即打成jar包),如果使⽤外置tomcat则不需要配置(即打成war包)
3、配置编码器,主要是需要后端向前端发送对象数据,如果只是发送普通的字符串数据的话,就不需要
1 import com.alibaba.fastjson.JSON;
2 utils.MsgUtil;
3
4 import javax.websocket.EncodeException;
5 import javax.websocket.Encoder;
6 import javax.websocket.EndpointConfig;
7
8 /**
9 * @Name:EncoderClassVo
10 * @Description:编码器,防⽌发送对象出错
11 * @Version:V1.0.0
12 * @Author:mYunYu
13 * @Create Date:2018/11/15 14:43
14 */
15 public class EncoderClassVo implements Encoder.Text<MsgUtil> {
16
17 @Override
18 public void init(EndpointConfig endpointConfig) {
19
20 }
21
被代表
22 @Override
23 public void destroy() {
24
25 }
26
27 @Override
28 public String encode(MsgUtil msgUtil) throws EncodeException {
29 try{
30 JSONString(msgUtil) ;
31 }catch (Exception e){
32 e.printStackTrace() ;
33 return null;
34 }
35 }
36 }
4、接下来就是写controller层了,我这边demo只是简单的发送⼀些随机的数据,具体信息还需要根据各个项⽬需要来写
1 pojo.User;
2 utils.MsgUtil;
3 webSocket.WebSocket;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.RestController;
6
7 import javax.annotation.Resource;
8 import java.io.IOException;
9
10 /**
11 * @Name:SocketController
12 * @Description:消息发送Controller
13 * @Version:V1.0.0
14 * @Author:mYunYu
15 * @Create Date:2018/11/15 16:44
16 */
17 @RestController
18 public class SocketController {
19
20 @Resource
21 WebSocket webSocket;
22
23 /**
24 * @Name:helloManyWebSocket
25 * @Description:发消息
26 * @Author:mYunYu
27 * @Create Date:16:44 2018/11/15
28 * @Parameters:
29 * @Return:
30 */
十二五科技成就展31 @RequestMapping("many")
32 public String helloManyWebSocket(){
33
34 int i = 1 ;
35 while(i > 0){
36 i=1+(int)(Math.random()*600) ;
37 User user = new User() ;
38 user.setUserid(i+1) ;
39 user.setUsername(String.valueOf(i) + String.valueOf(i+i)) ;
40
41 //将对象转为json对象,并发送到前端
42 MsgUtil msgUtil = MsgUtil.success().addMsg("map", user);
43 webSocket.sendMessage(msgUtil);
44
45 try{
46 Thread.sleep(1000) ;
47 }catch (Exception e){
48 e.printStackTrace() ;
49 }
50 }
51
52 return "发送成功";
53 }
54
55 /**
56 * @Name:helloOneWebSocket
57 * @Description:根据session单个发送消息
58 * @Author:mYunYu
59 * @Create Date:16:44 2018/11/15
60 * @Parameters:
61 * @Return:
62 */
63 @RequestMapping("one")
64 public String helloOneWebSocket(String sessionId) throws IOException {
65 //向某个⼈发送消息
66 webSocket.sendMessage(sessionId,"你好~!,单个⽤户");
67
68 return "发送成功";
69 }
70
71
72 }
5、接下来该在前端写配置了,我这⾥使⽤的是thymeleaf模板