Onionr/static-data/www/shared/direct-connections.js

61 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2019-08-16 20:41:56 +00:00
/*
Onionr - Private P2P Communication
Functions to detect
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
*/
direct_connections = {}
let waitForConnection = function(pubkey){
fetch('/dc-client/isconnected/' + pubkey, {
headers: {
"token": webpass
}})
2019-08-16 22:40:17 +00:00
.then((resp) => resp.text())
2019-08-16 20:41:56 +00:00
.then(function(resp) {
if (resp === ""){
2019-08-16 22:40:17 +00:00
// Try to get the client address again again in a few seconds
setTimeout(function(){waitForConnection(pubkey)}, 3000)
}
else{
// add to the dc object
direct_connections[pubkey] = resp
2019-08-16 20:41:56 +00:00
}
})
}
let createConnection = function(pubkey){
2019-08-16 22:40:17 +00:00
// Tells the Onionr daemon to create a client connection to a remote peer for generic direct connections
// If the pubkey is already connected, don't bother
2019-08-16 20:41:56 +00:00
if (direct_connections.hasOwnProperty(pubkey)){
return
}
2019-08-16 22:40:17 +00:00
// Do the request, then spawn a function to wait for the connection to be created
2019-08-16 20:41:56 +00:00
fetch('/dc-client/connect/' + pubkey, {
headers: {
"token": webpass
}})
2019-08-16 22:40:17 +00:00
.then((resp) => resp.text())
2019-08-16 20:41:56 +00:00
.then(function(resp) {
2019-08-16 22:40:17 +00:00
if (resp === "pending"){
setTimeout(function(){waitForConnection(pubkey)}, 3000)
2019-08-16 20:41:56 +00:00
}
else{
direct_connections[pubkey] = resp
}
2019-08-16 20:41:56 +00:00
})
}