Skip to content

Instantly share code, notes, and snippets.

@lmccart
Created March 23, 2016 21:02
Show Gist options
  • Save lmccart/5fdd0230395d9f4781a6 to your computer and use it in GitHub Desktop.
Save lmccart/5fdd0230395d9f4781a6 to your computer and use it in GitHub Desktop.
// Explained in tutorial here: http://socket.io/get-started/chat/
// 1. Install socket.io module: "npm install socket.io"
// 2. Install express module: "npm install express"
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('public/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message', function(msg){
console.log('message: ' + msg);
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on port 3000');
});
////
socket.on('position', function(msg){
// console.log('position x: ' + msg.x + ' y: ' + msg.y);
//io.emit('position', msg);
// this explains about broadcasting to diff users: http://stackoverflow.com/questions/10058226/send-response-to-all-clients-except-sender-socket-io
socket.broadcast.emit('position', msg);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment