socket.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. const { Controller } = require('ee-core');
  3. const socket = require('../utils/socket')
  4. const pySocket = new socket()
  5. class SocketController extends Controller {
  6. constructor(ctx) {
  7. super(ctx);
  8. }
  9. /**
  10. * Connect to WebSocket server
  11. */
  12. async connect() {
  13. await new Promise(async (resolve,reject) => {
  14. pySocket.init(this.app)
  15. })
  16. }
  17. /**
  18. * 发送 ping 消息
  19. */
  20. sendPing() {
  21. pySocket.sendPing()
  22. }
  23. /**
  24. * 发送消息到服务器
  25. * @param {string} message - JSON 字符串
  26. */
  27. sendMessage(message) {
  28. pySocket.sendMessage(message)
  29. }
  30. /**
  31. * 断开连接
  32. */
  33. disconnect() {
  34. pySocket.disconnect()
  35. }
  36. /**
  37. * 获取 MCU 错误日志列表
  38. */
  39. getMcuErrorLogs() {
  40. return pySocket.getMcuErrorLogs();
  41. }
  42. /**
  43. * 获取 MCU 错误日志未读数
  44. */
  45. getMcuErrorUnreadCount() {
  46. return pySocket.getMcuErrorUnreadCount();
  47. }
  48. /**
  49. * 清除 MCU 错误日志未读状态
  50. */
  51. clearMcuErrorUnread() {
  52. return pySocket.clearMcuErrorUnread();
  53. }
  54. /**
  55. * 设置 MCU 错误日志最大条数
  56. * @param {number} max - 最大条数
  57. */
  58. setMcuErrorLogMax(max) {
  59. return pySocket.setMcuErrorLogMax(max);
  60. }
  61. }
  62. SocketController.toString = () => '[class SocketController]';
  63. module.exports = SocketController;