You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
zhishifufei_uniapp/libs/socket.js

74 lines
1.5 KiB

import config from "@/config";
const { APP_WS_URL } = config;
class Socket {
constructor() {
}
onSocketOpen(my) {
uni.$emit('socketOpen', my)
}
init() {
var that = this;
this.timer = setInterval(function () {
that.send({
type: "ping"
});
}, 10000);
}
send(data) {
let datas = JSON.stringify(Object.assign(data, {
channel: "kefu"
}));
return uni.sendSocketMessage({
data: datas
});
}
onMessage(res) {
const {
type,
data = {}
} = JSON.parse(res.data);
uni.$emit(type, data)
}
onClose() {
uni.closeSocket()
clearInterval(this.timer);
uni.$emit("socket_close");
}
onError(e) {
uni.$emit("socket_error", e);
}
close() {
uni.closeSocket();
}
onStart() {
this.ws = uni.connectSocket({
url: APP_WS_URL,
header: {
'content-type': 'application/json'
},
method: 'GET',
success: (res) => { }
});
this.ws.onOpen(this.onSocketOpen.bind(this))
this.ws.onError(this.onError.bind(this));
this.ws.onMessage(this.onMessage.bind(this))
this.ws.onClose(this.onClose.bind(this));
}
// close() {
// clearInterval(this.timer);
// this.ws.close();
// },
}
export default Socket;