Merge branch 'kdysy1130-socket' into 'master'
Kdysy1130 socket kdysy1130-socket branch에 env, config설정 socket example server 만듦. See merge request !2
Showing
8 changed files
with
239 additions
and
0 deletions
server/.gitignore
0 → 100644
server/config/config.js
0 → 100644
1 | +const path = require('path') | ||
2 | +require('dotenv').config({path: path.join(__dirname, "../.env")}) | ||
3 | + | ||
4 | +const ip = process.env.IP || null | ||
5 | +const s_port = process.env.SOCKET_PORT || null | ||
6 | +const token = process.env.TOKEN || null | ||
7 | + | ||
8 | +module.exports = { | ||
9 | + 'ip' : ip, | ||
10 | + 's_port' : s_port, | ||
11 | + 'token' : token | ||
12 | +} |
server/openAPIs/papago_api.js
0 → 100644
1 | +const config = require('../config/config') | ||
2 | +const request = require('request'); | ||
3 | + | ||
4 | +const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt' | ||
5 | +const dPAPAGO_URL = 'https://openapi.naver.com/v1/papago/detectLangs' | ||
6 | +const PAPAGO_ID = process.env.PAPAGO_ID | ||
7 | +const PAPAGO_SECRET = process.env.PAPAGO_SECRET | ||
8 | + | ||
9 | +exports.trans = (message, lang, io, room) => { | ||
10 | + request.post( | ||
11 | + { | ||
12 | + url: PAPAGO_URL, | ||
13 | + headers: { | ||
14 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | ||
15 | + 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
16 | + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
17 | + }, | ||
18 | + body: `source=${lang}&target=ko&text=` + message, | ||
19 | + json:true | ||
20 | + },(error, response, body) => { | ||
21 | + if(!error && response.statusCode == 200) { | ||
22 | + var Translated = body.message.result.translatedText; | ||
23 | + io.to(room).emit('chat message', "trans", Translated); | ||
24 | + } | ||
25 | + }); | ||
26 | +} | ||
27 | + | ||
28 | + | ||
29 | +exports.detect = (message,io,room) => { | ||
30 | + request.post( | ||
31 | + { | ||
32 | + url: dPAPAGO_URL, | ||
33 | + headers: { | ||
34 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | ||
35 | + 'X-Naver-Client-Id': `${PAPAGO_ID}`, | ||
36 | + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}` | ||
37 | + }, | ||
38 | + body: `query=` + message, | ||
39 | + json:true | ||
40 | + },(error, response, body) => { | ||
41 | + if(!error && response.statusCode == 200) { | ||
42 | + var lang = body.langCode; | ||
43 | + if(lang != 'ko'){ | ||
44 | + this.trans(message,lang,io,room) | ||
45 | + } | ||
46 | + } | ||
47 | + }); | ||
48 | +} |
server/openAPIs/twitch_api.js
0 → 100644
File mode changed
server/package-lock.json
0 → 100644
This diff is collapsed. Click to expand it.
server/package.json
0 → 100644
1 | +{ | ||
2 | + "name": "khuwitch-server", | ||
3 | + "version": "1.0.0", | ||
4 | + "description": "twitch translator chatbot & tts server dev", | ||
5 | + "main": "socket_server.js", | ||
6 | + "scripts": { | ||
7 | + "test": "echo \"Error: no test specified\" && exit 1" | ||
8 | + }, | ||
9 | + "author": "Daeyeonkim97", | ||
10 | + "license": "ISC", | ||
11 | + "dependencies": { | ||
12 | + "dotenv": "^8.2.0", | ||
13 | + "ejs": "^3.1.5", | ||
14 | + "express": "^4.17.1", | ||
15 | + "request": "^2.88.2", | ||
16 | + "socket.io": "^3.0.3" | ||
17 | + } | ||
18 | +} |
server/socket_server.js
0 → 100644
1 | +const config = require('./config/config') | ||
2 | + | ||
3 | +const app = require('express')(); | ||
4 | +const http = require('http').Server(app); | ||
5 | +const io = require('socket.io')(http); | ||
6 | +const papago = require('./openAPIs/papago_api') | ||
7 | + | ||
8 | +app.set('view engine', 'ejs'); | ||
9 | +app.set('views', './testviews'); | ||
10 | + | ||
11 | +let room = ['streamer1', 'streamer2']; | ||
12 | +let a = 0; | ||
13 | + | ||
14 | +app.get('/', (req, res) => { | ||
15 | + res.render('chat'); | ||
16 | +}); | ||
17 | + | ||
18 | +app.get('/list',(req,res) => { | ||
19 | + res.send(room); | ||
20 | +}) | ||
21 | + | ||
22 | +app.post('/add',(req,res)=>{ | ||
23 | + room.append(req.body.streamer); | ||
24 | + res.send(req.body.streamer); | ||
25 | +}) | ||
26 | + | ||
27 | + | ||
28 | +io.on('connection', (socket) => { | ||
29 | + socket.on('disconnect', () => { | ||
30 | + console.log('user disconnected'); | ||
31 | + }); | ||
32 | + | ||
33 | + socket.on('leaveRoom', (num,name) => { | ||
34 | + socket.leave(room[num], () => { | ||
35 | + console.log('Someone leave a ' + room[num]); | ||
36 | + io.to(room[num]).emit('leaveRoom', num ,name); | ||
37 | + }); | ||
38 | + }); | ||
39 | + | ||
40 | + socket.on('joinRoom', (num,name) => { | ||
41 | + socket.join(room[num], () => { | ||
42 | + console.log('Someone join a ' + room[num]); | ||
43 | + io.to(room[num]).emit('joinRoom', num ,name); | ||
44 | + }); | ||
45 | + }); | ||
46 | + | ||
47 | + socket.on('chat message', (num,name, msg) => { | ||
48 | + papago.detect(msg,io,room[num]); | ||
49 | + io.to(room[a]).emit('chat message', name, msg); | ||
50 | + }); | ||
51 | +}); | ||
52 | + | ||
53 | + | ||
54 | +http.listen(process.env.SOCKET_PORT, () => { | ||
55 | + console.log(`Connect at ${process.env.SOCKET_PORT}`); | ||
56 | +}); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
server/testviews/chat.ejs
0 → 100644
1 | +<html> | ||
2 | +<head> | ||
3 | + <style> | ||
4 | + * { | ||
5 | + margin: 0; | ||
6 | + padding: 0; | ||
7 | + box-sizing: border-box; | ||
8 | + } | ||
9 | + | ||
10 | + body { | ||
11 | + font: 13px Helvetica, Arial; | ||
12 | + } | ||
13 | + | ||
14 | + form { | ||
15 | + background: #000; | ||
16 | + padding: 3px; | ||
17 | + position: fixed; | ||
18 | + bottom: 0; | ||
19 | + width: 100%; | ||
20 | + } | ||
21 | + | ||
22 | + form input { | ||
23 | + border: 0; | ||
24 | + padding: 10px; | ||
25 | + width: 90%; | ||
26 | + margin-right: .5%; | ||
27 | + } | ||
28 | + | ||
29 | + form button { | ||
30 | + width: 9%; | ||
31 | + background: rgb(130, 224, 255); | ||
32 | + border: none; | ||
33 | + padding: 10px; | ||
34 | + } | ||
35 | + | ||
36 | + #messages { | ||
37 | + list-style-type: none; | ||
38 | + margin: 0; | ||
39 | + padding: 0; | ||
40 | + } | ||
41 | + | ||
42 | + #messages li { | ||
43 | + padding: 5px 10px; | ||
44 | + } | ||
45 | + | ||
46 | + #messages li:nth-child(odd) { | ||
47 | + background: #eee; | ||
48 | + } | ||
49 | + </style> | ||
50 | +</head> | ||
51 | +<body> | ||
52 | +<select> | ||
53 | + <option value="streamer1">streamer1</option> | ||
54 | + <option value="streamer2">streamer2</option> | ||
55 | +</select> | ||
56 | +<ul id="messages"></ul> | ||
57 | +<form action=""> | ||
58 | + <input id="m" autocomplete="off"/> | ||
59 | + <button>Send</button> | ||
60 | +</form> | ||
61 | +<script src="/socket.io/socket.io.js"></script> | ||
62 | +<script src="http://code.jquery.com/jquery-1.11.1.js"></script> | ||
63 | +<script> | ||
64 | + $(() => { | ||
65 | + const name = prompt('What your name'); | ||
66 | + const socket = io(); | ||
67 | + let room = ['streamer1', 'streamer2']; | ||
68 | + let num = 0; | ||
69 | + | ||
70 | + socket.emit('joinRoom', num, name); | ||
71 | + | ||
72 | + $('select').change(() => { | ||
73 | + socket.emit('leaveRoom', num, name); | ||
74 | + num++; | ||
75 | + num = num % 2; | ||
76 | + socket.emit('joinRoom', num, name); | ||
77 | + }); | ||
78 | + | ||
79 | + | ||
80 | + $('form').submit(() => { | ||
81 | + socket.emit('chat message', num, name, $('#m').val()); | ||
82 | + $('#m').val(''); | ||
83 | + return false; | ||
84 | + }); | ||
85 | + | ||
86 | + socket.on('chat message', (name, msg) => { | ||
87 | + $('#messages').append($('<li>').text(name + ' : ' + | ||
88 | + msg)); | ||
89 | + }); | ||
90 | + | ||
91 | + socket.on('leaveRoom', (num, name) => { | ||
92 | + $('#messages').append($('<li>').text(name + ' leaved ' | ||
93 | + + room[num] + ' :(')); | ||
94 | + }); | ||
95 | + | ||
96 | + socket.on('joinRoom', (num, name) => { | ||
97 | + $('#messages').append($('<li>').text(name + ' joined ' | ||
98 | + + room[num] + ':)')); | ||
99 | + }); | ||
100 | + }); | ||
101 | +</script> | ||
102 | +</body> | ||
103 | +</html> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment