김대연

Connect with Papago API and make testView

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 +}
This diff is collapsed. Click to expand it.
...@@ -10,7 +10,9 @@ ...@@ -10,7 +10,9 @@
10 "license": "ISC", 10 "license": "ISC",
11 "dependencies": { 11 "dependencies": {
12 "dotenv": "^8.2.0", 12 "dotenv": "^8.2.0",
13 + "ejs": "^3.1.5",
13 "express": "^4.17.1", 14 "express": "^4.17.1",
15 + "request": "^2.88.2",
14 "socket.io": "^3.0.3" 16 "socket.io": "^3.0.3"
15 } 17 }
16 } 18 }
......
1 -const config = require(__dirname + '/config/config') 1 +const config = require('./config/config')
2 2
3 -const express = require('express'); 3 +const app = require('express')();
4 -const app = express(); 4 +const http = require('http').Server(app);
5 -const server = require('http').Server(app); 5 +const io = require('socket.io')(http);
6 -const io = require('socket.io')(server); 6 +const papago = require('./openAPIs/papago_api')
7 -const port = process.env.SOCKET_PORT;
8 7
9 -server.listen(port, () => { console.log(`Listening on port ${port}`) }); 8 +app.set('view engine', 'ejs');
9 +app.set('views', './views');
10 10
11 -io.on('connection', socket => { 11 +let room = ['streamer1', 'streamer2'];
12 - console.log("connected socketID : ", socket.id); 12 +let a = 0;
13 - io.to(socket.id).emit('my socket id',{socketId: socket.id});
14 13
15 - socket.on('enter chatroom', () => { 14 +app.get('/', (req, res) => {
16 - console.log("channel에 입장"); 15 + res.render('chat');
17 - socket.broadcast.emit('receive chat', {type: "alert", chat: "누군가가 입장하였습니다.", regDate: Date.now()}); 16 +});
18 - })
19 17
20 - socket.on('send voice', data => { 18 +app.get('/list',(req,res) => {
21 - console.log(`${socket.id} : ${data.chat}`); 19 + res.send(room);
22 - io.emit('send voice', data); 20 +})
23 - })
24 -
25 - socket.on('leave chatroom', data => {
26 - console.log('leave chatroom ', data);
27 - socket.broadcast.emit('receive chat', {type: "alert", chat: "누군가가 퇴장하였습니다.", regDate: Date.now()});
28 - })
29 21
22 +app.post('/add',(req,res)=>{
23 + room.append(req.body.streamer);
24 + res.send(req.body.streamer);
30 }) 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
......
1 -<script src="/socket.io/socket.io.js"></script>
2 -<script>
3 - var socket = io();
4 -</script>
5 -<h1>Socket Chat</h1>
...\ No newline at end of file ...\ No newline at end of file
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