当前位置:   article > 正文

Vue使用socket实现实时通信

Vue使用socket实现实时通信

一、新建socket文件

  1. class SocketService {
  2. constructor() {
  3. this.socket = null;
  4. this.reconnectTimer = null;
  5. this.messageCallback = null;
  6. this.connectionParams = null;
  7. this.path=null
  8. }
  9. init() {
  10. this.clearReconnectTimer(); // 尝试重新连接之前先清除重连定时器
  11. if (typeof WebSocket === "undefined") {
  12. throw new Error("您的浏览器不支持WebSocket");
  13. }
  14. this.socket = new WebSocket(this.path);
  15. this.socket.addEventListener("open", this.open.bind(this));
  16. this.socket.addEventListener("error", this.error.bind(this));
  17. this.socket.addEventListener("close", this.close.bind(this));
  18. this.socket.addEventListener("message", this.getMessage.bind(this));
  19. }
  20. open() {
  21. console.log('%c [ WebSocket已连接 ]', 'font-size:16px; background:#7fce50; color:white;')
  22. this.clearReconnectTimer();
  23. }
  24. close(event) {
  25. console.log('%c [ WebSocket已关闭并断开连接 ]', 'font-size:16px; background:red; color:white;')
  26. this.destroyWebsocket();
  27. }
  28. error(event) {
  29. console.log('%c [ WebSocket已断开连接 ]', 'font-size:16px; background:red; color:white;')
  30. this.startReconnectTimer();
  31. }
  32. getMessage(event) {
  33. if (event.data === "连接成功") {
  34. return;
  35. }
  36. const message = event.data;
  37. if (this.messageCallback) {
  38. this.messageCallback(message);
  39. }
  40. }
  41. startReconnectTimer() {
  42. if (!this.reconnectTimer) {
  43. this.reconnectTimer = setInterval(() => {
  44. console.log('%c [ WebSocket已断开 尝试重新连接... ]', 'font-size:16px; background:red; color:white;')
  45. if (this.connectionParams) {
  46. this.init();
  47. }
  48. }, 3000); // 重连间隔为 3 秒
  49. }
  50. }
  51. clearReconnectTimer() {
  52. if (this.reconnectTimer) {
  53. clearInterval(this.reconnectTimer);
  54. this.reconnectTimer = null;
  55. this.clearReconnectTimer();
  56. }
  57. }
  58. destroyWebsocket() {
  59. if (this.socket) {
  60. this.socket.close();
  61. this.socket = null;
  62. this.clearReconnectTimer();
  63. this.messageCallback = null; // 或者清除 messageCallback 的引用
  64. }
  65. }
  66. }
  67. const socketService = new SocketService();
  68. export default socketService;

二、引入并调用

  1. //引入
  2. import socketService from "./socket.js";
  3. //开启
  4. initSocketService() {
  5. // const id = '';
  6. const path = `${Monitor_Alarm}${this.tenantId}/${this.userInfo.id}`;
  7. // const path =
  8. // "ws://aes.aes-iot.com:8301/sys-alarm/ws/dashboard/admin/d35fd488525686145aca387b0158c234";
  9. // let url = .replace(/^https?:\/\//g, "");
  10. // const path = `ws://${url}/sys-alarm/ws/dashboard/802/10000`;
  11. // const type = "device";
  12. // 初始化 WebSocket 连接 传递类型 设备id
  13. socketService.path = path;
  14. socketService.init();
  15. socketService.messageCallback = this.handleMessage;
  16. },
  17. //接收消息后的处理
  18. handleMessage(message) {}
  19. //关闭
  20. this.closeWebsocket();

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/814705
推荐阅读
相关标签
  

闽ICP备14008679号