From 67b7d3d3b6565b88ef94b681b7f7db99b11d2435 Mon Sep 17 00:00:00 2001 From: Eshin Kunishima Date: Sun, 7 May 2017 00:05:38 +0900 Subject: Replace ws with uws (#2807) * Replace ws with uws * fix indent * Adjust indentation * remove trailing space --- streaming/index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'streaming/index.js') diff --git a/streaming/index.js b/streaming/index.js index 883bf33f5..3a7764ee9 100644 --- a/streaming/index.js +++ b/streaming/index.js @@ -7,7 +7,7 @@ import redis from 'redis' import pg from 'pg' import log from 'npmlog' import url from 'url' -import WebSocket from 'ws' +import WebSocket from 'uws' import uuid from 'uuid' const env = process.env.NODE_ENV || 'development' @@ -273,12 +273,16 @@ if (cluster.isMaster) { // Setup stream output to WebSockets const streamToWs = (req, ws) => { - const heartbeat = setInterval(() => ws.ping(), 15000) + const heartbeat = setInterval(() => { + // TODO: Can't add multiple listeners, due to the limitation of uws. + if (ws.readyState !== ws.OPEN) { + log.verbose(req.requestId, `Ending stream for ${req.accountId}`) + clearInterval(heartbeat) + return + } - ws.on('close', () => { - log.verbose(req.requestId, `Ending stream for ${req.accountId}`) - clearInterval(heartbeat) - }) + ws.ping() + }, 15000) return (event, payload) => { if (ws.readyState !== ws.OPEN) { -- cgit