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);
|
||||
takeFromQueue(room);
|
||||
}
|
||||
socket.on('disconnect', () => {
|
||||
const room = socket.handshake.query.id;
|
||||
console.log('user ' + socket.id + ' disconnected');
|
||||
queue[room]=[];
|
||||
console.log('create queue: ' + room);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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
|
||||
});
|
||||
+4
-3
@@ -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
|
||||
|
||||
|
||||
+9
-6
@@ -1,14 +1,17 @@
|
||||
<script src="/ws/socket.io/socket.io.js"></script>
|
||||
<script src="http://localhost:8201/ws/socket.io/socket.io.js"></script>
|
||||
<!-- <script src="/ws/socket.io/socket.io.min.js"></script>-->
|
||||
<script>
|
||||
const socket = io("http://localhost", {
|
||||
const socket = io("http://localhost:8201", {
|
||||
path: '/ws/socket.io',
|
||||
query: 'id=123'
|
||||
query: 'id=channel'
|
||||
});
|
||||
socket.on('data', (arg) => {
|
||||
console.log(arg);
|
||||
const doc = document.getElementById('test');
|
||||
const div = document.createElement('div');
|
||||
div.innerHTML = JSON.stringify(arg);
|
||||
doc.append(div);
|
||||
});
|
||||
</script>
|
||||
|
||||
<div id=test>
|
||||
</div>
|
||||
<pre id="test">
|
||||
</pre>
|
||||
|
||||
+2
-3
@@ -1,8 +1,7 @@
|
||||
<?php
|
||||
|
||||
$ch = curl_init('http://push:8080/pub/123');
|
||||
$ch = curl_init('http://push:8080/pub/channel');
|
||||
# Setup request to send json via POST.
|
||||
$payload = json_encode( array( "customer"=> $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.
|
||||
|
||||
Reference in New Issue
Block a user