Initial commit
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
FROM node:14-alpine
|
||||
|
||||
RUN npm install --silent socket.io
|
||||
|
||||
RUN npm install --silent express
|
||||
|
||||
RUN mkdir /srv/public/
|
||||
|
||||
COPY ./app.js /srv/app.js
|
||||
COPY ./public/index.html /srv/public/index.html
|
||||
|
||||
CMD node /srv/app.js
|
||||
@@ -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);
|
||||
@@ -0,0 +1,26 @@
|
||||
version: '3'
|
||||
services:
|
||||
push:
|
||||
image: registry.docker.tlfactory.pl/apps/push-stream-channels:0.1
|
||||
build:
|
||||
context: .
|
||||
dockerfile: ./Dockerfile
|
||||
restart: on-failure
|
||||
environment:
|
||||
- PORT=8080
|
||||
ports:
|
||||
- 80:8080
|
||||
volumes:
|
||||
- ./app.js:/srv/app.js
|
||||
- ./public:/srv/public
|
||||
networks:
|
||||
- backend
|
||||
php:
|
||||
image: registry.docker.tlfactory.pl/apps/php-apache:7.4.20
|
||||
networks:
|
||||
- backend
|
||||
volumes:
|
||||
- ./public/:/var/www/html
|
||||
|
||||
networks:
|
||||
backend:
|
||||
Binary file not shown.
@@ -0,0 +1,13 @@
|
||||
<script src="/ws/socket.io/socket.io.js"></script>
|
||||
<script>
|
||||
const socket = io("http://localhost", {
|
||||
path: '/ws/socket.io',
|
||||
query: 'id=123'
|
||||
});
|
||||
socket.on('data', (arg) => {
|
||||
console.log(arg);
|
||||
});
|
||||
</script>
|
||||
|
||||
<div id=test>
|
||||
</div>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
$ch = curl_init('http://push:8080/pub/123');
|
||||
# Setup request to send json via POST.
|
||||
$payload = json_encode( array( "customer"=> $data ) );
|
||||
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
|
||||
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
|
||||
# Return response instead of printing.
|
||||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
|
||||
# Send request.
|
||||
$result = curl_exec($ch);
|
||||
var_export($result);
|
||||
curl_close($ch);
|
||||
# Print response.
|
||||
echo "<pre>$result</pre>";
|
||||
Reference in New Issue
Block a user