-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
61 lines (48 loc) · 1.83 KB
/
Copy pathapp.js
File metadata and controls
61 lines (48 loc) · 1.83 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
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const socketIO = require('socket.io')(server); //get package and instantiate with server
const LISTEN_PORT = 8080; //make sure greater than 3000. Some ports are reserved/blocked by firewall ...
app.use((express.static(__dirname + '/public'))); //set root dir to the public folder
answer = [];
//routes
app.get('/', function(req,res) {
res.sendFile(__dirname + '/public/index.html');
});
//codemaker
app.get('/codemaker', function(req,res) {
res.sendFile(__dirname + '/public/codemaker.html');
});
//websocket events
socketIO.on('connection', function(socket){
console.log(socket.id + " has connected!");
socket.on('disconnect', function(data){
console.log(socket.id + " has disconnected!");
})
//custom events
socket.on('submitButton', function(data){
console.log("checking event heard");
console.log(data);
socketIO.sockets.emit('check_answer', data);
});
socket.on('red', function(data){
console.log("red event heard");
socketIO.sockets.emit('color_change', {r:255, g:0, b:0});
});
socket.on('green', function(data){
console.log("green event heard");
socketIO.sockets.emit('color_change', {r:0, g:255, b:0});
});
socket.on('yellow', function(data){
console.log("yellow event heard");
socketIO.sockets.emit('color_change', {r:255, g:255, b:0});
});
socket.on('white', function(data){
console.log("white space event heard");
socketIO.sockets.emit('color_change', {r:255, g:255, b:255});
});
});
//finally, start server
server.listen(LISTEN_PORT);
console.log('listening to port: ' + LISTEN_PORT);