Commit 47afee1dcfee9c91caf4f903ff024bc58a20b00b

Authored by Adhidarma Hadiwinoto
1 parent 5cb04b2190
Exists in master

auto reconnect

Showing 1 changed file with 19 additions and 8 deletions Side-by-side Diff

... ... @@ -34,6 +34,19 @@ var logger = new (winston.Logger)({
34 34 ]
35 35 });
36 36  
  37 +
  38 +function xmppConnect() {
  39 + logger.info('Connecting to XMPP server');
  40 +
  41 + xmpp.connect({
  42 + jid: config.globals.jid,
  43 + password: config.globals.password,
  44 + });
  45 +
  46 + // check for incoming subscription requests
  47 + xmpp.getRoster();
  48 +}
  49 +
37 50 function onHttpIncomingMessage(request, response) {
38 51 var qs = url.parse(request.url, true).query;
39 52 logger.info("onHttpIncomingMessage()", {qs: qs});
... ... @@ -86,6 +99,11 @@ function reportToEvo(from, message) {
86 99  
87 100 }
88 101  
  102 +xmpp.on('close', function() {
  103 + logger.warn('XMPP disconnected');
  104 + setTimeout(xmppConnect, 10 * 1000);
  105 +});
  106 +
89 107 xmpp.on('online', function(data) {
90 108 logger.info('XMPP online', {data: data});
91 109 logger.info('Connected with JID: ' + data.jid.user);
... ... @@ -121,12 +139,5 @@ xmpp.on('subscribe', function(from) {
121 139 xmpp.acceptSubscription(from);
122 140 });
123 141  
124   -xmpp.connect({
125   - jid: config.globals.jid,
126   - password: config.globals.password,
127   -});
128   -
129   -// check for incoming subscription requests
130   -xmpp.getRoster();
131   -
132 142 createHttpListener();
  143 +xmppConnect();