-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
72 lines (56 loc) · 2.04 KB
/
server.js
File metadata and controls
72 lines (56 loc) · 2.04 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
var express = require('express');
var app = express();
var server = app.listen(3000);
app.use (express.static('public'));
var socket = require ('socket.io');
var io = socket (server);
//if I have a new connection
io. on ('connection', newConnection);
function newConnection(socket){
console.log ('newConnection:'+ socket.id);
console.log (socket.id);// get new connections
socket.on ('mouse', mouseMsg);
function mouseMsg(data){
// socket.broadcast.emit('mouse', data);
console.log (data);
}
setInterval(function(){
io.emit('player2mouse', {x: 50, y:110});
}, 1000);
}
// Set up the server
// process.env.PORT is related to deploying on heroku
// var server = app.listen(process.env.PORT || 3000, 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('mouse',
// function(data) {
// // Data comes in as whatever was sent, including objects
// console.log("Received: 'mouse' " + data.x + " " + data.y);
// // Send it to all other clients
// socket.broadcast.emit('mouse', data);
// // This is a way to send to everyone including sender
// // io.sockets.emit('message', "this goes to everyone");
// }
// );
// //
// socket.on('disconnect', function() {
console.log("Client has disconnected");
// });
// }
// );