Commit 18e732c088a40dbada7b4a4b07f64e50995d1101
1 parent
45725db906
Exists in
master
relogin on send error
Showing 1 changed file with 35 additions and 10 deletions Side-by-side Diff
adaptor-ym.js
... | ... | @@ -2,15 +2,19 @@ var YM = require('yahoomessenger'); |
2 | 2 | |
3 | 3 | var username; |
4 | 4 | var password; |
5 | - | |
6 | 5 | var callbacks; |
7 | 6 | |
7 | +var isOnline = false; | |
8 | +var msSleepBeforeResendOnFail = 10000; | |
9 | + | |
8 | 10 | function onReady() { |
9 | 11 | logger.info('Going to login to YM as ' + username); |
10 | 12 | YM.login(username, password); |
11 | 13 | } |
12 | 14 | |
13 | 15 | function onOnline(data) { |
16 | + isOnline = true; | |
17 | + | |
14 | 18 | logger.info('YM login successful', {data: data}); |
15 | 19 | if (callbacks.onOnline) { |
16 | 20 | callbacks.onOnline(); |
... | ... | @@ -49,19 +53,40 @@ function init(_username, _password, _logger, _callbacks) { |
49 | 53 | YM.on('pm', onPM); |
50 | 54 | YM.on('offlinePM', onPM); |
51 | 55 | |
52 | - /* | |
53 | - setTimeout( | |
54 | - YM.newInstance, | |
55 | - 3000 | |
56 | - ) | |
57 | - */ | |
58 | 56 | YM.newInstance(); |
59 | 57 | } |
60 | 58 | |
61 | -function sendMessage(destination, msg) { | |
62 | - logger.verbose('Sending message', {from: username, destination: destination, msg: msg}); | |
63 | - YM.sendPM(destination, msg); | |
59 | +function sendMessage(destination, msg, requestId) { | |
60 | + if (!isOnline) { | |
61 | + logger.info('We are not online right now, resending in ' + msSleepBeforeResendOnFail + 'ms', {destination: destination, msg: msg, requestId: requestId}); | |
62 | + | |
63 | + setTimeout( | |
64 | + sendMessage, | |
65 | + msSleepBeforeResendOnFail, | |
66 | + destination, | |
67 | + msg, | |
68 | + requestId | |
69 | + ) | |
70 | + return; | |
71 | + } | |
72 | + | |
73 | + logger.verbose('Sending message', {from: username, destination: destination, msg: msg, requestId: requestId}); | |
74 | + | |
75 | + try { | |
76 | + YM.sendPM(destination, msg); | |
77 | + } | |
78 | + catch(e) { | |
79 | + logger.warn(__filename + ': Exception on sending message: ' + e, {e: e, destination: destination, msg: msg, requestId: requestId}); | |
80 | + logger.info('Trying to relogin to YM in 3 secs'); | |
81 | + | |
82 | + isOnline = false; | |
83 | + setTimeout( | |
84 | + YM.newInstance, | |
85 | + 3000 | |
86 | + ); | |
87 | + } | |
64 | 88 | } |
65 | 89 | |
66 | 90 | exports.init = init; |
67 | 91 | exports.sendMessage = sendMessage; |
92 | +exports.isOnline = isOnline; |