connect.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup Messenger Messenger
  6. * @ingroup UnaModules
  7. * @{
  8. */
  9. /**
  10. * Primus class to work with the server and main messenger class
  11. */
  12. ;window.oRTWSF = (function(){
  13. let _oPrimus = null, _oData = Object.create(null), _sIdent, sJWT, _sIP = '0.0.0.0';
  14. return {
  15. /**
  16. * Init Primus with provided settings and attaches listeners on event emitters from the server
  17. *@param object oOptions options
  18. */
  19. init:function(oOptions){
  20. const _this = this,
  21. { server, ip, ident, jwt } = oOptions;
  22. _this._sIdent = ident || _this._sIdent;
  23. _this._sIP = ip || _this._sIP;
  24. if (!server.length) {
  25. console.log('Jot Server is not defined');
  26. return;
  27. }
  28. if (_oPrimus === null)
  29. _oPrimus = new Primus(server);
  30. // on data received from the server
  31. _oPrimus.on('data', function(oData) {
  32. if (typeof oData.action !== "undefined" && !_oPrimus.emit(oData.action, oData))
  33. console.log('Unknown server response', oData);
  34. }).on('error', function error(oError) {
  35. console.log('Primus Error', oError);
  36. }).on('reconnect scheduled', function (oOptions) {
  37. console.log('Reconnecting in %d ms', oOptions.scheduled);
  38. console.log('This is attempt %d out of %d', oOptions.attempt, oOptions.retries);
  39. _this.onReconnecting(oOptions);
  40. }).on('reconnected', function (oOptions) {
  41. _this.onReconnected(oOptions);
  42. }).on('reconnect failed', function (oOptions) {
  43. _this.onReconnectFailed(oOptions);
  44. }).on('open', function () {
  45. const oSettings = _this.getSettings();
  46. if (typeof oSettings !== 'undefined' && typeof oSettings.user_id !== 'undefined' && typeof oSettings.status !== 'undefined'){
  47. this.write({
  48. action:'init',
  49. jwt: jwt,
  50. ip:_this._sIP,
  51. ident:_this._sIdent,
  52. user_id: oSettings.user_id,
  53. status: oSettings.status
  54. });
  55. }
  56. }).on('typing', function (oData) {
  57. _this.onTyping(oData);
  58. }).on('msg', function (oData){
  59. _this.onMessage(oData);
  60. }).on('update_status', function (oData) {
  61. _this.onStatusUpdate(oData);
  62. }).on('check_sent', function (oData) {
  63. _this.onServerResponse(oData);
  64. }).on('denied', function (oData) {
  65. console.log('Access Denied for your IP');
  66. _this.onReconnecting(oOptions);
  67. }).on('jwt-error', function (oData) {
  68. console.log('JWT token is not verified!');
  69. _this.onDestroy(oData);
  70. }).on('token-init', function (oData) {
  71. const { token } = oData;
  72. sJWT = token;
  73. });
  74. },
  75. isInitialized:() => _oPrimus !== null && typeof _oPrimus !== 'undefined',
  76. /*
  77. *Methods occur when received data from the server or on primus events
  78. *BEGIN
  79. */
  80. onTyping:function(){
  81. console.log('overwrite it in the main messenger class');
  82. },
  83. onMessage:function(){
  84. console.log('overwrite it in the main messenger class');
  85. },
  86. onStatusUpdate:function(){
  87. console.log('overwrite it in the main messenger class');
  88. },
  89. getSettings:function(){
  90. console.log('overwrite it in the main messenger class');
  91. },
  92. onServerResponse:function(){
  93. console.log('overwrite it in the main messenger class');
  94. },
  95. onReconnecting:function(){
  96. console.log('overwrite it in the main messenger class');
  97. },
  98. onReconnected:function(){
  99. console.log('overwrite it in the main messenger class');
  100. },
  101. onReconnectFailed:function(){
  102. console.log('overwrite it in the main messenger class');
  103. },
  104. onDestroy:function(){
  105. console.log('overwrite it in the main messenger class');
  106. },
  107. /** End **/
  108. /*
  109. *Methods are called on members' activities in chat window and send data to the server
  110. *BEGIN
  111. */
  112. initSettings:function(oData){
  113. this.exec('init', oData);
  114. },
  115. message:function(oData){
  116. this.exec('msg', oData);
  117. },
  118. typing:function(oData){
  119. this.exec('typing', oData);
  120. },
  121. end:function(oData){
  122. this.exec('before_delete', oData);
  123. _oPrimus.end();
  124. },
  125. updateStatus:function(oData){
  126. this.exec('update_status', oData);
  127. },
  128. exec:function(sParam, oData){
  129. if (!this.isInitialized())
  130. return;
  131. if (!_oPrimus.writable)
  132. _oPrimus.open();
  133. const oObject = { action:sParam, ip:this._sIP, ident:this._sIdent };
  134. _oPrimus.write($.extend(oData, oObject));
  135. }
  136. /** END **/
  137. }
  138. })();
  139. /** @} */