taskmaster-websocket.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * TASKMASTER WEBSOCKET UTILITIES
  3. * ==============================
  4. *
  5. * Utilities for broadcasting TaskMaster state changes via WebSocket.
  6. * Integrates with the existing WebSocket system to provide real-time updates.
  7. */
  8. /**
  9. * Broadcast TaskMaster project update to all connected clients
  10. * @param {WebSocket.Server} wss - WebSocket server instance
  11. * @param {string} projectName - Name of the updated project
  12. * @param {Object} taskMasterData - Updated TaskMaster data
  13. */
  14. export function broadcastTaskMasterProjectUpdate(wss, projectName, taskMasterData) {
  15. if (!wss || !projectName) {
  16. console.warn('TaskMaster WebSocket broadcast: Missing wss or projectName');
  17. return;
  18. }
  19. const message = {
  20. type: 'taskmaster-project-updated',
  21. projectName,
  22. taskMasterData,
  23. timestamp: new Date().toISOString()
  24. };
  25. wss.clients.forEach((client) => {
  26. if (client.readyState === 1) { // WebSocket.OPEN
  27. try {
  28. client.send(JSON.stringify(message));
  29. } catch (error) {
  30. console.error('Error sending TaskMaster project update:', error);
  31. }
  32. }
  33. });
  34. }
  35. /**
  36. * Broadcast TaskMaster tasks update for a specific project
  37. * @param {WebSocket.Server} wss - WebSocket server instance
  38. * @param {string} projectName - Name of the project with updated tasks
  39. * @param {Object} tasksData - Updated tasks data
  40. */
  41. export function broadcastTaskMasterTasksUpdate(wss, projectName, tasksData) {
  42. if (!wss || !projectName) {
  43. console.warn('TaskMaster WebSocket broadcast: Missing wss or projectName');
  44. return;
  45. }
  46. const message = {
  47. type: 'taskmaster-tasks-updated',
  48. projectName,
  49. tasksData,
  50. timestamp: new Date().toISOString()
  51. };
  52. wss.clients.forEach((client) => {
  53. if (client.readyState === 1) { // WebSocket.OPEN
  54. try {
  55. client.send(JSON.stringify(message));
  56. } catch (error) {
  57. console.error('Error sending TaskMaster tasks update:', error);
  58. }
  59. }
  60. });
  61. }
  62. /**
  63. * Broadcast MCP server status change
  64. * @param {WebSocket.Server} wss - WebSocket server instance
  65. * @param {Object} mcpStatus - Updated MCP server status
  66. */
  67. export function broadcastMCPStatusChange(wss, mcpStatus) {
  68. if (!wss) {
  69. console.warn('TaskMaster WebSocket broadcast: Missing wss');
  70. return;
  71. }
  72. const message = {
  73. type: 'taskmaster-mcp-status-changed',
  74. mcpStatus,
  75. timestamp: new Date().toISOString()
  76. };
  77. wss.clients.forEach((client) => {
  78. if (client.readyState === 1) { // WebSocket.OPEN
  79. try {
  80. client.send(JSON.stringify(message));
  81. } catch (error) {
  82. console.error('Error sending TaskMaster MCP status update:', error);
  83. }
  84. }
  85. });
  86. }
  87. /**
  88. * Broadcast general TaskMaster update notification
  89. * @param {WebSocket.Server} wss - WebSocket server instance
  90. * @param {string} updateType - Type of update (e.g., 'initialization', 'configuration')
  91. * @param {Object} data - Additional data about the update
  92. */
  93. export function broadcastTaskMasterUpdate(wss, updateType, data = {}) {
  94. if (!wss || !updateType) {
  95. console.warn('TaskMaster WebSocket broadcast: Missing wss or updateType');
  96. return;
  97. }
  98. const message = {
  99. type: 'taskmaster-update',
  100. updateType,
  101. data,
  102. timestamp: new Date().toISOString()
  103. };
  104. wss.clients.forEach((client) => {
  105. if (client.readyState === 1) { // WebSocket.OPEN
  106. try {
  107. client.send(JSON.stringify(message));
  108. } catch (error) {
  109. console.error('Error sending TaskMaster update:', error);
  110. }
  111. }
  112. });
  113. }