Initial commit
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
const PORT = process.env.PORT || 8080;
|
||||
const PREFIX = process.env.PREFIX || '/ws';
|
||||
console.log('Socket.io listen on: http://localhost:' + PORT + '' + PREFIX + '/socket.io');
|
||||
console.log('Publisher listen on: http://localhost:' + PORT + '' + PREFIX + '/pub/$roomID');
|
||||
|
||||
const app = require('express')();
|
||||
const server = require('http').createServer(app);
|
||||
const io = require('socket.io')(server, {path: PREFIX + '/socket.io'});
|
||||
const bodyParser = require('body-parser');
|
||||
|
||||
|
||||
var jsonParser = bodyParser.json();
|
||||
|
||||
app.get('/test', function (req, res) {
|
||||
//res.send('<script src="/socket.io/socket.io.js"></script><script> const socket = io("http://localhost");</script>')
|
||||
res.sendFile(__dirname + '/public/index.html')
|
||||
});
|
||||
|
||||
app.post('/pub/:roomId', jsonParser, function (req,res) {
|
||||
console.log('publish to ' + req.params.roomId + ' data:');
|
||||
console.log(req.body);
|
||||
io.to(req.params.roomId).emit('data',req.body);
|
||||
res.sendStatus(200);
|
||||
});
|
||||
|
||||
|
||||
io.on('connection', function(socket) {
|
||||
console.log('new user ' + socket.id);
|
||||
if (typeof socket.handshake.query.id !== 'undefined') {
|
||||
const room = socket.handshake.query.id;
|
||||
socket.join(room);
|
||||
console.log('user ' + socket.id + ' joined to room ' + room);
|
||||
}
|
||||
socket.on('disconnect', () => {
|
||||
console.log('user ' + socket.id + ' disconnected');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
server.listen(PORT);
|
||||
Reference in New Issue
Block a user