test queue
This commit is contained in:
@@ -21,6 +21,7 @@ const bodyParser = require('body-parser');
|
||||
|
||||
|
||||
var jsonParser = bodyParser.json();
|
||||
const queue = {};
|
||||
|
||||
app.get('/test', function (req, res) {
|
||||
//res.send('<script src="/socket.io/socket.io.js"></script><script> const socket = io("http://localhost");</script>')
|
||||
@@ -28,22 +29,49 @@ app.get('/test', function (req, res) {
|
||||
});
|
||||
|
||||
app.post('/pub/:roomId', jsonParser, function (req,res) {
|
||||
console.log('publish to ' + req.params.roomId + ' data:');
|
||||
const room = req.params.roomId;
|
||||
console.log('publish to ' + room + ' data:');
|
||||
console.log(req.body);
|
||||
io.to(req.params.roomId).emit('data',req.body);
|
||||
if (typeof queue[room] !== 'undefined') {
|
||||
queue[room].push(req.body);
|
||||
} else {
|
||||
io.to(room).emit('data',req.body);
|
||||
}
|
||||
console.log(queue);
|
||||
res.sendStatus(200);
|
||||
});
|
||||
|
||||
function takeFromQueue(room) {
|
||||
if (typeof queue[room] == 'undefined') {
|
||||
return;
|
||||
}
|
||||
const d = queue[room].shift();
|
||||
if (typeof d === 'undefined') {
|
||||
delete queue[room];
|
||||
console.log("Queue delivered. Remove obj.")
|
||||
return;
|
||||
}
|
||||
io.to(room).emit('data',d);
|
||||
|
||||
setTimeout(()=>{
|
||||
takeFromQueue(room);
|
||||
}, 500);
|
||||
}
|
||||
|
||||
io.on('connection', function(socket) {
|
||||
console.log('new user ' + socket.id);
|
||||
console.log("_________________________")
|
||||
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);
|
||||
}
|
||||
const room = socket.handshake.query.id;
|
||||
socket.join(room);
|
||||
console.log('user ' + socket.id + ' joined to room ' + room);
|
||||
takeFromQueue(room);
|
||||
}
|
||||
socket.on('disconnect', () => {
|
||||
console.log('user ' + socket.id + ' disconnected');
|
||||
const room = socket.handshake.query.id;
|
||||
console.log('user ' + socket.id + ' disconnected');
|
||||
queue[room]=[];
|
||||
console.log('create queue: ' + room);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user