-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
53 lines (42 loc) · 1.49 KB
/
server.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
/*
Ignore first few lines with ///
They are for Visual Studio Code Intellisense.
*/
/// <reference path="typings/tsd.d.ts" />
/// <reference path="typings/node/node.d.ts" />
/// <reference path="typings/express/express.d.ts"/>
/// <reference path="typings/socket.io/socket.io.d.ts" />
var express = require("express"),
app = express(),
server = require("http").Server(app),
io = require("socket.io")(server);
app.use(express.static('public'));
// The game host will be connecting here
// send them index.html
app.get('/', function(req, res) {
res.sendFile(__dirname + "/public/index.html");
});
//The controllers will be connecting here
// send them controller.html
app.get('/:game_id/:controller_id', function(req, res) {
res.sendFile(__dirname + "/public/controller.html");
});
io.on('connection', function(socket) {
//On connection of controller(mobile)
//Inform the game host(pc) about it
socket.on('controller_connect', function(game_host, controller_id){
socket.broadcast.to(game_host).emit('controller_connected', socket.id, controller_id);
socket.on('move', function(direction, game_host, controller_id) {
socket.broadcast.to(game_host).emit('move', direction, controller_id);
console.log(direction);
});
//When Controller disconnects
//Inform game host about it
socket.on('disconnect', function() {
socket.broadcast.to(game_host).emit('controller_disconnect', socket.id);
});
});
});
server.listen(8080, function(){
console.log("Server up and running");
});