diff options
author | anekos | 2010-08-07 15:05:44 +0000 |
---|---|---|
committer | anekos | 2010-08-07 15:05:44 +0000 |
commit | 2991c8cf1474082c43a7b5e275e17e77e5ebd9d9 (patch) | |
tree | 884534b0c2fdbb250840fbd30703d1790a3fa72c | |
parent | 01b468e103ef8e4f7aff491a81e6a3af4a8623ca (diff) | |
download | vimperator-plugins-2991c8cf1474082c43a7b5e275e17e77e5ebd9d9.tar.bz2 |
ChirpUserStream を切断されたら、再接続するようにした
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@38218 d0d07461-0603-4401-acd4-de1884942a52
-rwxr-xr-x | twittperator.js | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/twittperator.js b/twittperator.js index 1993411..f31cd31 100755 --- a/twittperator.js +++ b/twittperator.js @@ -1169,11 +1169,29 @@ let PLUGIN_INFO = (m && m[0]); let connectionInfo; + let restartCount = 0; + let startTime; + + function restart() { + stop(); + + if (restartCount > 13) + return liberator.echoerr('Twittperator: Gave up to connect to ChirpUserStream...'); + + liberator.echoerr('Twittperator: ChirpUserStream will be restared...'); + + // 試行済み回数^2 秒後にリトライ + setTimeout(start, Math.pow(2, restartCount) * 1000); + + restartCount++; + } function stop() { if (!connectionInfo) return; + liberator.log("Twittperator: stop chirp user stream"); + clearInterval(connectionInfo.interval); connectionInfo.sos.close(); connectionInfo.sis.close(); @@ -1186,6 +1204,8 @@ let PLUGIN_INFO = stop(); + startTime = new Date(); + let host = "chirpstream.twitter.com"; let path = "/2b/user.json"; @@ -1219,6 +1239,12 @@ let PLUGIN_INFO = let len = sis.available(); if (len <= 0) return; + + // 5分間接続されていたら、カウントをクリア + // 何かの事情で即切断されてしまうときに、高頻度でアクセスしないための処置です。 + if (restartCount && (new Date().getTime() - startTime.getTime()) > (5 * 60 * 1000)) + restartCount = 0; + let data = sis.read(len); let lines = data.split(/\n/); if (lines.length > 2) { @@ -1226,15 +1252,18 @@ let PLUGIN_INFO = for (let [, line] in Iterator(lines.slice(0, -1))) { try { onMsg(fixStatusObject(JSON.parse(line)), line); - } catch (e) {liberator.log('E =>\n' + line);} + } catch (e) {} } buf = lines.slice(-1)[0]; } else { buf += data; } + } catch (e if /NS_ERROR_NET_RESET|NS_BASE_STREAM_CLOSED/(e)) { + restart(); + liberator.echoerr('Twittperator: ChirpStream was stopped by ' + e.name + '.'); } catch (e) { - stop(); - liberator.echoerr('ChirpUserStream was stopped.'); + restart(); + liberator.echoerr('Twittperator: Unknown error on ChirpStream connection: ' + e.name); } }, 500); |