pc端
created() {
this.openSoket();
},
methods: {
openSoket() {
//console.log(typeof(WebSocket))
if (typeof WebSocket === "undefined") {
alert("您的浏览器不支持socket");
} else {
if (this.global.ws.readyState != 1) {
let phone = this.$store.getters.phone;
//let path = `` WS地址;
// 实例化socket
this.ws = new WebSocket(path);
this.global.setWs(this.ws);
// 监听socket连接
this.ws.onopen = this.open;
// 监听socket错误信息
this.ws.onerror = this.error;
// 监听socket消息
this.ws.onmessage = this.getMessage;
}
}
},
// socket 链接提示
open() {
console.log("socket连接成功");
},
// socket 错误处理
error(e) {
console.log("连接错误00000000000000000000", e);
// this.open()
},
getMessage(msg) { //获取消息
if (msg != undefined) {
//let json = JSON.parse(msg.data)
if (msg.data.indexOf("{") !== -1) {
let json = JSON.parse(msg.data);
//console.log(json)