Skip to content

Instantly share code, notes, and snippets.

@Zfinix
Last active November 12, 2020 07:01
Show Gist options
  • Select an option

  • Save Zfinix/7b852ef2f934fc8bdf3a2005a49a34ae to your computer and use it in GitHub Desktop.

Select an option

Save Zfinix/7b852ef2f934fc8bdf3a2005a49a34ae to your computer and use it in GitHub Desktop.
Web socket server
const server = require('http').createServer()
const io = require('socket.io')(server)
io.on('connection', function (client) {
console.log('client connect...', client.id);
client.on('typing', function name(data) {
console.log(data);
io.emit('typing', data)
})
client.on('message', function name(data) {
console.log(data);
io.emit('message', data)
})
client.on('location', function name(data) {
console.log(data);
io.emit('location', data);
})
client.on('connect', function () {
})
client.on('disconnect', function () {
console.log('client disconnect...', client.id)
// handleDisconnect()
})
client.on('error', function (err) {
console.log('received error from client:', client.id)
console.log(err)
})
})
var server_port = process.env.PORT || 3000;
server.listen(server_port, function (err) {
if (err) throw err
console.log('Listening on port %d', server_port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment