123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import base from '@/config/baseUrl';
- import store from '@/store';
- class socket {
- constructor(options) {
- //地址
- this.socketUrl = base.socketUrl;
- this.socketStart = false;
- this.monitorSocketError();
- this.monitorSocketClose();
- this.socketReceive();
- }
- init(callback) {
- const _this = this;
- if (base.socketUrl) {
- if(this.socketStart){
- }else{
- uni.connectSocket({
- url: this.socketUrl,
- method: 'GET'
- });
- uni.onSocketOpen((res) => {
- this.socketStart = true;
- callback && callback();
- });
- setTimeout(() => {
- _this.getHeartbeat();
- }, 5000);
- }
- }else{
- }
- }
- //Socket给服务器发送消息
- send(data, callback) {
- const _this = this;
- if (store.state.userInfo.uid) {
- data.userUid = store.state.userInfo.uid;
- }
- uni.sendSocketMessage({
- data: JSON.stringify(data),
- success: () => {
- callback && callback(true);
- },
- fail: () => {
- callback && callback(false);
- }
- });
- }
- //Socket接收服务器发送过来的消息
- socketReceive() {
- const _this = this;
- uni.onSocketMessage(function(res) {
- let data = JSON.parse(res.data);
- console.log('收到服务器内容:', data);
- _this.acceptMessage && _this.acceptMessage(data);
- });
- }
- //关闭Socket
- closeSocket() {
- uni.closeSocket();
- _this.socketStart = false;
- }
- //监听Socket关闭
- monitorSocketClose() {
- const _this = this;
- uni.onSocketClose(function(res) {
- console.log('WebSocket 已关闭!');
- _this.socketStart = false;
- setTimeout(function() {
- _this.init();
- }, 3000);
- });
- }
- //监听Socket错误
- monitorSocketError() {
- const _this = this;
- uni.onSocketError(function(res) {
- _this.socketStart = false;
- console.log('WebSocket连接打开失败,请检查!');
- });
- }
- //心跳
- getHeartbeat() {
- const _this = this;
- this.send({
- type: "心跳",
- userUid: store.state.userInfo.userUid
- }, (val) => {
- setTimeout(() => {
- if (val) {
- _this.getHeartbeat();
- } else {
- _this.init();
- }
- }, 10000);
- });
- }
- };
- const mySocket = new socket();
- export default mySocket;
|