mqantlib.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. 'use strict';
  2. /**
  3. * Created by liangdas on 17/2/25.
  4. * Email 1587790525@qq.com
  5. */
  6. var hashmap = function () {
  7. }
  8. hashmap.prototype = {
  9. constructor: hashmap,
  10. add: function (k, v) {
  11. if (!this.hasOwnProperty(k)) {
  12. this[k] = v;
  13. }
  14. },
  15. remove: function (k) {
  16. if (this.hasOwnProperty(k)) {
  17. delete this[k];
  18. }
  19. },
  20. update: function (k, v) {
  21. this[k] = v;
  22. },
  23. has: function (k) {
  24. var type = typeof k;
  25. if (type === 'string' || type === 'number') {
  26. return this.hasOwnProperty(k);
  27. } else if (type === 'function' && this.some(k)) {
  28. return true;
  29. }
  30. return false;
  31. },
  32. clear: function () {
  33. for (var k in this) {
  34. if (this.hasOwnProperty(k)) {
  35. delete this[k];
  36. }
  37. }
  38. },
  39. empty: function () {
  40. for (var k in this) {
  41. if (this.hasOwnProperty(k)) {
  42. return false;
  43. }
  44. }
  45. return true;
  46. },
  47. each: function (fn) {
  48. for (var k in this) {
  49. if (this.hasOwnProperty(k)) {
  50. fn.call(this, this[k], k, this);
  51. }
  52. }
  53. },
  54. map: function (fn) {
  55. var hash = new Hash;
  56. for (var k in this) {
  57. if (this.hasOwnProperty(k)) {
  58. hash.add(k, fn.call(this, this[k], k, this));
  59. }
  60. }
  61. return hash;
  62. },
  63. filter: function (fn) {
  64. var hash = new Hash;
  65. for (var k in this) {
  66. }
  67. },
  68. join: function (split) {
  69. split = split !== undefined ? split : ',';
  70. var rst = [];
  71. this.each(function (v) {
  72. rst.push(v);
  73. });
  74. return rst.join(split);
  75. },
  76. every: function (fn) {
  77. for (var k in this) {
  78. if (this.hasOwnProperty(k)) {
  79. if (!fn.call(this, this[k], k, this)) {
  80. return false;
  81. }
  82. }
  83. }
  84. return true;
  85. },
  86. some: function (fn) {
  87. for (var k in this) {
  88. if (this.hasOwnProperty(k)) {
  89. if (fn.call(this, this[k], k, this)) {
  90. return true;
  91. }
  92. }
  93. }
  94. return false;
  95. },
  96. find: function (k) {
  97. var type = typeof k;
  98. if (type === 'string' || type === 'number' && this.has(k)) {
  99. return this[k];
  100. } else if (type === 'function') {
  101. for (var _k in this) {
  102. if (this.hasOwnProperty(_k) && k.call(this, this[_k], _k, this)) {
  103. return this[_k];
  104. }
  105. }
  106. }
  107. return null;
  108. }
  109. };
  110. var mqant = function () {
  111. }
  112. mqant.prototype = {
  113. constructor: window.mqant,
  114. curr_id: 0,
  115. client:null,
  116. waiting_queue:new hashmap(),
  117. init:function(prop){
  118. prop["onFailure"]=prop["onFailure"]||function () {
  119. console.log("onFailure");
  120. }
  121. prop["onConnectionLost"]=prop["onConnectionLost"]||function (responseObject) {
  122. if (responseObject.errorCode !== 0) {
  123. console.log("onConnectionLost:" + responseObject.errorMessage);
  124. console.log("连接已断开");
  125. }
  126. }
  127. prop["useSSL"]=prop["useSSL"]||false
  128. this.client = new Paho.MQTT.Client(prop["host"], prop["port"], prop["client_id"]);
  129. this.client.connect({
  130. onSuccess: prop["onSuccess"],
  131. onFailure: prop["onFailure"],
  132. mqttVersion: 3,
  133. useSSL:prop["useSSL"],
  134. cleanSession: true,
  135. });//连接服务器并注册连接成功处理事件
  136. this.client.onConnectionLost =prop["onConnectionLost"] ;//注册连接断开处理事件
  137. this.client.onMessageArrived = onMessageArrived;//注册消息接收处理事件
  138. var self=this
  139. function onMessageArrived(message) {
  140. try{
  141. var callback=self.waiting_queue.find(message.destinationName)
  142. if(callback!=null){
  143. //有等待消息的callback 还缺一个信息超时的处理机制
  144. var h=message.destinationName.split("/")
  145. if(h.length>2){
  146. //这个topic存在msgid 那么这个回调只使用一次
  147. self.waiting_queue.remove(message.destinationName)
  148. }
  149. callback(message)
  150. }
  151. }catch(e) {
  152. console.log(e);
  153. }
  154. }
  155. },
  156. /**
  157. * 向服务器发送一条消息
  158. * @param topic
  159. * @param msg
  160. * @param callback
  161. */
  162. request:function(topic,msg,callback){
  163. this.curr_id=this.curr_id+1
  164. var topic=topic+"/"+this.curr_id //给topic加一个msgid 这样服务器就会返回这次请求的结果,否则服务器不会返回结果
  165. var payload=JSON.stringify(msg)
  166. this.on(topic,callback)
  167. this.client.send(topic,payload ,0);
  168. },
  169. /**
  170. * 向服务器发送一条消息,但不要求服务器返回结果
  171. * @param topic
  172. * @param msg
  173. */
  174. requestNR:function(topic,msg){
  175. var payload=JSON.stringify(msg)
  176. this.client.send(topic,payload ,0);
  177. },
  178. /**
  179. * 监听指定类型的topic消息
  180. * @param topic
  181. * @param callback
  182. */
  183. on:function(topic,callback){
  184. //服务器不会返回结果
  185. this.waiting_queue.add(topic,callback) //添加这条消息到等待队列
  186. }
  187. }
  188. window.mqant=new mqant()