mirror of
https://github.com/openstf/stf
synced 2025-10-05 10:39:25 +02:00
Use promises for socket client for better reliability.
This commit is contained in:
parent
3b54213e97
commit
df3a29d94c
1 changed files with 83 additions and 71 deletions
|
@ -24,7 +24,7 @@ module.exports = function(options) {
|
|||
, app = express()
|
||||
, server = http.createServer(app)
|
||||
, io = socketio.listen(server)
|
||||
, groupRouter = new events.EventEmitter()
|
||||
, channelRouter = new events.EventEmitter()
|
||||
|
||||
app.set('view engine', 'jade')
|
||||
app.set('views', pathutil.resource('app/views'))
|
||||
|
@ -76,7 +76,7 @@ module.exports = function(options) {
|
|||
})
|
||||
|
||||
sub.on('message', function(channel, data) {
|
||||
groupRouter.emit(channel.toString(), channel, data)
|
||||
channelRouter.emit(channel.toString(), channel, data)
|
||||
})
|
||||
|
||||
app.get('/partials/*', function(req, res) {
|
||||
|
@ -202,13 +202,13 @@ module.exports = function(options) {
|
|||
|
||||
function joinChannel(channel) {
|
||||
channels.push(channel)
|
||||
groupRouter.on(channel, messageListener)
|
||||
channelRouter.on(channel, messageListener)
|
||||
sub.subscribe(channel)
|
||||
}
|
||||
|
||||
function leaveChannel(channel) {
|
||||
_.pull(channels, channel)
|
||||
groupRouter.removeListener(channel, messageListener)
|
||||
channelRouter.removeListener(channel, messageListener)
|
||||
sub.unsubscribe(channel)
|
||||
}
|
||||
|
||||
|
@ -267,20 +267,13 @@ module.exports = function(options) {
|
|||
// @todo Use socket.io to push global events to all clients instead
|
||||
// of listening on every connection, otherwise we're very likely to
|
||||
// hit EventEmitter's leak complaints (plus it's more work)
|
||||
groupRouter.on(wireutil.global, messageListener)
|
||||
channelRouter.on(wireutil.global, messageListener)
|
||||
|
||||
// User's private group
|
||||
joinChannel(user.group)
|
||||
|
||||
socket
|
||||
// Clean up all listeners and subscriptions
|
||||
.on('disconnect', function() {
|
||||
groupRouter.removeListener(wireutil.global, messageListener)
|
||||
channels.forEach(function(channel) {
|
||||
groupRouter.removeListener(channel, messageListener)
|
||||
sub.unsubscribe(channel)
|
||||
})
|
||||
})
|
||||
new Promise(function(resolve, reject) {
|
||||
socket.on('disconnect', resolve)
|
||||
// Grouping
|
||||
.on('group.invite', function(data) {
|
||||
push.send([
|
||||
|
@ -341,6 +334,25 @@ module.exports = function(options) {
|
|||
, wireutil.envelope(new wire.ShellKeepAliveMessage(data))
|
||||
])
|
||||
})
|
||||
})
|
||||
.finally(function() {
|
||||
// Clean up all listeners and subscriptions
|
||||
channelRouter.removeListener(wireutil.global, messageListener)
|
||||
channels.forEach(function(channel) {
|
||||
channelRouter.removeListener(channel, messageListener)
|
||||
sub.unsubscribe(channel)
|
||||
})
|
||||
})
|
||||
.catch(function(err) {
|
||||
// Cannot guarantee integrity of client
|
||||
log.error(
|
||||
'Client had an error, disconnecting due to probable loss of integrity'
|
||||
, err.stack
|
||||
)
|
||||
|
||||
socket.disconnect(true)
|
||||
})
|
||||
|
||||
|
||||
/*
|
||||
socket.on('flick', function(data) {})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue