Last active
March 4, 2019 06:31
-
-
Save yangga/a4b3f16e24d959a1085f1319dfeb8e66 to your computer and use it in GitHub Desktop.
This file contains 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 express = require('express'); | |
var app = express(); | |
app.get('/', function (req, res) { | |
res.send('Hello Socket!'); | |
}); | |
var server = require('http').createServer(app).listen(3000, function() { | |
console.log(`Listening on port 3000 with http`); | |
}); | |
const socketio = require('socket.io') | |
const io = socketio(server) | |
io.on('connection', (socket) => { | |
console.log('Client connected'); | |
// echo received chat message | |
socket.on('chat', function(msg){ | |
console.log('Recv chat:', msg) | |
io.emit('chat', msg); | |
}); | |
socket.on('disconnect', () => { | |
console.log('Client disconnected'); | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment