App.vue 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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:8888/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. }
  47. </style>