sentanos / roblox-js

!!!THIS PROJECT IS NO LONGER MAINTAINED!!! Execute ROBLOX website actions in node.js
MIT License
45 stars 45 forks source link

onMessage event doesn't fire when API reconnects #25

Closed dylanohughes closed 7 years ago

dylanohughes commented 7 years ago

I believe I've noticed an issue after a few days of using this API. I've been modifying my code to see if is anything on my part, but I I am 90% sure it isn't. I have a Node server running 24/7, but after I see the following in the console, it doesn't seem as if the onMessage event fires anymore.

Scheduled Reconnection: Error: read ECONNRESET Reconnected!

Help is greatly appreciated.

sentanos commented 7 years ago

The socket is losing connection which is expected when the server is running nonstop for so long. However, this should be fine because the socket automatically reconnects in these cases (as you can see from the output). This is very difficult to reproduce because you have to run the server for so long without guarantee that it will actually error. When manually closing the socket, however, the function continued to work after reconnecting.

Make sure you are catching errors on the event by using something like this:

event.on('error', function (err) {
  console.error('onMessage error: ' + err);
});

As uncaught errors may terminate the script. This may not be the reason but you should at least try adding it and see what happens.

dylanohughes commented 7 years ago

Thanks for the reply.

I've tried adding the code you've stated above, however, no error is being shown. Only what I said in my initial comment is being shown within the console.

After modifying my code a little more, I've noticed the below error only appears when I use the "onMessage" event. It is displayed when I first start up the bot. At first, I wasn't sure what this was but it may have something to do with it.

SyntaxError: Unexpected token < in JSON at position 4 at JSON.parse (<anonymous>) at /home/projado/Discord/SCPBot/node_modules/roblox-js/lib/getMessages.js:22:21 at tryCatcher (/home/projado/Discord/SCPBot/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/projado/Discord/SCPBot/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (/home/projado/Discord/SCPBot/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromise0 (/home/projado/Discord/SCPBot/node_modules/bluebird/js/release/promise.js:614:10) at Promise._settlePromises (/home/projado/Discord/SCPBot/node_modules/bluebird/js/release/promise.js:693:18) at Async._drainQueue (/home/projado/Discord/SCPBot/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/home/projado/Discord/SCPBot/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/home/projado/Discord/SCPBot/node_modules/bluebird/js/release/async.js:17:14) at runCallback (timers.js:672:20) at tryOnImmediate (timers.js:645:5)

sentanos commented 7 years ago

That error usually comes up when the user is not logged in or something, it shouldn't be related to any direct problem.

Anyways I've made a couple modifications that may fix your problem. Firstly errors will actually be caught now, before they were being eaten because I forgot to catch the onNotification errors in onMessage. Secondly the reconnection for onNotification will use a completely new connection, it's possible your session expired or something else behind the scenes that was causing the connection to stop. Continue catching errors as I instructed before.

I have not committed anything yet, since I don't have the ability to replicate these errors right now I'll need you to test it for me. Replace the files with these for onMessage and onNotification and tell me if it fixes the problem.

dylanohughes commented 7 years ago

It seems to be working as expected now. Thank you very much for fixing it!

I'm not sure if the "Connection aborted" messages were debug messages, so I thought I'd let you know:

Scheduled Reconnection: Error: read ECONNRESET onMessage Error: Error: Lost connection, reconnecting Connection aborted Scheduled Reconnection: Error: read ECONNRESET onMessage Error: Error: Lost connection, reconnecting Connection aborted

Everything works fine though! :)

dylanohughes commented 7 years ago

Seems as if there's still an error. It's still appearing to disconnect from ROBLOX, with this error:

Scheduled Reconnection: Error: write ECONNRESET onMessage Error: Error: Lost connection, reconnecting Error Message: Can't abort connection Exception: { Error: read ECONNRESET at exports._errnoException (util.js:1050:11) at TLSWrap.onread (net.js:582:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' } Error Data: { hostname: 'realtime.roblox.com', port: null, method: 'POST', path: '/notifications/abort?clientProtocol=1.5&transport=serverSentEvents&connectionData=%5B%7B%22name%22%3A%22usernotificationhub%22%7D%5D&connectionToken=1ZY5KTtyhDsJoHiT4dbsp2h21%2FJQcFp6h73TNh9XUFrhEKRjSHz3VgsH%2BlvwWC0hxOpGL8kAYsFHgl5hWBn6FTNoqPOdBcaF%2FQqEKsLVSQ2ekycd' } onMessage Error: HTTP Negotiate Error

sentanos commented 7 years ago

This is not a direct problem with reconnection, it is some problem with the ROBLOX site or your network. It could have been that the site went down for some period or the connection simply failed for some reason.

Make sure you have something setup to log your bot in regularly, otherwise the cookie could expire. Apart from that you'll have to add your own logic to reconnect after connection errors, as that usually suggests a problem unrelated to simply reconnection.

On May 25, 2017, at 09:23, Projado notifications@github.com wrote:

Seems as if there's still an error. It's still appearing to disconnect from ROBLOX, with this error:

Scheduled Reconnection: Error: write ECONNRESET onMessage Error: Error: Lost connection, reconnecting Error Message: Can't abort connection Exception: { Error: read ECONNRESET at exports._errnoException (util.js:1050:11) at TLSWrap.onread (net.js:582:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' } Error Data: { hostname: 'realtime.roblox.com', port: null, method: 'POST', path: '/notifications/abort?clientProtocol=1.5&transport=serverSentEvents&connectionData=%5B%7B%22name%22%3A%22usernotificationhub%22%7D%5D&connectionToken=1ZY5KTtyhDsJoHiT4dbsp2h21%2FJQcFp6h73TNh9XUFrhEKRjSHz3VgsH%2BlvwWC0hxOpGL8kAYsFHgl5hWBn6FTNoqPOdBcaF%2FQqEKsLVSQ2ekycd' } onMessage Error: HTTP Negotiate Error

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.