Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to implement onDisconnect method? #4

Closed
yong2khoo88 opened this issue Nov 13, 2016 · 21 comments
Closed

Is it possible to implement onDisconnect method? #4

yong2khoo88 opened this issue Nov 13, 2016 · 21 comments

Comments

@yong2khoo88
Copy link

In Javascript, it is possible to pre-define the function/codes to be executed on the server side through the onDisconnect event. (such as remove its content)

Is it possible to do so in AS3?

Thanks in advance.

@agentphantom
Copy link
Member

Hi,

You can implement this on several ways. For example in AIR mobile I detect when the user leaves the chat screen, when that happens I close and dispose the URLStream.

You only need to do something like:

myURLStream.removeEventListener(ProgressEvent.PROGRESS, myFunction);

myFunction is the same function that receives the messages.

You can see a full implementation in the following file:

https://github.com/PhantomAppDevelopment/pizza-app/blob/master/src/chatScreens/ChatScreen.as#L239

In the previous example on the dispose() function I can do anything I want, such as cleaning up objects or sending a request to the server.

@yong2khoo88
Copy link
Author

yong2khoo88 commented Nov 13, 2016

Hi agentphantom, sorry i don't quite get it. The onDisconnect is "used to manage operations that will be run on the server when the clients disconnect". Here, what i mean is when the user suddenly loses the internet connection. The onDisconnect is used to 'pre-define' what actions to be executed on the database (such as removes the user ID) when the server detects that the user has been disconnected.

When you mention 'dispose()' function, how does it 'send a request to the server' when it is already disconnected (say he loses internet connection)?
Or, How can other users tell when one of the users has lost the internet connection?

In javascript, the code is something like this:

var userRef = new Firebase('https://<demo>.firebaseio.com/presence/' + userid);
amOnline.on('value', function(snapshot) {
  if (snapshot.val()) {
    userRef.onDisconnect().remove(); //this line is able to remove his own node as soon as he is disconnected
    userRef.set(true);
  }
});

@agentphantom
Copy link
Member

Hi,

Now I fully understand what you mean. Right now the REST API and by extension the AS3 API officially don't support this feature (unless you use an ANE).

I took a look at the JavaScript SDK and found out that the onDisconnect function sends a small request to the server with specific strings "cancel", "remove", "set".

Problem is, the JavaScript SDK is minified, and that's just an educated guess from what I could understand.

Maybe it can be ported for AS3, but I don't know enough JavaScript to debug it. If you wish you can give it a try with JavaScript, debug all the requests that are sent, paste them here and then I can port it to AS3.

@yong2khoo88
Copy link
Author

Thanks for your reply. To be honest, i have no idea how to debug this. The network tab on the browser shows nothing even though i debug through the codes line by line. Anyway, i will continue looking into this.

@yong2khoo88
Copy link
Author

I posted the question in stackoverflow. Meanwhile, i found an un-minified version of the firebase at here. Still in the attempts of understanding the magic behind...

@agentphantom
Copy link
Member

Thanks!

I read the question and they have the right idea. Monitoring the outgoing traffic will do the trick.
I believe that a network analyzer like Wireshark can do the job.

Meanwhile I will keep reading the JS file to see if I can find the answer there.

@agentphantom
Copy link
Member

This block looks interesting:

onDisconnectPut:function(pathString, data, opt_onComplete) { if (this.connected_) { this.sendOnDisconnect_("o", pathString, data, opt_onComplete); } else { this.onDisconnectRequestQueue_.push({pathString:pathString, action:"o", data:data, onComplete:opt_onComplete}); } }, onDisconnectMerge:function(pathString, data, opt_onComplete) { if (this.connected_) { this.sendOnDisconnect_("om", pathString, data, opt_onComplete); } else { this.onDisconnectRequestQueue_.push({pathString:pathString, action:"om", data:data, onComplete:opt_onComplete}); } }, onDisconnectCancel:function(pathString, opt_onComplete) { if (this.connected_) { this.sendOnDisconnect_("oc", pathString, null, opt_onComplete); } else { this.onDisconnectRequestQueue_.push({pathString:pathString, action:"oc", data:null, onComplete:opt_onComplete}); }

@yong2khoo88
Copy link
Author

I finally saw something like this at the network tab:
fb

it seems like web socket or sth.

And when i click on that, it shows:
fb2

(the 'ns' is my firebase namespace).

I am not sure if the normal REST API through urlrequest is able to achieve this, as mentioned by one guy in the stackoverflow....

@agentphantom
Copy link
Member

Thanks for sharing those pictures!

Maybe the 'v' parameter does something special.

URLStream behaves almost the same as a websocket, the difference is that all the headers and responses are abstracted so you have an easier time sending and receiving them.

Disregard the commend about URLRequest, that guy doesn't know what he's talking about. URLRequest just describes what you want to do, it doesn't do any net connection by itself.

My recommendation is to find a way to read the outgoing traffic, there lies the answer.

@yong2khoo88
Copy link
Author

Thank you for your reply. Allow me to spend more time on this before making a conclusion.

@yong2khoo88
Copy link
Author

wireshark

Sorry for the long delay. Finally i am trying to use wireshark to capture something... However, i don't i think i understand what is being captured, as attached.

The only recognize-able letters are .....firebaseio.com.....
Did i miss out something in the wireshark?

@agentphantom
Copy link
Member

Hi,

My bad! I did some research and Wireshark is to analyze traffic after it has been processed, that's why the output is encrypted.

I recommend to use Fiddler: http://www.telerik.com/fiddler

This one analyzes the HTTP and HTTPS requests before they are sent (similar to the ones you added here some days ago), but it says its more complete.

If you wish you can give it another go with Fiddler and report back your findings.

@yong2khoo88
Copy link
Author

Hi,
I managed to install and try the Fiddler. But I have no idea which part of the tabs show the 'data transferred' over the network. For example, if i perform login to facebook, i expect my username and password to be captured through the fiddler right? But i see nothing meaningful... Correct me if i am wrong.

@yong2khoo88
Copy link
Author

Hi Phantom,
I think i saw something. But before anything, could you please tell me how to connect to 'websocket' in AS3?

Say the end point is sth like:
..wss://s-usc1c-nss-129.firebaseio.com/.ws?...
i don't think URLLoader or URLRequest would work.

@agentphantom
Copy link
Member

HI,

I haven't tried this but it looks very complete and useful:
https://github.com/theturtle32/AS3WebSocket

Seeing at the examples it is very similar to setup like an URLStream.

@yong2khoo88
Copy link
Author

Thanks for you reply. i actually tried that. but it throws some errors:
Definition com.adobe.net.URI could not be found.
Definition com.adobe.net.URIEncodingBitmap could not be found.
Definition com.adobe.utils.StringUtil could not be found.

Anyway, this is what i found. i use websocket plugin from firefox instead, that enables me to view what has been sent to the firebase
ws
However, that seems to be sent over the websocket, but not throught database REST API?

@yong2khoo88
Copy link
Author

My Bad, i didn't include the as3corlib. Now seems i am able to 'connect'. I will further post after more testing.

@agentphantom
Copy link
Member

Ohh, interesting.

It sends a nested JSON with different parameters:

t (type maybe?) = "d" (probably refers to disconnect
d (a data object containing the payload)
r (no idea) = 4 (maybe it is a priority of some sorts)
a (action maybe) = "o" could be linked to a specific task such as on disconnected, I remember there are several onDisconnect variations.
b (a nested object containing the data, no clue what b could stand for)
p (path) = /allUser/3
d (data) = "Hey I'm disconnect"

With this new finding I can take another look at the unminified library.

I will update this comment if I find something new.

Thanks!

@yong2khoo88
Copy link
Author

yong2khoo88 commented Dec 13, 2016

Hi Phantom,

I think you are almost right (with those parameters). I have tested with the local machine and it seems working. The conclusion is, it is feasible. (a bit of trouble but still it works) I have to thank you for your insights provided. I can rest in peace now. lol

[you may close this post :)]

@agentphantom
Copy link
Member

That's great!

I will experiment with it and add it to the guide in the coming weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants