App.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <template>
  2. <div id="app">
  3. <router-view/>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'App',
  9. methods:{
  10. localSocket() {
  11. let that = this;
  12. if ("WebSocket" in window) {
  13. console.log("您的浏览器支持 WebSocket!");
  14. that.ws = new WebSocket(`ws:http://132.232.92.186:9001/api/chatHub`);
  15. that.$websocket.setWs(that.ws);
  16. that.ws.onopen = function() {
  17. console.log('开始连接')
  18. that.$websocket.ws.send('给后端必要的参数')
  19. };
  20. that.ws.onclose = function() {
  21. // 防链接超时,(websocket在一定时间内没有数据交互,就会断开),关闭后重启
  22. console.log("连接已关闭...");
  23. setTimeout(() => {
  24. that.localSocket();
  25. }, 2000);
  26. };
  27. } else {
  28. // 浏览器不支持 WebSocket
  29. console.log("您的浏览器不支持 WebSocket!");
  30. }
  31. }
  32. },
  33. created() {
  34. //websocket
  35. // this.localSocket()
  36. },
  37. mounted(){
  38. console.log(this.$route.meta.keepAlive==undefined);
  39. }
  40. }
  41. </script>
  42. <style>
  43. #app {
  44. margin: 0px;
  45. padding: 0px;
  46. font-family:"微软雅黑";
  47. }
  48. </style>