mirror of
https://github.com/openstf/stf
synced 2025-10-05 02:29:26 +02:00
The screen websocket now sends ping messages every now and then, so that it won't get caught by nginx's proxy_read_timeout if the screen is off or not moving. Previously, nginx may have decided to kill the connection after a period of no activity.
This commit is contained in:
parent
d1602e6b79
commit
5e9aa427e9
3 changed files with 43 additions and 35 deletions
|
@ -502,22 +502,18 @@ module.exports = syrup.serial()
|
|||
|
||||
wss.on('connection', function(ws) {
|
||||
var id = uuid.v4()
|
||||
var pingTimer
|
||||
|
||||
function wsStartNotifier() {
|
||||
function send(message, options) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var message = util.format(
|
||||
'start %s'
|
||||
, JSON.stringify(frameProducer.banner)
|
||||
)
|
||||
|
||||
switch (ws.readyState) {
|
||||
case WebSocket.OPENING:
|
||||
// This should never happen.
|
||||
log.warn('Unable to send banner to OPENING client "%s"', id)
|
||||
log.warn('Unable to send to OPENING client "%s"', id)
|
||||
break
|
||||
case WebSocket.OPEN:
|
||||
// This is what SHOULD happen.
|
||||
ws.send(message, function(err) {
|
||||
ws.send(message, options, function(err) {
|
||||
return err ? reject(err) : resolve()
|
||||
})
|
||||
break
|
||||
|
@ -527,41 +523,37 @@ module.exports = syrup.serial()
|
|||
break
|
||||
case WebSocket.CLOSED:
|
||||
// This should never happen.
|
||||
log.warn('Unable to send banner to CLOSED client "%s"', id)
|
||||
log.warn('Unable to send to CLOSED client "%s"', id)
|
||||
clearInterval(pingTimer)
|
||||
broadcastSet.remove(id)
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function wsStartNotifier() {
|
||||
return send(util.format(
|
||||
'start %s'
|
||||
, JSON.stringify(frameProducer.banner)
|
||||
))
|
||||
}
|
||||
|
||||
function wsPingNotifier() {
|
||||
return send('ping')
|
||||
}
|
||||
|
||||
function wsFrameNotifier(frame) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
switch (ws.readyState) {
|
||||
case WebSocket.OPENING:
|
||||
// This should never happen.
|
||||
return reject(new Error(util.format(
|
||||
'Unable to send frame to OPENING client "%s"', id)))
|
||||
case WebSocket.OPEN:
|
||||
// This is what SHOULD happen.
|
||||
ws.send(frame, {
|
||||
binary: true
|
||||
}, function(err) {
|
||||
return err ? reject(err) : resolve()
|
||||
})
|
||||
return
|
||||
case WebSocket.CLOSING:
|
||||
// Ok, a 'close' event should remove the client from the set
|
||||
// soon.
|
||||
return
|
||||
case WebSocket.CLOSED:
|
||||
// This should never happen.
|
||||
broadcastSet.remove(id)
|
||||
return reject(new Error(util.format(
|
||||
'Unable to send frame to CLOSED client "%s"', id)))
|
||||
}
|
||||
return send(frame, {
|
||||
binary: true
|
||||
})
|
||||
}
|
||||
|
||||
// Sending a ping message every now and then makes sure that
|
||||
// reverse proxies like nginx don't time out the connection [1].
|
||||
//
|
||||
// [1] http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout
|
||||
pingTimer = setInterval(wsPingNotifier, options.screenPingInterval)
|
||||
|
||||
ws.on('message', function(data) {
|
||||
var match = /^(on|off|(size) ([0-9]+)x([0-9]+))$/.exec(data)
|
||||
if (match) {
|
||||
|
@ -574,6 +566,7 @@ module.exports = syrup.serial()
|
|||
break
|
||||
case 'off':
|
||||
broadcastSet.remove(id)
|
||||
// Keep pinging even when the screen is off.
|
||||
break
|
||||
case 'size':
|
||||
frameProducer.updateProjection(
|
||||
|
@ -584,6 +577,7 @@ module.exports = syrup.serial()
|
|||
})
|
||||
|
||||
ws.on('close', function() {
|
||||
clearInterval(pingTimer)
|
||||
broadcastSet.remove(id)
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue