Start the Server using
node server.js --port 8080
Navigate to the index.html document frommany tabs or browsers.
Start the Server using
node server.js --port 8080
Navigate to the index.html document frommany tabs or browsers.
(function () { | |
'use strict'; | |
var socket = io.connect('http://localhost:8080'); | |
socket.on('message', function (data) { | |
console.log(data); | |
if(data.message === 'connected') { | |
console.log('New connection'); | |
} | |
}); | |
socket.emit('message', {event: 'connected', data: {room: 'lobby', name: navigator.userAgent}}); | |
}).call(this); |
<!doctype html> | |
<html> | |
<head> | |
<meta charset='utf-8'> | |
<title>Example</title> | |
<script src="http://localhost:8080/socket.io/socket.io.js"></script> | |
<script src="client.js"></script> | |
</head> | |
<body> | |
View the console. | |
</body> | |
</html> |
(function () { | |
'use strict'; | |
var io, port; | |
io = require('socket.io'); | |
process.argv.slice(2).forEach(function(val, index, array) { | |
if(val === '--port' || val === '-p') { | |
port = parseInt(array[index+1]); | |
} | |
if(/--port=|-p=/.test(val)){ | |
port = parseInt(val.split('=')[1]); | |
} | |
}); | |
if(!port) { | |
port = 8080; | |
} | |
function joinRoom(socket, room){ | |
if(socket.room) { | |
socket.leave(socket.room); | |
} | |
socket.join(room); | |
socket.room = room; | |
console.log('joined room', room); | |
} | |
console.info('Starting Socket.io on port ' + port); | |
io.listen(port).on('connection', function(socket) { | |
socket.on('message', function (data) { | |
if(data.event === 'connected' && data.data.room) { | |
joinRoom(socket, data.data.room); | |
} | |
socket.broadcast.to(socket.room).emit('message', data); | |
}); | |
socket.on('disconnect', function () { | |
socket.broadcast.to(socket.room).emit('message',{event : 'disconnect'}); | |
}); | |
}); | |
}).call(this); |