server.mock.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. WebSocketMock = require('./websocket.mock.js').WebSocketMock
  2. Stomp = require('../../lib/stomp.js').Stomp
  3. console = require 'console'
  4. class StompServerMock extends WebSocketMock
  5. # WebSocketMock handlers
  6. handle_send: (msg) =>
  7. @stomp_dispatch(Stomp.Frame.unmarshall(msg)[0])
  8. handle_close: =>
  9. @_shutdown()
  10. handle_open: =>
  11. @stomp_init()
  12. @_accept()
  13. # Stomp server implementation
  14. stomp_init: ->
  15. @transactions = {}
  16. @subscriptions = {}
  17. @messages = []
  18. stomp_send: (command, headers, body=null) ->
  19. @_respond(Stomp.Frame.marshall(command, headers, body))
  20. stomp_send_receipt: (frame) ->
  21. if frame.headers.message?
  22. @stomp_send("ERROR", {'receipt-id': frame.headers['receipt-id'], 'message': frame.headers.message})
  23. else
  24. @stomp_send("RECEIPT", {'receipt-id': frame.headers['receipt-id']})
  25. stomp_send_message: (destination, subscription, message_id, body) ->
  26. @stomp_send("MESSAGE", {
  27. 'destination': destination,
  28. 'message-id': message_id,
  29. 'subscription': subscription}, body)
  30. stomp_dispatch: (frame) ->
  31. handler = "stomp_handle_#{frame.command.toLowerCase()}"
  32. if this[handler]?
  33. this[handler](frame)
  34. if frame.receipt
  35. @stomp_send_receipt(frame)
  36. else
  37. console.log "StompServerMock: Unknown command: #{frame.command}"
  38. stomp_handle_connect: (frame) ->
  39. @session_id = Math.random()
  40. @stomp_send("CONNECTED", {'session': @session_id})
  41. stomp_handle_begin: (frame) ->
  42. @transactions[frame.headers.transaction] = []
  43. stomp_handle_commit: (frame) ->
  44. transaction = @transactions[frame.headers.transaction]
  45. for frame in transaction
  46. @messages.push(frame.body)
  47. delete @transactions[frame.headers.transaction]
  48. stomp_handle_abort: (frame) ->
  49. delete @transactions[frame.headers.transaction]
  50. stomp_handle_send: (frame) ->
  51. if frame.headers.transaction
  52. @transactions[frame.headers.transaction].push(frame)
  53. else
  54. @messages.push(frame)
  55. stomp_handle_subscribe: (frame) ->
  56. sub_id = frame.headers.id or Math.random()
  57. cb = (id, body) => @stomp_send_message(frame.headers.destination, sub_id, id, body)
  58. @subscriptions[sub_id] = [frame.headers.destination, cb]
  59. stomp_handle_unsubscribe: (frame) ->
  60. if frame.headers.id in Object.keys(@subscriptions)
  61. delete @subscriptions[frame.headers.id]
  62. else
  63. frame.headers.message = "Subscription does not exist"
  64. stomp_handle_disconnect: (frame) ->
  65. @_shutdown()
  66. # Test helpers
  67. test_send: (sub_id, message) ->
  68. msgid = 'msg-' + Math.random()
  69. @subscriptions[sub_id][1](msgid, message)
  70. exports.StompServerMock = StompServerMock