-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.js
More file actions
105 lines (87 loc) · 3.05 KB
/
Copy pathbackend.js
File metadata and controls
105 lines (87 loc) · 3.05 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Based off of Shawn Van Every's Live Web
// http://itp.nyu.edu/~sve204/liveweb_fall2013/week3.html
// Using express: http://expressjs.com/
var express = require('express');
// Create the app
var app = express();
// Set up the server
// process.env.PORT is related to deploying on heroku
var server = app.listen(process.env.PORT || 3000, listen);
let numberOfPlayers = 0;
// This call back just tells us that the server has started
function listen() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://' + host + ':' + port);
}
app.use(express.static(__dirname + "/public"));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/booth/booth.html');
});
// WebSocket Portion
// WebSockets work with the HTTP server
var io = require('socket.io')(server);
// Register a callback function to run when we have an individual connection
// This is run for each individual user that connects
io.sockets.on('connection',
// We are given a websocket object in our function
function (socket) {
numberOfPlayers++;
console.log("We have a new client: " + socket.id);
// When this user emits, client side: socket.emit('otherevent',some data);
socket.on('mouse',
function (data) {
// Data comes in as whatever was sent, including objects
console.log("Received: 'mouse' " + data.x + " " + data.y + ` ${data.Emoji}`);
// Send it to all other clients
socket.broadcast.emit('mouse', data);
//
// socket.broadcast.emit('Blendbutton',BlendMode);
// This is a way to send to everyone including sender
// io.sockets.emit('message', "this goes to everyone");
}
);
socket.on('BlendButton',
function (BlendData) {
console.log(BlendData);
// socket.broadcast.emit('BlendButton', BlendData);
io.sockets.emit('BlendButton', BlendData);
}
);
socket.on('clearButton',
function (ClearData) {
console.log(ClearData);
// socket.broadcast.emit('BlendButton', BlendData);
io.sockets.emit('clearButton', ClearData);
}
);
socket.on('bgChoice', function (BackData) {
io.sockets.emit('bgChoice', BackData);
console.log('fuck');
});
socket.on('playerData',
function (data) {
console.log(socket.id + ' sent data ' + `${data}`)
io.emit('playerData', data);
io.emit('numberOfPlayers', numberOfPlayers);
console.log(numberOfPlayers);
}
);
socket.on('button', function (mouseIsPressed) {
console.log(socket.id + 'pressed their mouse') + mouseIsPressed
});
socket.on('disconnect', function () {
console.log("Client has disconnected");
});
socket.on('button1', function () {
console.log(socket.id + ' has pressed the first button')
});
socket.on('colorData', function (colordata) {
console.log(colordata);
io.emit('colorData', colordata);
});
}
);