-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (49 loc) · 1.84 KB
/
index.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
58
59
60
61
62
63
// 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 || 5000, listen);
// var server = app.listen(5000, listen);
// var server = app.listen(44812, listen);
// 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('public'));
// 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) {
console.log("We have a new client: " + socket.id);
// When this user emits, client side: socket.emit('otherevent',some data);
socket.on('key',
function(data) {
// Data comes in as whatever was sent, including objects
console.log("Received: 'key' " + data.fc + " size: " + data.size);
// Send it to all other clients
socket.broadcast.emit('key', data);
// This is a way to send to everyone including sender
// io.sockets.emit('message', "this goes to everyone");
}
);
socket.on('hit',
function(data) {
console.log("Received: 'hit' " + data.size + " - " + data.fp);
socket.broadcast.emit('hit', data);
}
);
socket.on('disconnect', function() {
console.log("Client has disconnected");
});
}
);