Page 1 of 1

User Disconnect then Reconnect Error

Posted: 26 Oct 2011, 18:53
by Iszard
Hey SFS2X Team,

I have a AS3 client side I wish to allow a user to be able to disconnect and also reconnect from. However I am receiving an Error #2032: Stream Error. URL: http://127.0.0.1:0/BlueBox/BlueBox.do

As you can see this is a local server.

Steps:

1) Client Auto Loads sfs-config_http.xml
2) User can then manually connect and when they do so it connects
3) On Connection client side auto logs in for the user giving the user a guest account.
3) User can then manually disconnect and does so. "SmartFox.disconnect"
4) User can then manually trigger the same connect trigger. "SmartFox.connect()" Client side auto fills in the HOST and PORT from the SmartFox.config.host and SmartFox.config.port.

Then I get the before mentioned error.

Is this forum of re-connection not allowed or is there a delay preset to not allow connection spam to the server? If so is there a way to config that or for the client side to receive that info so I can set up a timer to allow the re-connection after the delay has passed?

Posted: 27 Oct 2011, 06:35
by Lapo
If you disconnect then recreate the SmartFox object before proceeding with a new connection.
This is not necessary if you just log-out because in that case the socket remains open waiting for a new connection to a different Zone.

So...

Posted: 27 Oct 2011, 19:07
by Iszard
So what your saying is if I want to be able to have the user disconnect themselves and say return at a later time with out having to relaunch the client side is that I need to set up a new SmartFox object during each connection scenario?

The user already has the ability to log-out and back in in my client side. However being that it is a guest log-in so they always log-in and get new user id's anyways.

I just wouldn't want to maintain a connection to the server if there is no need and I thought that having to reload the config file was a little redundant but I am guessing that is just how it is?

Posted: 27 Oct 2011, 20:20
by Lapo
Yes simply create a new SmartFox object, as simple as that :)

Re: User Disconnect then Reconnect Error

Posted: 25 Sep 2012, 18:03
by JimmiQR
I found this thread with the exact same concern, but wouldn't creating a new SmartFox object render all listeners attached to it orphaned?

If I disconnect(), or reboot/cycle the server, I would like all the clients to reconnect to the server again, without action from the user. I have a singleton class wrapper for my SmartFox instance, that I can then add listeners to anywhere in my project classes. I just go Server.getInstance(). So I can interact with my SmartFox object from anywhere.

Code: Select all

Server.getInstance().send(...
Server.getInstance().connect();
//etc
but I also have listeners on that instance as well.

Code: Select all

Server.getInstance().addEventListener(SFSEvent.EXTENSION_RESPONSE, extensionResponseHandler);
Server.getInstance().addEventListener(SFSEvent.ROOM_VARIABLES_UPDATE, roomVariablesUpdateHandler);
//etc
So as far as I can tell, my singleton structure doesn't allow me to create a new SFS object, which in turn doesn't allow me to re-connect to the server with out an entire page refresh.

Unless I am missing something?? Is there a programming struct I am not aware of to maintain these listeners?
Can't I just re-request the security policy file on this existing instance? Just go through the connection flow again (get config, policy file, connect, handshake, login, join zone, join room)?

Re: User Disconnect then Reconnect Error

Posted: 25 Sep 2012, 18:15
by Lapo
JimmiQR wrote:I found this thread with the exact same concern, but wouldn't creating a new SmartFox object render all listeners attached to it orphaned?
Yes, that's correct.
If you organize your code correctly by separating the creation of the SmartFox instance from the setup of the listeners you just need one line of code to recreate the instance and then you call the method that sets up your new listeners.
If I disconnect(), or reboot/cycle the server, I would like all the clients to reconnect to the server again, without action from the user. I have a singleton class wrapper for my SmartFox instance, that I can then add listeners to anywhere in my project classes. I just go Server.getInstance(). So I can interact with my SmartFox object from anywhere.
The problem is: you cannot know from client side if the server was rebooted, crashed, or your local network dropped or something in between kicked you out. If you just detect a disconnection and attempt to reconnect again you will end up with clients that never go away :)
So as far as I can tell, my singleton structure doesn't allow me to create a new SFS object, which in turn doesn't allow me to re-connect to the server with out an entire page refresh.
Since AS3 is not multithreaded you can safely invoke a reset() method on the singleton which internally creates a new instance.

Re: User Disconnect then Reconnect Error

Posted: 25 Sep 2012, 19:42
by JimmiQR
Ok, I will begin to go with this method of recreating a new SFS object upon a new connection in my Singleton. But I am still confused as to how this will be as easy as one line of code in my Singleton to set up my new listeners on this new object. Do one of the SmartFox sample codes that come with the setup utilize this technique?

You see, my singleton doesn't attach any listeners, but rather I have a bunch of classes that attach listeners. A chat panel, a game panel, a player panel, etc. And more to come. They listen for extensionResponses, and public and private message responses, and room updates. How would my singleton go about telling all these classes they need to re-attach their listeners? Or do I need a different way to attach them, using some overridden structure...

I know this is now beyond the scope of SmartFox help, since I am really asking about a coding structure now, but I am trying to avoid going down the road of adding hoop-jumping code to all my classes when what I really want is just the Server.getInstance().connect() to just work a second time. I realize there may be reasons such as complexity or security as to why this is the case, so I won't harp on that. But what is the clean way of having all my classes know that they must now reattach all of their listeners to this new Singleton object? If there really is some sample laying around, I would gladly dig through that.

Re: User Disconnect then Reconnect Error

Posted: 25 Sep 2012, 19:49
by rjgtav
Hi.

A quick and dirty way of doing this is to listen to all SmartFox's Events that you use in your singleton, handle those events in the same function, and then dispatch them again. This way, on the rest of your application, you simply need to add the listeners to the singleton and not to the SmartFox instance.

Re: User Disconnect then Reconnect Error

Posted: 25 Sep 2012, 21:03
by Lapo
You see, my singleton doesn't attach any listeners, but rather I have a bunch of classes that attach listeners. A chat panel, a game panel, a player panel, etc. And more to come. They listen for extensionResponses, and public and private message responses, and room updates. How would my singleton go about telling all these classes they need to re-attach their listeners?
I could reply with another question. How would you go about telling all those components to clear their current state because the connection was closed and we need to start over? I guess the same mechanism applies for re-attaching the listeners.

Re: User Disconnect then Reconnect Error

Posted: 26 Sep 2012, 19:29
by JimmiQR
Hey Lapo, Thanks for the responses.

Yes, some of my components may need to know to refresh their state, but not all of them. For instance, I wouldn't want my chat window to refresh, erasing the chat log just because of a re-connection, so no need to even tell it to redraw. I am either going to turn off BlueBox for now, or use rjgtav's quick and dirty method of first handling SFS events internal to my singleton all of the events my project uses, and having the singleton re-dipatch them, which allows me to add listeners to my singleton static class, and not my Singleton.getInstance. But this is a sloppy approach, as I have to remember to include any new events inside my singleton as well.

But that aside, I have been testing with this, and I have to say I feel it is an actual bug somewhere in com.smartfoxserver.v2.SmartFox. I made a simple interface to CONNECT, DISCONNECT, TOGGLE BLUEBLOX, and CREATE NEW SMARTFOX INSTANCE (supplied in code below). The bug can be replicated by making an attempt to connect to a non-reachable server with the blue box. Once it fails with the blue box, it will never succeed with socket or bluebox ever again. BUT, if you never attempt+and+fail a blue box connection, you can always connect to the server using the connect() whenever the server is back up. It is like a state machine, that once it gets to a failed Bluebox attempt, it can never get out of that broken state. No way to tell it to reset. It appears to cerate a session, but never send the handshake or login. You suggested we just new the SmartFox instance, but couldn't this bug either be tracked down, or add a reset() function to SmartFox allowing it to clean itself up internally?

This state machine will continue to work successfully.
  1. No server running
  2. run client
  3. click BlueBox toggle, set it to false
  4. click Connect, wait a few seconds for it to create session, but never log in and fail socket connection
  5. run server
  6. click Connect, SUCCESS and Login!
  7. close server
  8. goto and repeat at step 4
REPLICATE BUG using code below. State machine gets stuck in broken state.
  1. No server running
  2. run client
  3. click BlueBox toggle, set it to true
  4. click Connect, wait a few seconds for it to create session, but never log in and fail socket connection, as well as fail http blue box conection
  5. run server
  6. click Connect, FAIL.
  7. nothing seems to get the client out of this dead end state such as trying to set useBlueBox back to false, hitting disconnect to clean up, etc. The only thing short of a browser refresh is clicking on New Instance, which I am trying to avoid for 'listener' reasons
I hope this helps in classifying this as a bug, and could supply any additional info if you would like, but I did try to keep the bug replication code to a minimum.
  • AS3 client 1.1.1
    Flash players 11,2,202,235, 11.3.31.232, 11,4,402,265, as well as debug
    swf published for 9.0, 10.0, 10.3, 11.0, and 11.1; same results
    Chrome version 21, (but it may be even higher in the next couple of minutes)

Code: Select all

package {
	import com.smartfoxserver.v2.core.SFSEvent;
	import com.smartfoxserver.v2.requests.LoginRequest;
	import com.smartfoxserver.v2.SmartFox;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	

	public class Main extends Sprite {
		
		
		private var _smartFox:SmartFox;
		private var _toggleBlueBoxTextField:TextField = new TextField();
		
		
		public function Main():void {
			init();
			draw();
		}
		
		
		private function init():void {
			_smartFox = new SmartFox();
			_smartFox.addEventListener(SFSEvent.CONNECTION, connectionHandler);
			_smartFox.addEventListener(SFSEvent.CONFIG_LOAD_SUCCESS, configLoadSuccessHandler);
			_smartFox.loadConfig("sfs-config", false);
		}

		
		private function draw():void {
			var connectTextField:TextField = new TextField();
			connectTextField.addEventListener(MouseEvent.MOUSE_DOWN, connectTextFieldHandler);
			connectTextField.text  = "[connect]";
			addChild(connectTextField);

			var disconnectTextField:TextField = new TextField();
			disconnectTextField.addEventListener(MouseEvent.MOUSE_DOWN, disconnectTextFieldHandler);
			disconnectTextField.text  = "[disconnect]";
			addChild(disconnectTextField);
			disconnectTextField.y = 20;

			addChild(_toggleBlueBoxTextField);
			_toggleBlueBoxTextField.y = 60;
			_toggleBlueBoxTextField.text = "BlueBox (config xml not loaded yet...]"
			_toggleBlueBoxTextField.addEventListener(MouseEvent.MOUSE_DOWN, toggleBlueBoxHandler);

			var newInstanceTextField:TextField = new TextField();
			newInstanceTextField.addEventListener(MouseEvent.MOUSE_DOWN, newInstanceTextFieldHandler);
			newInstanceTextField.text  = "[new instance]";
			addChild(newInstanceTextField);
			newInstanceTextField.y = 100;
		}
		
		
		private function configLoadSuccessHandler(event:SFSEvent):void {
			trace("config loaded");
			_toggleBlueBoxTextField.text = "[BlueBox is " + _smartFox.useBlueBox + "]";
		}
		
		
		private function connectTextFieldHandler(mouseEvent:MouseEvent):void {
			trace("attempting connect...");
			_smartFox.connect("127.0.0.1", 9933);
		}

		
		private function disconnectTextFieldHandler(mouseEvent:MouseEvent):void {
			trace("attempting disconnect...");
			_smartFox.disconnect();
		}
		
		
		private function toggleBlueBoxHandler(mouseEvent:MouseEvent):void {
			_smartFox.useBlueBox = !_smartFox.useBlueBox;
			TextField(mouseEvent.target).text = "[BlueBox is " + _smartFox.useBlueBox + "]";
		}
		
		
		private function connectionHandler(sfsEvent:SFSEvent):void {
			if (sfsEvent.params.success == true) {
				trace("  connected");
				_smartFox.send(new LoginRequest());
			}
			else {
				trace("  disconnected");
			}
		}
		
		
		private function newInstanceTextFieldHandler(mouseEvent:MouseEvent):void {
			init();
		}
		
		
	}
	
}
Similar bug being reported can be found here:
Socket Error -> kill client for ever http://forums.smartfoxserver.com/viewtopic.php?t=13726
bug on reconnection http://forums.smartfoxserver.com/viewtopic.php?t=10205

Re: User Disconnect then Reconnect Error

Posted: 27 Sep 2012, 07:40
by Lapo
Hi,
thanks for the long report. The client API are designed so that each instance is used for one session and then discarded, so it's not possible to use it the way you ask.

You can use weak references in your listeners to avoid removing them manually and when you need to restart the application you can re-run the method that attaches the listeners to your components.

Thanks