Created
May 17, 2018 13:30
-
-
Save probil/907048d2cf6b4a6620aeaf2563581661 to your computer and use it in GitHub Desktop.
Example of socket.io server for #67
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
'use strict'; | |
const app = require('express')(); | |
const http = require('http').Server(app); | |
const io = require('socket.io')(http, { serveClient: false }); | |
const middleware = require('socketio-wildcard')(); | |
io.use(middleware); | |
app.use((req, res, next) => { | |
res.header('Access-Control-Allow-Origin', '*'); | |
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); | |
next(); | |
}); | |
io.on('connection', socket => { | |
console.log('a user connected'); | |
socket.on('disconnect', () => { | |
console.log('user disconnected'); | |
}); | |
socket.on('*', packet => { | |
const [eventName, ...data] = packet.data; | |
console.log('EMITTED:', eventName, ...data); | |
socket.emit(eventName, ...data) | |
}) | |
}); | |
http.listen(3000, () => { | |
console.log('listening on *:3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment