forked from romualdr/githubplaysgameboy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (46 loc) · 1.52 KB
/
app.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
var express = require('express');
var app = express();
var path = require('path')
, server = require('http').createServer(app)
, io = require('socket.io').listen(server)
, fs = require('fs');
var config = require('./config.json');
var allowedInputs = (config.allowed_inputs || ["start", "select", "left", "right", "up", "down", "a", "b"]);
var rom = __dirname + '/' + (config.rom || 'rom.gb');
server.listen(process.env.PORT || (config.port || 3000));
app.set('view engine', 'ejs');
app.get('/', function(req, res) {
res.render('index', config);
});
app.get('/master', function(req, res) {
res.render('master', config);
});
app.use(express.static(path.join(__dirname, 'public')));
io.sockets.on('connection', function (socket) {
socket.on('master:ready', function (data) {
fs.readFile(rom, function(err, original_data){
var rom = original_data.toString('base64');
socket.emit('master:loadgame', {
file: rom
});
});
});
socket.on('keypress', function (data) {
if (allowedInputs.indexOf(data.key) <= -1)
return socket.disconnect();
socket.get('username', function (err, name) {
if (err) {
return socket.disconnect();
}
socket.broadcast.emit('master:push', {name: name, key: data.key});
});
});
socket.emit('io:askusername', {});
socket.on('io:username', function (data) {
if (!data.name || !data.name.trim().length)
return socket.disconnect();
socket.set('username', data.name, function () {
socket.emit('io:launchPlay', {name: data.name, inputs: allowedInputs});
});
})
});