1.下載安裝
npm install vue-socket.io --save
2.在main.js中引用
import VueSocketIO from 'vue-socket.io'
Vue.use(new VueSocketIO({
debug: true,
// connection: SocketIO ('http://127.0.0.1:5000',{'timeout': 300000,'reconnectionDelayMax':1000,'reconnectionDelay':500}),
connection: 'http://localhost:5000',
options: { 'timeout': 300000,'reconnectionDelayMax':1000,'reconnectionDelay':500}
}))
new Vue({
el: '#app',
router,
store,
gsap,
render: h => h(App)
})
注:connection 連接方式很重要,我第一種寫的
connection: SocketIO ('http://127.0.0.1:5000',{'timeout': 300000,'reconnectionDelayMax':1000,'reconnectionDelay':500}),是不可行的
3.在mounted中執行連接
mounted() {
this.$socket.emit('startRead');//發布消息
this.sockets.subscribe('card message', (msg) => {//訂閱消息,返回的是服務端發送的消息
var data = base.decode(msg);
var data=JSON.parse(data);
console.log("連接成功")
this.studentInfo=data
console.log("retcode:" + data.retcode + "retmsg:" + data.retmsg + " errmsg:" + data.errmsg + "卡類型:"+data.CardType+ " 姓名:" + data.name +" 英文姓名:"+data.EngName+" 性別:" + data.sex + " 民族:" + data.nation + " 出生:" + data.born + " 住址:" + data.address +" 身份證號:" + data.cardno + " 有效期開始:" + data.userlifeb + " 有效期結束:" + data.userlifee + " 發證機關:" + data.police +" 指紋信息:" + data.FpDescribe+" 指紋特征值base64:"+data.FpData +"通行證號碼:" + data.PassID +"簽發次數:" + data.IssuesTimes )
})
},
