import * as https from './utils';
var link_socket : any;
export function linkSocket() {
if (link_socket) {
return;
};
link_socket = uni.connectSocket({
url: 'ws://8.210.67.139:8451', //域名需替换
complete: () => { }
});
link_socket.onOpen(function (res:any) {
})
link_socket.onMessage(function (res:any) {
let data = JSON.parse(res.data);
if(data.client_id ) {
if(uni.getStorageSync('token')) {
//接口请求需替换
https.http_post('community/bindSocket',{client_id:data.client_id}).then((res:any)=>{
})
} else {
link_socket.close({});
}
} else {
uni.$emit('chat_message',data);
}
})
link_socket.onClose(function () {
link_socket = null;
setTimeout(function () {
linkSocket()
}, 3000)
})
link_socket.onError(function () {
link_socket = null;
setTimeout(function () {
linkSocket()
}, 3000)
})
}
//关闭Socket
export function closeSocket() {
if (link_socket) {
link_socket.close({});
link_socket = null;
}
}
uniapp封装socket请求
最新推荐文章于 2024-04-26 10:50:51 发布