messageHandler.js 609 B

12345678910111213141516171819202122232425262728
  1. 'use strict'
  2. import { formatLog } from '../functions'
  3. import { wsLogger } from '../../config'
  4. /**
  5. * @typedef {Function} MessageHandler
  6. * @param {string} data a message received from a client
  7. */
  8. /**
  9. * Treat received message from a WebSocket client
  10. * @param {object} ws a WebSocket connected client
  11. * @returns {MessageHandler} the message handler
  12. */
  13. const messageHandler = ws => async data => {
  14. let json
  15. try {
  16. json = JSON.parse(data)
  17. }
  18. catch (err) {
  19. throw new Error('Invalid JSON data.')
  20. }
  21. wsLogger.info(formatLog(json, 'message'))
  22. ws.send('ok')
  23. }
  24. export default messageHandler