-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (43 loc) · 1.16 KB
/
index.js
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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const fetch = require('node-fetch');
const PORT = process.env.PORT || 3000;
const URL = 'https://rocky-mesa-28651.herokuapp.com'
app.get('/', function(req, res)
{
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket)
{
console.log(' connected');
socket.on('disconnect', function()
{
console.log('user disconnected');
});
socket.on('shaggy', function(msg)
{
headers={'Content-Type' : 'application/json'};
fetch(URL, {method : 'POST', headers : headers, body : JSON.stringify({"language" : msg.targetLang, "text" : msg.text})})
.then(res => res.json())
.then(body => {
toSend = {};
console.log(msg)
toSend[msg.lang] = {
message : msg.text,
language : msg.lang,
};
toSend[body.language] = {
message : body.translatedText,
language : body.language
};
toSend["username"] = msg.username;
console.log(toSend)
io.emit('scooby', toSend);
});
});
});
http.listen(PORT, function()
{
console.log('listening on ' + PORT);
});