stomp-node.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Generated by CoffeeScript 1.7.1
  2. /*
  3. Stomp Over WebSocket http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0
  4. Copyright (C) 2013 [Jeff Mesnil](http://jmesnil.net/)
  5. */
  6. (function() {
  7. var Stomp, net, overTCP, overWS, wrapTCP, wrapWS;
  8. Stomp = require('./stomp');
  9. net = require('net');
  10. Stomp.Stomp.setInterval = function(interval, f) {
  11. return setInterval(f, interval);
  12. };
  13. Stomp.Stomp.clearInterval = function(id) {
  14. return clearInterval(id);
  15. };
  16. wrapTCP = function(port, host) {
  17. var socket, ws;
  18. socket = null;
  19. ws = {
  20. url: 'tcp:// ' + host + ':' + port,
  21. send: function(d) {
  22. return socket.write(d);
  23. },
  24. close: function() {
  25. return socket.end();
  26. }
  27. };
  28. socket = net.connect(port, host, function(e) {
  29. return ws.onopen();
  30. });
  31. socket.on('error', function(e) {
  32. return typeof ws.onclose === "function" ? ws.onclose(e) : void 0;
  33. });
  34. socket.on('close', function(e) {
  35. return typeof ws.onclose === "function" ? ws.onclose(e) : void 0;
  36. });
  37. socket.on('data', function(data) {
  38. var event;
  39. event = {
  40. 'data': data.toString()
  41. };
  42. return ws.onmessage(event);
  43. });
  44. return ws;
  45. };
  46. wrapWS = function(url) {
  47. var WebSocketClient, connection, socket, ws;
  48. WebSocketClient = require('websocket').client;
  49. connection = null;
  50. ws = {
  51. url: url,
  52. send: function(d) {
  53. return connection.sendUTF(d);
  54. },
  55. close: function() {
  56. return connection.close();
  57. }
  58. };
  59. socket = new WebSocketClient();
  60. socket.on('connect', function(conn) {
  61. connection = conn;
  62. ws.onopen();
  63. connection.on('error', function(error) {
  64. return typeof ws.onclose === "function" ? ws.onclose(error) : void 0;
  65. });
  66. connection.on('close', function() {
  67. return typeof ws.onclose === "function" ? ws.onclose() : void 0;
  68. });
  69. return connection.on('message', function(message) {
  70. var event;
  71. if (message.type === 'utf8') {
  72. event = {
  73. 'data': message.utf8Data
  74. };
  75. return ws.onmessage(event);
  76. }
  77. });
  78. });
  79. socket.connect(url);
  80. return ws;
  81. };
  82. overTCP = function(host, port) {
  83. var socket;
  84. socket = wrapTCP(port, host);
  85. return Stomp.Stomp.over(socket);
  86. };
  87. overWS = function(url) {
  88. var socket;
  89. socket = wrapWS(url);
  90. return Stomp.Stomp.over(socket);
  91. };
  92. exports.overTCP = overTCP;
  93. exports.overWS = overWS;
  94. }).call(this);