pomelo-client-proxy.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Created by shihuayang on 7/16/2016.
  3. */
  4. var EventEmitter = require('events').EventEmitter;
  5. var util = require('util');
  6. var pomelo = require('./pomelo-client.js');
  7. var COS_CLIENT_VERSION = '0.2.0';
  8. var PomeloClientProxy = function (opts) {
  9. EventEmitter.call(this);
  10. this.cosToken = "";
  11. this.lastMsgSeqId = 0;
  12. this.initCallback = null;
  13. this.heartbeatTimer = null;
  14. this.heartbeatInterval = 2000;
  15. this.heartbeatTimeoutTimes = 3;
  16. this.heartbeatTryTimes = 1;
  17. this.__realpomelo__ = new pomelo(opts);
  18. };
  19. util.inherits(PomeloClientProxy, EventEmitter);
  20. module.exports = PomeloClientProxy;
  21. var pomeloClientProxy = PomeloClientProxy.prototype;
  22. pomeloClientProxy.init = function(params, cb) {
  23. var self = this;
  24. if(!!cb && typeof cb === 'function') {
  25. self.initCallback = cb;
  26. }
  27. this.__realpomelo__.on('error', function (msg) {
  28. if (!!self.heartbeatTimer) {
  29. clearInterval(self.heartbeatTimer);
  30. delete self.heartbeatTimer;
  31. }
  32. });
  33. this.__realpomelo__.on('close', function(msg){
  34. if (!!self.heartbeatTimer) {
  35. clearInterval(self.heartbeatTimer);
  36. delete self.heartbeatTimer;
  37. }
  38. });
  39. //pomelo.emit('processMessage', msg);
  40. this.__realpomelo__.on('processMessage', function (msg) {
  41. self.emit(msg.route, msg.body);
  42. });
  43. this.__realpomelo__.on('onKick', function (data) {
  44. self.emit('onKick', data);
  45. });
  46. this.__realpomelo__.init(params, function () {
  47. //内部handshake完成,开始走proxy的handshake
  48. var params = {
  49. cosVersion: COS_CLIENT_VERSION,
  50. cosSeq: self.lastMsgSeqId
  51. };
  52. if (self.cosToken.length > 0) {
  53. params.cosToken = self.cosToken;
  54. }
  55. var route = 'cos.connector.handshake';
  56. self.__realpomelo__.request(route, params, function(data) {
  57. onHandshake(self, data);
  58. });
  59. });
  60. };
  61. pomeloClientProxy.request = function(route, msg, cb) {
  62. var self = this;
  63. this.__realpomelo__.request(route, msg, function (body) {
  64. var cosSeq = body.cosSeq;
  65. if(!!cosSeq && cosSeq > self.lastMsgSeqId) {
  66. self.lastMsgSeqId = cosSeq;
  67. }
  68. cb(body);
  69. });
  70. };
  71. pomeloClientProxy.notify = function (route, msg) {
  72. this.__realpomelo__.notify(route, msg);
  73. };
  74. pomeloClientProxy.disconnect = function() {
  75. this.__realpomelo__.disconnect();
  76. if (!!this.heartbeatTimer) {
  77. clearInterval(this.heartbeatTimer);
  78. delete this.heartbeatTimer;
  79. }
  80. };
  81. var onHandshake = function (pomelo, data) {
  82. var code = data.code;
  83. if (code != 0) {
  84. if (code === 4) {
  85. console.log('Package version not match');
  86. } else if (code === 5) {
  87. console.log('Token not exist');
  88. }
  89. return;
  90. }
  91. pomelo.cosToken = data.cosToken;
  92. //注意:只所以初使化的回调在这里返回,是为了保证C-S连接全部完成之后,client才可以请求
  93. if(!!pomelo.initCallback && 'function' === typeof pomelo.initCallback) {
  94. heartbeat(pomelo, data);
  95. pomelo.initCallback();
  96. }
  97. };
  98. var heartbeat = function(pomelo, data) {
  99. if(!pomelo.heartbeatInterval) {
  100. // no heartbeat
  101. return;
  102. }
  103. if(!pomelo.heartbeatTimer) {
  104. pomelo.heartbeatTryTimes = 1;
  105. pomelo.heartbeatTimer = setInterval(function () {
  106. pomelo.heartbeatTryTimes++;
  107. if (pomelo.heartbeatTryTimes >= pomelo.heartbeatTimeoutTimes)
  108. {
  109. //console.info(, '');
  110. pomelo.__realpomelo__.disconnect();
  111. } else {
  112. var params = {
  113. cosSeq: pomelo.lastMsgSeqId
  114. };
  115. var route = 'cos.connector.heartbeat';
  116. pomelo.__realpomelo__.request(route, params, function(data) {
  117. onHeartbeat(pomelo, data);
  118. });
  119. }
  120. }, pomelo.heartbeatInterval);
  121. }
  122. };
  123. var onHeartbeat = function (pomelo, data) {
  124. pomelo.heartbeatTryTimes = 1;
  125. };