1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 02:09:32 +02:00

Make inviting/kicking work.

This commit is contained in:
Simo Kinnunen 2014-02-03 01:31:18 +09:00
parent 4ac766c371
commit 3a0d177925
6 changed files with 55 additions and 21 deletions

View file

@ -22,7 +22,7 @@ module.exports = function(options) {
, app = express()
, server = http.createServer(app)
, io = socketio.listen(server)
, router = new events.EventEmitter()
, groupRouter = new events.EventEmitter()
app.set('view engine', 'jade')
app.set('views', pathutil.resource('app/views'))
@ -69,11 +69,7 @@ module.exports = function(options) {
})
sub.on('message', function(channel, data) {
router.emit(
channel.toString()
, channel
, wire.Envelope.decode(data)
)
groupRouter.emit(channel.toString(), channel, data)
})
app.get('/partials/:name', function(req, res) {
@ -171,10 +167,10 @@ module.exports = function(options) {
var messageListener = wirerouter()
.on(wire.JoinGroupMessage, function(channel, message) {
socket.emit('join', message)
socket.emit('group.join', message)
})
.on(wire.LeaveGroupMessage, function(channel, message) {
socket.emit('leave', message)
socket.emit('group.leave', message)
})
.handler()
@ -183,23 +179,23 @@ 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)
channels.push(wireutil.global)
router.on(wireutil.global, messageListener)
groupRouter.on(wireutil.global, messageListener)
// User's private group
channels.push(group)
sub.subscribe(group)
router.on(group, messageListener)
groupRouter.on(group, messageListener)
// Clean up all listeners and subscriptions
socket.on('disconnect', function() {
groupRouter.removeListener(wireutil.global, messageListener)
channels.forEach(function(channel) {
router.removeListener(channel, messageListener)
groupRouter.removeListener(channel, messageListener)
sub.unsubscribe(channel)
})
})
socket.on('invite', function(data) {
socket.on('group.invite', function(data) {
push.send([wireutil.global, wireutil.makeGroupMessage(
group
, options.groupTimeout
@ -207,7 +203,7 @@ module.exports = function(options) {
)])
})
socket.on('kick', function(data) {
socket.on('group.kick', function(data) {
push.send([group, wireutil.makeUngroupMessage(
group
, data

View file

@ -37,6 +37,8 @@ devutil.matchesRequirements = function(capabilities, requirements) {
default:
return false
}
return true
})
}

View file

@ -22,7 +22,7 @@ ChannelManager.prototype.register = function(id, timeout) {
ChannelManager.prototype.unregister = function(id) {
var channel = this.channels[id]
delete this.channels[id]
clearInterval(channel.timer)
clearTimeout(channel.timer)
}
ChannelManager.prototype.keepalive = function(id) {

View file

@ -35,12 +35,27 @@ var wireutil = {
return wireutil.envelope(new wire.GroupMessage(
channel
, timeout
, requirements
, Object.keys(requirements).map(function(name) {
var item = requirements[name]
return new wire.DeviceRequirement(
name
, item.value
, wire.RequirementType[item.match.toUpperCase()]
)
})
))
}
, makeUngroupMessage: function(requirements) {
, makeUngroupMessage: function(channel, requirements) {
return wireutil.envelope(new wire.UngroupMessage(
requirements
channel
, Object.keys(requirements).map(function(name) {
var item = requirements[name]
return new wire.DeviceRequirement(
name
, item.value
, wire.RequirementType[item.match.toUpperCase()]
)
})
))
}
, makeJoinGroupMessage: function(serial) {

View file

@ -1,11 +1,30 @@
define(['./module', 'oboe'], function(mod, oboe) {
function DeviceListCtrl($scope, deviceService) {
define(['./module'], function(mod) {
function DeviceListCtrl($scope, deviceService, groupService) {
$scope.devices = deviceService.devices
$scope.invite = function(device) {
groupService.invite({
serial: {
value: device.serial
, match: 'exact'
}
})
}
$scope.kick = function(device) {
groupService.kick({
serial: {
value: device.serial
, match: 'exact'
}
})
}
}
mod.controller('DeviceListCtrl'
, [ '$scope'
, 'deviceService'
, 'groupService'
, DeviceListCtrl
])
})

View file

@ -6,11 +6,13 @@ define(['./module', 'lodash'], function(mod, _) {
socket.on('group.join', function(data) {
groupService.members.push(data.serial)
console.log('group.join', data)
$rootScope.$digest()
})
socket.on('group.left', function(data) {
socket.on('group.leave', function(data) {
_.pull(groupService.members, data.serial)
console.log('group.leave', data)
$rootScope.$digest()
})