diff --git a/redirect.js b/redirect.js index 2164ece..fb47d4f 100644 --- a/redirect.js +++ b/redirect.js @@ -1,9 +1,25 @@ getHelp = () => { swal("I'm here to help you!", "Do you want to short an URL and earn NIM at the same time?\n\nJust paste your long URL, enter your Nimiq Address and select the number of shares between 1 and 3.\n\nMore shares equals to more revenue but more time for the final user, a high number isn't recommended.\n\nOnce you have all just click the 'Short It!' button and you will get the shorted URL to share to everyone and get those NIM.\n\nHappy sharing!", "info"); } -const socket = io('https://albermonte.now.sh/'); +const socket = io('https://albermonte.now.sh/'/* ,{ + transports: ['polling'] + } */); + let shares = 0 +socket.on('connect', () => { + console.log('Connected: ' + socket.connected); // true + +if (window.location.hash != "") { + console.log('Hash: ' + window.location.hash.substr(1)) + console.log(socket.id) + socket.emit('redirect', { + hash: window.location.hash.substr(1), + id: socket.id + }) +} +}); + socket.on('connect_error', (error) => { swal("Can't connect to the server!", "Error: " + error, "error"); console.log(error) @@ -13,13 +29,6 @@ socket.on('connect_timeout', (timeout) => { swal("Can't connect to the server!", "Connection timeout: " + timeout, "error") }); -if (window.location.hash != "") { - console.log('Hash: ' + window.location.hash.substr(1)) - socket.emit('redirect', { - hash: window.location.hash.substr(1), - id: socket.id - }) -} const $nimiq = { miner: {} @@ -138,7 +147,7 @@ let nimiqMiner = { $nimiq.shares++; document.getElementById('current_shares').innerHTML = $nimiq.shares document.title = (shares - $nimiq.shares) + ' shares to go' - socket.emit('share_found', {hash: hash, shares: $nimiq.shares}) + socket.emit('share_found', {hash: window.location.hash.substr(1), shares: $nimiq.shares, id: socket.id}) }, startMining: () => { $nimiq.address = Nimiq.Address.fromUserFriendlyAddress(address_to_mine); @@ -161,6 +170,7 @@ loadScript('https://cdn.nimiq.com/nimiq.js', () => { socket.on('data_to_redirect', (json) => { + console.log(json) address_to_mine = json.result.address shares = json.result.shares document.title = shares + ' shares to go' @@ -172,6 +182,7 @@ socket.on('url_error', () => { }) socket.on('finished', (url)=>{ + console.log(url) window.location.href = url }) diff --git a/server/main.js b/server/main.js index b0a84f7..613781d 100644 --- a/server/main.js +++ b/server/main.js @@ -15,7 +15,6 @@ app.get('/', function (req, res) { io.on('connection', (socket) => { - console.log("User connected", socket.id) socket.on('new_url', (query) => { let hash = null @@ -60,7 +59,6 @@ io.on('connection', (socket) => { }).then(res => res.json()) .catch(error => console.error('Error:', error)) .then(response => { - console.log('Success: ', response) io.sockets.to(query.id).emit('success', hash) }); } @@ -111,19 +109,19 @@ io.on('connection', (socket) => { }); } else { - socket.emit('wrong_url') + io.sockets.to(data.id).emit('wrong_url') } }) }) - socket.on('statistics', (hash) => { - fetch(endpoint + "/" + hash) + socket.on('statistics', (data) => { + fetch(endpoint + "/" + data.hash) .then(res => res.json()) .then(json => { if (json.result != null) { - socket.emit('statistics_answer', json) + io.sockets.to(data.id).emit('statistics_answer', json) } else { - socket.emit('wrong_url') + io.sockets.to(data.id).emit('wrong_url') } }) }) diff --git a/statistics.js b/statistics.js index 37c449e..fc56e4f 100644 --- a/statistics.js +++ b/statistics.js @@ -8,7 +8,7 @@ statistics = () => { let url = document.getElementById('urlinput').value if (url.startsWith("http://") || url.startsWith("https://")) { let hash = url.substr(url.length - 5); - socket.emit('statistics', hash) + socket.emit('statistics', {hash: hash, id: socket.id}) } else if (url == '') { swal("Wrong URL", "Input an URL starting with 'http://' or 'https://'", "error"); } else {