diff --git a/app.js b/app.js index 0d0728d..050eddc 100644 --- a/app.js +++ b/app.js @@ -21,6 +21,7 @@ const bodyParser = require('body-parser'); var jsonParser = bodyParser.json(); +const queue = {}; app.get('/test', function (req, res) { //res.send('') @@ -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); }); }); diff --git a/client.js b/client.js new file mode 100644 index 0000000..c994f69 --- /dev/null +++ b/client.js @@ -0,0 +1,20 @@ +const PORT = process.env.PORT || 8080; +const PREFIX = process.env.PREFIX || '/ws'; +const DEV = process.env.DEV || false; +console.log('Socket.io listen on: http://localhost:' + PORT + '' + PREFIX + '/socket.io'); +console.log('Publisher listen on: http://localhost:' + PORT + '' + PREFIX + '/pub/$roomID'); +if (DEV !== false) { + console.log('System is on DEV mode'); +} +if (DEV !== false) { + io = require('socket.io')(server, {path: PREFIX + '/socket.io', cors: {origin: '*',}}); +} else { + io = require('socket.io')(server, {path: PREFIX + '/socket.io'}); +} + + +io.connect("", { + path: '/ws/socket.io', + query: "id=channel", + reconnection: false +}); \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index aeba3d9..45388ad 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: '3' services: push: - image: docker.git.tlfactory.pl/apps/push-stream-channels:0.4 + image: docker.git.tlfactory.pl/apps/push-stream-channels:0.4.1 build: context: . dockerfile: ./Dockerfile @@ -15,7 +15,8 @@ services: - ./app.js:/srv/app.js - ./public:/srv/public php: - image: registry.docker.tlfactory.pl/apps/php-apache:7.4.20 + image: docker.git.tlfactory.pl/apps/php-apache:7.4.20 + ports: + - 8123:80 volumes: - ./public/:/var/www/html - diff --git a/public/index.html b/public/index.html index af76e11..2edd437 100644 --- a/public/index.html +++ b/public/index.html @@ -1,14 +1,17 @@ - + - -
+diff --git a/public/test.php b/public/test.php index f6247bb..384016a 100644 --- a/public/test.php +++ b/public/test.php @@ -1,8 +1,7 @@ $data ) ); +$payload = json_encode( array( "customer"=> $argv[1]) ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); # Return response instead of printing.