1. 程式人生 > >websocket在Vue裡的呼叫

websocket在Vue裡的呼叫

//初始化websocket
initWebsocket(wbUrl){
  this.websocket = new WebSocket(wbUrl);
  this.websocket.onopen = this.websocketOpen;
  this.websocket.onmessage = this.websocketMessage;
  this.websocket.onerror = this.websocketError;
},

//傳送請求
websocketOpen(){
  console.log('websocketOK');
  this.websocket.send(this.$route.query.id);
},

//websocket連結成功
websocketMessage(e){
   console.log(e)
},
//建立
created(){
   this.initWebsocket('你的websocketUrl');
},
//銷燬
destroyed(){
   this.websocket.close();
}