let openSocket = require('../../config/socket')
let app = getApp()
let socket = null
Page({
data: {
zl: [[422, 400, 468, 834, 785, 446, 845, 517, 630, 797, 890, 529, 553, 425, 469, 470, 837, 841, 521, 525], [422, 400, 468, 834, 785, 446, 845, 517, 630, 797, 890, 529, 553, 425, 469, 470, 837, 841, 521, 525]]
},
onLoad: function () {
let that = this;
//socket调用
openSocket.connect(function (status, ws) {
if (status) {
socket = ws
this.subscribe('zl')//对封装好对订阅方法进行调用
socket.on('broadcast', function (msg) {//广播
console.log("broadcast");
console.log(msg);
})
} else {
alert("socket 连接失败")
}
});
},
subscribe: function (type) {
if (socket) {
let eis = this.data[type]
if (eis && eis.length > 0) {
let param = {//仅供参考,根据接口自行更改
eis: eis.join(',')
}
socket.emit('subscribe', JSON.stringify(param));
}
}
}
});
|