You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.4 KiB
51 lines
1.4 KiB
const express = require("express");
|
|
const app = express();
|
|
const server = require('http').createServer(app);
|
|
const io = require('socket.io')(server, {
|
|
cors: {
|
|
origin: "http://127.0.0.1:8000",
|
|
methods: ["GET", "POST"],
|
|
}
|
|
});
|
|
const redisAdapter = require('socket.io-redis');
|
|
const redis = require("redis");
|
|
const subscriber = redis.createClient({host: '127.0.0.1', port: 6379, password: 'root'});
|
|
|
|
//setup redis
|
|
io.adapter(redisAdapter({host: '127.0.0.1', port: 6379, password: 'root'}));
|
|
|
|
// parse application/x-www-form-urlencoded
|
|
app.use(express.urlencoded({ extended: false }));
|
|
|
|
// parse application/json
|
|
app.use(express.json());
|
|
|
|
// server main route
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(__dirname + '/static/index.html');
|
|
});
|
|
|
|
io.on('connection', async (socket) => {
|
|
socket.join('room');
|
|
// this is just for testing.
|
|
setTimeout(() => {
|
|
io.to('room').emit('room-event', {index: 'users', id: undefined});
|
|
}, 10*1000);
|
|
});
|
|
|
|
|
|
app.post('/emit/:room', function (req, res) {
|
|
console.log('room is :','room-' + req.params.room);
|
|
console.log('message',req.body.data.title);
|
|
io.to('room-' + req.params.room).emit('room-event', req.body.data.title);
|
|
res.send('done');
|
|
});
|
|
subscriber.on("message", function (channel, message) {
|
|
io.to('room').emit('server-msg', message);
|
|
});
|
|
|
|
server.listen(3030, () => {
|
|
console.log('listening on *:3030');
|
|
});
|
|
|
|
subscriber.subscribe("test-channel");
|