uniapp手机端 无法直接使用stomp 因为在 uniapp里 根本没有 new WebSocket(),所以我们要做的就是对uniapp 的 Socket 进行封装
直接上代码
let socketOpen = false;
let socketMsgQueue = [];
export default {
client: null,
baseURL: 'ws://***********',
header:{
login: '*****',
passcode: '******'
},
init() {
if (this.client) {
return Promise.resolve(this.client);
}
return new Promise((resolve, reject) => {
const ws = {
send: this.sendMessage,
onopen: null,
onmessage: null,
close: this.closeSocket,
};
uni.connectSocket({
url: this.baseURL,
header: this.header,
});
uni.onSocketOpen(function(res) {
console.log('WebSocket连接已打开!', res);
socketOpen = true;
for (let i = 0; i < socketMsgQueue.length; i++) {
ws.send(socketMsgQueue[i]);
}
socketMsgQueue = [];
ws.onopen && ws.onopen();
});
uni.onSocketMessage(function(res) {
ws.onmessage && ws.onmessage(res);
});
uni.onSocketError(function(res) {
console.log('WebSocket 错误!', res);
});
uni.onSocketClose((res) => {
this.client.disconnect();
this.client = null;
socketOpen = false;
console.log('WebSocket 已关闭!', res);
});
const Stomp = require('./stomp.js').Stomp;
Stomp.setInterval = function(interval, f) {
return setInterval(f, interval);
};
Stomp.clearInterval = function(id) {
return clearInterval(id);
};
const client = this.client = Stomp.over(ws);
client.connect(this.header, function() {
console.log('stomp connected');
resolve(client);
});
});
},
disconnect() {
uni.closeSocket();
},
sendMessage(message) {
if (socketOpen) {
uni.sendSocketMessage({
data: message,
});
} else {
socketMsgQueue.push(message);
}
},
closeSocket() {
console.log('closeSocket');
},
};
上述文件 命名为 websocket-uni;
调用
import WebSocket from '@/common/socket/websocket-uni';
WebSocket.init().then(client => {
client.subscribe(‘/*******’, function(res){
console.log('返回数据',JSON.stringify(res));
});
});