-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (39 loc) · 1.34 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
52
53
54
55
56
57
/*
Require dependencies
*/
var express = require('express'); //express is used for a webframework
var app = require('express')(); //creates a new instance of express
var http = require('http').Server(app); //http package is used HTTP server
var io = require('socket.io')(http); //socket.io package for creating socket connections
app.use(express.static('public'));
/*
Writing a route
*/
// when client requests a '/' from the browser, we serve 'random_function.html'
app.get('/', function(req, res){
res.sendfile('random_function.html');
});
/*
Writing the socket funtions
*/
//Whenever someone connects this gets executed
io.on('connection', function(socket){
console.log('A user connected');
//Whenever 'clientEvent' occurs this gets executed
socket.on('clientEvent', function(data){
//The data from clientEvent is sent as 'serverEvent' to whoever listens to 'serverEvent'
socket.broadcast.emit('serverEvent',data);
});
//Whenever someone disconnects this piece of code executed
socket.on('disconnect', function () {
console.log('A user disconnected');
});
});
//Whenever someone attemtps to connect, this gets executed
io.on('connect',function(socket){
console.log("Attempted connect");
});
//Finally, we set the server to listen on port - 3000
http.listen(3000, function(){
console.log('listening on *:3000');
});