# websocket实现二维码扫描
# 后端代码
# 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
1
2
3
4
2
3
4
# 配置类
@Configuration
public class WebSocketConfig {
/**
* 注入ServerEndpointExporter,
* 这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 处理层
1、可以把@ServerEndpoint
理解成@Controller
;
2、权限部分记得要处理好,我这里为了省事,在权限部分直接配置了白名单;
@ServerEndpoint(value = "/websocket/{shopId}")
@Component
public class WebSocketServer {
private Session session;
private static CopyOnWriteArraySet<WebSocketServer> webSockets =new CopyOnWriteArraySet<>();
private static Map<String,Session> sessionPool = new HashMap<>();
@OnOpen
public void onOpen(Session session, @PathParam(value="shopId")String shopId) {
this.session = session;
webSockets.add(this);
sessionPool.put(shopId, session);
System.out.println("【websocket消息】有新的连接,总数为:"+webSockets.size());
}
@OnClose
public void onClose() {
webSockets.remove(this);
System.out.println("【websocket消息】连接断开,总数为:"+webSockets.size());
}
@OnMessage
public void onMessage(String message) {
System.out.println("【websocket消息】收到客户端消息:"+message);
}
// 此为广播消息
public void sendAllMessage(String message) {
for(WebSocketServer webSocket : webSockets) {
System.out.println("【websocket消息】广播消息:"+message);
try {
webSocket.session.getAsyncRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 此为单点消息
public void sendOneMessage(String shopId, String message) {
Session session = sessionPool.get(shopId);
if (session != null) {
try {
session.getAsyncRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 前端代码
<template>
<el-container style="height: 100%">
<div class="qrcode" ref="qrCodeUrl"></div>
</el-container>
</template>
<script>
import QRCode from 'qrcodejs2'
export default {
name: "",
data() {
return {
deliveryCode:null,
qrcode:null,
websock: null,
};
},
//加载后运行
created(){
this.deliveryCode=sessionStorage.getItem('deliveryCode');
//创建二维码
if (this.qrcode == null) {
this.$nextTick(() => {
this.creatQrCode();
})
}
//初始化webSocket
this.initWebSocket();
},
destroyed() {
this.websock.close() //离开路由之后断开websocket连接
},
methods: {
creatQrCode() {
this.qrcode = new QRCode(this.$refs.qrCodeUrl, {
text: this.deliveryCode,
width: 250,
height: 250,
colorDark: '#000000',
colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.H
})
},
initWebSocket(){ //初始化weosocket
if(typeof(WebSocket) === "undefined"){
alert("您的浏览器不支持socket")
}else {
const wsuri = "ws://tms.aupri.cn:8079/websocket/"+this.deliveryCode;
this.websock = new WebSocket(wsuri);
this.websock.onmessage = this.websocketonmessage;
this.websock.onopen = this.websocketonopen;
this.websock.onerror = this.websocketonerror;
this.websock.onclose = this.websocketclose;
}
},
websocketonopen(){ //连接建立之后执行send方法发送数据
console.log("websocket连接成功")
alert("成功");
},
websocketonerror(){//连接建立失败重连
this.initWebSocket();
},
websocketonmessage(e){ //数据接收
console.log("收到消息:"+e.data);
//只要接收到消息就置灰二维码(省略...)
},
websocketsend(Data){//数据发送
this.websock.send(Data);
},
websocketclose(e){ //关闭
console.log('websocket断开连接',e);
alert("失败");
},
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
扫码的接口:(在扫码成功之后的接口调用,告诉后台这个二维码被我扫到了)
@Autowired
private WebSocketServer webSocketServer;
@GetMapping("/scanSuccess/{deliveryCode}")
public String scanSuccess(@PathVariable("deliveryCode") String deliveryCode){
//
webSocketServer.sendOneMessage(deliveryCode, "成功");
return "success";
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11