forked from rauchg/socket.io-computer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.js
59 lines (46 loc) · 1.39 KB
/
io.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var sio = require('socket.io');
var debug = require('debug');
process.title = 'socket.io-computer-io';
var port = process.env.COMPUTER_IO_PORT || 6001;
var io = module.exports = sio(port);
console.log('listening on *:' + port);
// redis socket.io adapter
var uri = require('redis').uri;
io.adapter(require('socket.io-redis')(uri, {key: 'xpemu'}));
// redis queries instance
var redis = require('./redis').io();
var uid = process.env.COMPUTER_IO_SERVER_UID || port;
debug('server uid %s', uid);
io.total = 0;
io.on('connection', function(socket) {
var req = socket.request;
// keep track of connected clients
updateClientCount(++io.total);
socket.on('disconnect', function() {
updateClientCount(--io.total);
});
// in case user is reconneting send last known state
redis.get('computer:frame', function(err, image) {
if (image) socket.emit('raw', {
width: 800,
height: 600,
x: 0,
y: 0,
image: image
});
});
// send keypress to emulator
socket.on('keydown', function(key) {
redis.publish('computer:keydown', key);
});
// pointer events
socket.on('pointer', function(x, y, state) {
redis.publish('computer:pointer', x + ':' + y + ':' + state);
});
socket.on('turn-request', function(time) {
redis.publish('computer:turn', socket.id);
});
});
function updateClientCount(total) {
redis.hset('computer:connections', uid, total);
}