김대연

Connect with Papago API and make testView

const config = require('../config/config')
const request = require('request');
const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
const dPAPAGO_URL = 'https://openapi.naver.com/v1/papago/detectLangs'
const PAPAGO_ID = process.env.PAPAGO_ID
const PAPAGO_SECRET = process.env.PAPAGO_SECRET
exports.trans = (message, lang, io, room) => {
request.post(
{
url: PAPAGO_URL,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Naver-Client-Id': `${PAPAGO_ID}`,
'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
},
body: `source=${lang}&target=ko&text=` + message,
json:true
},(error, response, body) => {
if(!error && response.statusCode == 200) {
var Translated = body.message.result.translatedText;
io.to(room).emit('chat message', "trans", Translated);
}
});
}
exports.detect = (message,io,room) => {
request.post(
{
url: dPAPAGO_URL,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Naver-Client-Id': `${PAPAGO_ID}`,
'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
},
body: `query=` + message,
json:true
},(error, response, body) => {
if(!error && response.statusCode == 200) {
var lang = body.langCode;
if(lang != 'ko'){
this.trans(message,lang,io,room)
}
}
});
}
This diff is collapsed. Click to expand it.
......@@ -10,7 +10,9 @@
"license": "ISC",
"dependencies": {
"dotenv": "^8.2.0",
"ejs": "^3.1.5",
"express": "^4.17.1",
"request": "^2.88.2",
"socket.io": "^3.0.3"
}
}
......
const config = require(__dirname + '/config/config')
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const port = process.env.SOCKET_PORT;
server.listen(port, () => { console.log(`Listening on port ${port}`) });
io.on('connection', socket => {
console.log("connected socketID : ", socket.id);
io.to(socket.id).emit('my socket id',{socketId: socket.id});
socket.on('enter chatroom', () => {
console.log("channel에 입장");
socket.broadcast.emit('receive chat', {type: "alert", chat: "누군가가 입장하였습니다.", regDate: Date.now()});
})
socket.on('send voice', data => {
console.log(`${socket.id} : ${data.chat}`);
io.emit('send voice', data);
})
socket.on('leave chatroom', data => {
console.log('leave chatroom ', data);
socket.broadcast.emit('receive chat', {type: "alert", chat: "누군가가 퇴장하였습니다.", regDate: Date.now()});
})
})
\ No newline at end of file
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', './views');
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}`);
});
\ No newline at end of file
......
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
<h1>Socket Chat</h1>
\ No newline at end of file
<html>
<head>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font: 13px Helvetica, Arial;
}
form {
background: #000;
padding: 3px;
position: fixed;
bottom: 0;
width: 100%;
}
form input {
border: 0;
padding: 10px;
width: 90%;
margin-right: .5%;
}
form button {
width: 9%;
background: rgb(130, 224, 255);
border: none;
padding: 10px;
}
#messages {
list-style-type: none;
margin: 0;
padding: 0;
}
#messages li {
padding: 5px 10px;
}
#messages li:nth-child(odd) {
background: #eee;
}
</style>
</head>
<body>
<select>
<option value="streamer1">streamer1</option>
<option value="streamer2">streamer2</option>
</select>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off"/>
<button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(() => {
const name = prompt('What your name');
const socket = io();
let room = ['streamer1', 'streamer2'];
let num = 0;
socket.emit('joinRoom', num, name);
$('select').change(() => {
socket.emit('leaveRoom', num, name);
num++;
num = num % 2;
socket.emit('joinRoom', num, name);
});
$('form').submit(() => {
socket.emit('chat message', num, name, $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', (name, msg) => {
$('#messages').append($('<li>').text(name + ' : ' +
msg));
});
socket.on('leaveRoom', (num, name) => {
$('#messages').append($('<li>').text(name + ' leaved '
+ room[num] + ' :('));
});
socket.on('joinRoom', (num, name) => {
$('#messages').append($('<li>').text(name + ' joined '
+ room[num] + ':)'));
});
});
</script>
</body>
</html>
\ No newline at end of file