在renderjs中写html原生的webSocket倒是可以用,也是没有办法的办法了。有人知道正常写法的话麻烦告诉一下。
template:
<view id="weighWs" :prop="weighWsMessage" :change:prop="weighWs.updateWeighWs"></view>
vue的script:
data(){
return {
weighWsMessage: {}
}
},
methods:{
changeWeight(val) {
this.centerWeight = val
},
connectWs(){
// 加个时间戳使得每次都能触发updateWeighWs
this.weighWsMessage = {type: 'connectWs', value: 'ws://xxxxxxxx', timeStamp: new Date()}
},
closeWs(){
this.weighWsMessage = {type: 'closeWs', timeStamp: new Date()}
}
}
renderjs:
<script module="weighWs" lang="renderjs">
// #ifndef H5
let ws
export default {
mounted() {
this.connectWs('ws://192.168.xxxx')
},
destroyed() {
this.closeWs()
},
methods: {
updateWeighWs(newVal) {
if (newVal.type === 'closeWs') {
this.closeWs()
} else if (newVal.type === 'connectWs') {
this.connectWs(newVal.value)
}
},
closeWs() {
ws.close()
ws = null
},
connectWs(url) {
ws && this.closeWs()
const _this = this
ws = new WebSocket(url);
ws.onopen = function (evt) {
console.log("Connection open ...");
};
ws.onmessage = function (evt) {
console.log("Received Message: " + evt.data);
_this.$ownerInstance.callMethod('changeWeight', JSON.parse(evt.data).value)
};
ws.onclose = function (evt) {
console.log("Connection closed.");
};
}
}
}
// #endif
</script>
t***@sino-essence.com (作者)
试过了,还是不行
2022-06-02 15:48