Created
April 22, 2017 18:00
-
-
Save sohelamin/91c23bd6df34e895414578d4e2dc788b to your computer and use it in GitHub Desktop.
Socket.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = require('express')(); | |
var http = require('http').Server(app); | |
var io = require('socket.io')(http); | |
var port = process.env.PORT || 3000; | |
app.get('/', function(req, res){ | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
app.get('/chat', function(req, res){ | |
res.sendFile(__dirname + '/chat.html'); | |
}); | |
var people = {}; | |
io.on('connection', function(socket) { | |
socket.on('online user', function(user) { | |
people[user] = socket.id; | |
}); | |
socket.on('chat message', function(msg, user) { | |
socket.to(people[user]).emit('chat message', msg); | |
}); | |
}); | |
http.listen(port, function(){ | |
console.log('listening on *:' + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment