socket_server.js
1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const config = require('./config/config')
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const papago = require('./openAPIs/papago_api')
app.set('view engine', 'ejs');
app.set('views', './testviews');
let room = ['streamer1', 'streamer2'];
let a = 0;
app.get('/', (req, res) => {
res.render('chat');
});
app.get('/list',(req,res) => {
res.send(room);
})
app.post('/add',(req,res)=>{
room.append(req.body.streamer);
res.send(req.body.streamer);
})
io.on('connection', (socket) => {
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.on('leaveRoom', (num,name) => {
socket.leave(room[num], () => {
console.log('Someone leave a ' + room[num]);
io.to(room[num]).emit('leaveRoom', num ,name);
});
});
socket.on('joinRoom', (num,name) => {
socket.join(room[num], () => {
console.log('Someone join a ' + room[num]);
io.to(room[num]).emit('joinRoom', num ,name);
});
});
socket.on('chat message', (num,name, msg) => {
papago.detect(msg,io,room[num]);
io.to(room[a]).emit('chat message', name, msg);
});
});
http.listen(process.env.SOCKET_PORT, () => {
console.log(`Connect at ${process.env.SOCKET_PORT}`);
});