vue使用socket.io长连接

2021-12-22  本文已影响0人  小北呀_

1.main.js

import VueSocketio from 'vue-socket.io';
Vue.use(new VueSocketio({
  debug: true,
  connection: 'http://xxxxxxxxxxx',
  options: { 'transports': ['websocket']} //Optional options
}))

2.index.vue

<template>
   <div>socket首页</div>
</template>
<script>
   import Vue from 'vue';
   import socket from 'vue-socket.io';
   Vue.use(new socket({
       debug: true,
       connection: 'http://172.16.8.61:5000/websocket/monitor/detail',
       options: { 'transports': ['websocket']} //Optional options
   }))

   export default {
       name: "Socket",
       sockets: {
           connect(){
             window.console.log('socket连接成功');
           },
           response(data){
               window.console.log(data,'后台返回数据');
           }
       },
       mounted() {
           this.$socket.emit('monitor',{'satellite_id':'GS-1a'})
       },
   }
</script>

<style scoped>

</style>

上一篇 下一篇

猜你喜欢

热点阅读