220 lines
22 KiB
HTML
220 lines
22 KiB
HTML
|
|
<title>Text-Translation (Multi-Lingual) using Conversation.js ® Muaz Khan</title>
|
|||
|
|
|
|||
|
|
<h1 style="text-align: center;display: block;border-bottom: 1px solid #CCCCCC;margin-bottom: 0em;padding: .6em 0;font-size: 2em;">
|
|||
|
|
Text-Translation (Multi-Lingual) using <a href="https://github.com/muaz-khan/WebRTC-Experiment/tree/master/Conversation.js" style="text-decoration:none;">Conversation.js</a>
|
|||
|
|
</h1>
|
|||
|
|
|
|||
|
|
<link href="common-styles.css" rel="stylesheet">
|
|||
|
|
|
|||
|
|
<!-- www.RTCMultiConnection.org -->
|
|||
|
|
<script src="//cdn.webrtc-experiment.com/RTCMultiConnection.js"></script>
|
|||
|
|
|
|||
|
|
<!-- github.com/muaz-khan/FileBufferReader -->
|
|||
|
|
<script src="//cdn.webrtc-experiment.com/FileBufferReader.js"></script>
|
|||
|
|
|
|||
|
|
<!-- conversation.js; runs top over RTCMultiConnection.js -->
|
|||
|
|
<script src="//cdn.webrtc-experiment.com/conversation.js"></script>
|
|||
|
|
|
|||
|
|
<!-- modify it to use websockets or XMPP or whatever! -->
|
|||
|
|
<script src="common-signaling.js"></script>
|
|||
|
|
|
|||
|
|
<button id="btn-invite-username" style="position:absolute; right:0;">Invite</button>
|
|||
|
|
<input id="invite-username" type="text" placeholder="invite user" results=4>
|
|||
|
|
|
|||
|
|
<input id="input-text-chat" type="text" disabled>
|
|||
|
|
<div id="output"></div>
|
|||
|
|
|
|||
|
|
<audio src="data:audio/mp3;base64,//OkVAARncUUyjDDlA/Y7jF0Ykw9WB2qmuqHYWA3TKNX+cPHidGYMq1lXM47s43HSc5+ksJ0YllxAEsUMJ1DZbPphaZOiB6RAvYbLaHNKlNCeJSeJTSm8po8J3hH+8NA7I8J6cJ4IDsBghAdkrx0U0//5TS9NP+aV4TxKIeJvFN0ulNNKaU0puhPC7uhPpTT/+UxZAPnCYWdWByH44kFho4vXRpNtMIAMB8BgEzImOgmWDB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qteqc387lv///wudPNfKP1dfbOL6i9l7V2UVmaNYflBn+nrqg+/4uRH1MK2Kub7PAjpFmdpSlk0QSSmo27GjIwAIElcj+tQXagm9FXaqw3GQu3FdtK+xskqF3FLT1NS7gkwCz3Bi8uy92IvY05Ex+NXlW3XtZ6rXqnN/O5b///8LgNLTgn2dvlLew+qwUnLTJM6ZJn/1pomWxqv1mUU2GegAP5HrUlLafHHKbpKedntdmaWxhqO2Idl0DNef5QtK0EjRrdgC0UAySQgaJcAJzgYwGwDA0DV44hzCuYFwwQNzNCmpBN0FJJF1A2OpIX/Wv9e//OkVFAQjM1Su+3UAQwIhqp52tgAvrNThFk0Sus+yKK22UbA0bIZ7BU7/nud1gqGsSw6KPAT1PX/mv3P2el5s2fdUXLipBaxUjDspgBAAABSulpbU3SV521rszKbGGpmxGbUWd6mfNxUERoZxN0GemxJAgrDMmKg4oDAqkpjMfqsa2TLAACu5Aq/bkhucR0SY4vkVq7NpFz1z1hx0kxqMyVsSnIXM
|
|||
|
|
|
|||
|
|
<!-- this section handles UI-part -->
|
|||
|
|
<script>
|
|||
|
|
// button used to invite a user
|
|||
|
|
document.getElementById('btn-invite-username').onclick = function() {
|
|||
|
|
this.disabled = true;
|
|||
|
|
user.openconversationwith( document.getElementById('invite-username').value );
|
|||
|
|
document.getElementById('invite-username').disabled = true;
|
|||
|
|
};
|
|||
|
|
document.getElementById('invite-username').onkeyup = function(e) {
|
|||
|
|
if(e.keyCode == 13) document.getElementById('btn-invite-username').onclick();
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// text chat messages & boxes
|
|||
|
|
var inputTextChat = document.querySelector('#input-text-chat');
|
|||
|
|
|
|||
|
|
var numberOfKeys = 0;
|
|||
|
|
var lastMessageUUID;
|
|||
|
|
|
|||
|
|
inputTextChat.onkeyup = function(e) {
|
|||
|
|
numberOfKeys++;
|
|||
|
|
if (numberOfKeys > 3) numberOfKeys = 0;
|
|||
|
|
|
|||
|
|
if (!numberOfKeys) {
|
|||
|
|
if (!lastMessageUUID) lastMessageUUID = Math.round(Math.random() * 999999999) + 9995000;
|
|||
|
|
user.peers.emit('message', {
|
|||
|
|
typing: true,
|
|||
|
|
lastMessageUUID: lastMessageUUID
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (e.keyCode != 13) return;
|
|||
|
|
|
|||
|
|
// removing trailing/leading whitespace
|
|||
|
|
this.value = this.value.replace(/^\s+|\s+$/g, '');
|
|||
|
|
|
|||
|
|
if (!this.value.length) return;
|
|||
|
|
|
|||
|
|
user.peers.emit('message', {
|
|||
|
|
lastMessageUUID: lastMessageUUID,
|
|||
|
|
message: this.value
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
lastMessageUUID = null;
|
|||
|
|
|
|||
|
|
// self preview!
|
|||
|
|
appendDIV({
|
|||
|
|
username: user.username,
|
|||
|
|
message: this.value
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
this.value = '';
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
var output = document.querySelector('#output');
|
|||
|
|
|
|||
|
|
// a custom method used to append a new DIV into DOM
|
|||
|
|
function appendDIV(data) {
|
|||
|
|
var existing = false;
|
|||
|
|
if (document.getElementById(data.lastMessageUUID)) {
|
|||
|
|
div = document.getElementById(data.lastMessageUUID);
|
|||
|
|
existing = true;
|
|||
|
|
} else {
|
|||
|
|
div = document.createElement('div');
|
|||
|
|
if (data.lastMessageUUID) {
|
|||
|
|
div.id = data.lastMessageUUID;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(user.peers[data.username] && !data.translated) {
|
|||
|
|
user.peers[data.username].Translator.TranslateText(data.message, function(convertedText) {
|
|||
|
|
data.message = convertedText;
|
|||
|
|
data.translated = true;
|
|||
|
|
appendDIV(data);
|
|||
|
|
});
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
div.innerHTML = '<section class="name">' + data.username + '</section><section class="message">' + data.message + '</section>';
|
|||
|
|
|
|||
|
|
if (!existing) {
|
|||
|
|
output.insertBefore(div, output.firstChild);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
div.tabIndex = 0;
|
|||
|
|
div.focus();
|
|||
|
|
|
|||
|
|
inputTextChat.focus();
|
|||
|
|
}
|
|||
|
|
</script>
|
|||
|
|
|
|||
|
|
<!-- this section initializes "User" object -->
|
|||
|
|
<script>
|
|||
|
|
var user = new User();
|
|||
|
|
|
|||
|
|
// a custom method used to append a new DIV into DOM
|
|||
|
|
appendDIV({
|
|||
|
|
username: 'System',
|
|||
|
|
message: 'Your username is: <input type="text" style="width:auto" value="' + user.username + '" onkeyup="user.username=this.value;">'
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// these are RTCMultiConnection.js defaults
|
|||
|
|
// RTCMultiConnection.js is big-javascript library
|
|||
|
|
// RTCMultiConnection provides tons of WebRTC features!
|
|||
|
|
// read more here: www.RTCMultiConnection.org/docs/
|
|||
|
|
user.defaults = {
|
|||
|
|
log: true, // for production use only.
|
|||
|
|
trickleIce: true, // for SIP/XMPP and XHR
|
|||
|
|
getExternalIceServers: true, // ice-servers from xirsys.com
|
|||
|
|
leaveOnPageUnload: true,
|
|||
|
|
/*iceServers: [{
|
|||
|
|
url: 'stun:stun.l.google.com:19302'
|
|||
|
|
}],*/
|
|||
|
|
iceProtocols: {
|
|||
|
|
tcp: true,
|
|||
|
|
udp: true
|
|||
|
|
},
|
|||
|
|
candidates: {
|
|||
|
|
host: true, // local/host candidates
|
|||
|
|
reflexive: true, // STUN candidates
|
|||
|
|
relay: true // TURN candidates
|
|||
|
|
},
|
|||
|
|
autoReDialOnFailure: false, // renegotiation will not work if it is true
|
|||
|
|
// autoTranslateText: true,
|
|||
|
|
language: 'ar', // target-translation-language
|
|||
|
|
googKey: 'AIzaSyCUmCjvKRb-kOYrnoL2xaXb8I-_JJeKpf0' // You MUST use your OWN key.
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// optional method to display the logs
|
|||
|
|
user.on('log', function(message) {
|
|||
|
|
console.log(message);
|
|||
|
|
appendDIV({
|
|||
|
|
username: 'Dev-Logs',
|
|||
|
|
message: message.replace(/\\r\\n/g, '<br />')
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// "signaler" object is defined in "common-signaling.js"
|
|||
|
|
// this method connects users to signaling-channel
|
|||
|
|
// so that user can call/search/invite any other user
|
|||
|
|
// also, other users can search him.
|
|||
|
|
signaler.connect(user);
|
|||
|
|
|
|||
|
|
// this event is fired when conversation is opened
|
|||
|
|
user.on('conversation-opened', function (conversation) {
|
|||
|
|
inputTextChat.disabled = false;
|
|||
|
|
|
|||
|
|
// see, how simple it is to send a data-channel message
|
|||
|
|
// it a private conversation between two users!
|
|||
|
|
conversation.emit('message', {
|
|||
|
|
username: user.username,
|
|||
|
|
message: 'Hello there, data connection has been opened between you and me!'
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// receive all messages passed in this private conversation
|
|||
|
|
conversation.on('message', function (sender) {
|
|||
|
|
var message = sender.data;
|
|||
|
|
if(message.typing) {
|
|||
|
|
appendDIV({
|
|||
|
|
username: sender.username,
|
|||
|
|
message: sender.username + ' is typing...',
|
|||
|
|
lastMessageUUID: message.lastMessageUUID
|
|||
|
|
});
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(typeof message == 'string' && message.indexOf('is ready to share text messages with you') != -1) {
|
|||
|
|
user.conversations[sender.username].emit('message', 'Hi ' + sender.username + ', ' + user.username + ' is also online.');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(message.lastMessageUUID) {
|
|||
|
|
appendDIV({
|
|||
|
|
username: sender.username,
|
|||
|
|
message: message.message,
|
|||
|
|
lastMessageUUID: message.lastMessageUUID
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
else appendDIV(message);
|
|||
|
|
|
|||
|
|
document.getElementById('message-sound').play();
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
</script>
|
|||
|
|
|
|||
|
|
<footer style="font-size: .8em; text-align: center;">
|
|||
|
|
<a href="https://www.webrtc-experiment.com/">WebRTC Experiments!</a>
|
|||
|
|
© <a href="https://plus.google.com/+MuazKhan" rel="author">Muaz Khan</a>
|
|||
|
|
» <a href="mailto:muazkh@gmail.com" target="_blank">Email</a>
|
|||
|
|
» <a href="http://twitter.com/muazkh">@muazkh</a>
|
|||
|
|
» <a href="https://github.com/muaz-khan">Github</a>
|
|||
|
|
</footer>
|