PageRenderTime 47ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Prototypes/Client2/message-queue.js

https://bitbucket.org/EthanLevine/senior-design
JavaScript | 115 lines | 93 code | 8 blank | 14 comment | 18 complexity | e236c59591d258a76805ebb5aa9502e9 MD5 | raw file
  1. // message-queue.js
  2. // a message queuing service for the UConnocalypse client
  3. //
  4. // This library provides a simple message queue that supports continuations on
  5. // receiving confirmations for sent messages. Basically, when a message is
  6. // sent, a handler is returned that can be used to attach callbacks that are
  7. // called when the message's response is received for the server.
  8. //
  9. // For documentation on using this library, see the project's wiki at:
  10. // https://bitbucket.org/EthanLevine/senior-design/wiki/message-queue.js
  11. var messageQueue = (function () {
  12. "use strict";
  13. var onMessageReceive = function (mq, messageData) {
  14. var handler, cb;
  15. if (messageData[0] === "confirm") {
  16. handler = mq.callbackQueue.shift();
  17. cb = handler.confirmCallback;
  18. if (typeof cb === "function") {
  19. cb.apply(messageData, messageData.slice(2));
  20. }
  21. } else if (messageData[0] === "error") {
  22. handler = mq.callbackQueue.shift();
  23. cb = handler.errorCallbacks[messageData[1]];
  24. if (typeof cb === "function") {
  25. cb.apply(messageData, messageData.slice(1));
  26. } else {
  27. // try general error handler for this message
  28. cb = handler.errorGeneralCallback;
  29. if (typeof cb === "function") {
  30. cb.apply(messageData, messageData.slice(1));
  31. } else {
  32. // call the uncaught error handler.
  33. cb = mq.uncaughtErrorCallback;
  34. cb.apply(messageData, messageData.slice(1));
  35. }
  36. }
  37. } else {
  38. // neither confirm nor error - must be passed to mq's
  39. // general handler.
  40. cb = mq.generalCallback;
  41. cb(messageData);
  42. }
  43. };
  44. var MessageQueueHandler = function () {
  45. this.confirmCallback = null;
  46. this.errorCallbacks = {};
  47. this.errorGeneralCallback = null;
  48. };
  49. MessageQueueHandler.prototype = {};
  50. MessageQueueHandler.prototype.onconfirm = function (callback) {
  51. this.confirmCallback = callback;
  52. return this;
  53. };
  54. MessageQueueHandler.prototype.onerror = function (callback, errorCode) {
  55. if (typeof errorCode === "undefined") {
  56. this.errorGeneralCallback = callback;
  57. } else {
  58. this.errorCallbacks[errorCode] = callback;
  59. }
  60. return this;
  61. };
  62. var MessageQueue = function (socket, generalCallback, uncaughtErrorCallback) {
  63. this.ws = socket;
  64. this.callbackQueue = [];
  65. this.generalCallback = generalCallback;
  66. this.uncaughtErrorCallback = uncaughtErrorCallback;
  67. this.queuedMessages = [];
  68. var thisMessageQueue = this;
  69. this.ws.onmessage = function (event) {
  70. onMessageReceive(thisMessageQueue, JSON.parse(event.data));
  71. };
  72. this.ws.onopen = function (event) {
  73. while (thisMessageQueue.queuedMessages.length > 0) {
  74. thisMessageQueue.ws.send(JSON.stringify(thisMessageQueue.queuedMessages.shift()));
  75. }
  76. };
  77. this.ws.onerror = function (event) {
  78. thisMessageQueue.uncaughtErrorCallback(3, "An error occurred with the underlying websocket.");
  79. };
  80. };
  81. MessageQueue.prototype = {};
  82. MessageQueue.prototype.send = function (messageData) {
  83. var cbo = new MessageQueueHandler();
  84. this.callbackQueue.push(cbo);
  85. if (this.ws.readyState === 0) {
  86. this.uncaughtErrorCallback("Not yet connected to server");
  87. }
  88. else if(this.ws.readyState === 3){
  89. this.uncaughtErrorCallback("Couldn't Connect to WebSocket");
  90. }
  91. else {
  92. this.ws.send(JSON.stringify(messageData));
  93. }
  94. return cbo;
  95. };
  96. return {
  97. create: function (serverAddress, generalCallback, uncaughtErrorCallback) {
  98. if (typeof generalCallback !== "function") {
  99. generalCallback = function () {};
  100. }
  101. if (typeof uncaughtErrorCallback !== "function") {
  102. uncaughtErrorCallback = function () {};
  103. }
  104. return new MessageQueue(new WebSocket(serverAddress), generalCallback, uncaughtErrorCallback);
  105. }
  106. };
  107. })();