webSocket的基本使用
2021-01-24 23:14
标签:ref func var case open 弹窗 order class message 1.引入依赖 2.在前端页面编写WebSocket的js代码 3.配置WebSocket的配置类,具体原因:https://blog.csdn.net/qq_24283811/article/details/80584879?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase 4.编写WebSocket类 webSocket的基本使用 标签:ref func var case open 弹窗 order class message 原文地址:https://www.cnblogs.com/shouyaya/p/13246494.html dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-websocketartifactId>
dependency>
script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js">script>
script src="https://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js">script>
script>
var webSocket=null;
if(‘WebSocket‘in window){
//使用WebSocket要使用ws协议,将http替换成ws
webSocket=new WebSocket(‘ws://shouyaya.natapp1.cc/sell/webSocket‘);
}else{
alert(‘你的浏览器不支持webSocket‘);
}
webSocket.onopen=function (event) {
console.log(‘建立连接‘);
}
webSocket.onclose=function (event) {
console.log(‘断开连接‘);
}
webSocket.onmessage=function (event) {
//弹窗和播放音乐
$(‘#Mymodal‘).modal(‘show‘);
document.getElementById(‘orderSong‘).play();
}
webSocket.onerror=function (event) {
console.log(‘webSocket发生错误‘);
}
window.onbeforeunload=function () {
webSocket.close();
}
script>
@Component
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}
@Component
@Slf4j
@ServerEndpoint("/webSocket")
public class WebSocket {
private Session session;
private static CopyOnWriteArraySet
上一篇:js基础
下一篇:js中字符串常用方法