Last active
November 12, 2020 07:01
-
-
Save Zfinix/7b852ef2f934fc8bdf3a2005a49a34ae to your computer and use it in GitHub Desktop.
Web socket server
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
| 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