websocket建立连接有些时候能成功,有些时候不能成功。
连接成功并订阅主题后,有些时候能成功收到后台推送的消息,但是大多数时候不能收到,后台消息一直在推送。
let socketUrl="xxxxxx";
var sockJS = new SockJS(socketUrl);
this.stompClient = Stomp.over(sockJS);
window.GpsWebSocket = this.stompClient;
window.GpsWebSocket.onclose=this.WebSocketClose;
window.GpsWebSocket.onerror=this.WebSocketError;
this.stompClient.connect({}, () => {
console.log("WebSocket已连接!");
this.stompClient.subscribe("/users/" + xx + "/xxx", (msg) => {},{})
}, function () {
console.log("WebSocket连接失败!")
});
let path = "xxxx"
let socket = ""
init: function() {
if (typeof WebSocket === "undefined") {
alert("您的浏览器不支持socket");
} else {
// 实例化socket
this.socket = new WebSocket(this.path);
// 监听socket连接
this.socket.onopen = this.open;
// 监听socket错误信息
this.socket.onerror = this.error;
// 监听socket消息
this.socket.onmessage = this.getMessage;
}
},
open: function() {
console.log("socket连接成功");
},
error: function() {
swd.toast({
type: "fail",
content: "连接错误"
});
console.log("连接错误");
},
//获取后台返回数据
getMessage: function(msg) {
// console.log("这是什么", JSON.parse(msg.data));
},
send: function() {
this.socket.send(params);
},
close: function() {
console.log("socket已经关闭");
},