My client can't receive connnection lost event

Post here your questions about the Java client / Android API for SFS2X

Moderators: Lapo, Bax

Post Reply
hhd90
Posts: 67
Joined: 21 Dec 2012, 05:17

My client can't receive connnection lost event

Post by hhd90 »

Hi smartfoxTeam,
I write simple client which connect and try to reconnect when connection lost, but my client just receive this event at the first time server shutdown, from the second time it does not receive connection lost event any more.
Here is my code

Code: Select all

package main;

import java.util.LinkedList;
import java.util.List;

import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.exceptions.SFSException;

import sfs2x.client.SmartFox;
import sfs2x.client.core.BaseEvent;
import sfs2x.client.core.IEventListener;
import sfs2x.client.core.SFSEvent;
import sfs2x.client.requests.ExtensionRequest;
import sfs2x.client.requests.IRequest;
import sfs2x.client.requests.LoginRequest;

public class SmartfoxConnector implements IEventListener, Runnable {
	private String host = "localhost";
	private int port = 9940;
	private SmartFox smartFox;
	// private List<SmsInfo> listSmsInfo;
	private boolean running;
	private boolean connect;
	private boolean isWaitResponse;

	public SmartfoxConnector(String host, int port) {
		this.host = host;
		this.port = port;
		// this.listSmsInfo = new LinkedList<SmsInfo>();
		// init();
	}

	public void start() {
		this.running = true;
		Thread thread = new Thread(this);
		thread.start();
	}

	public boolean isConnect() {
		return connect;
	}

	public void init() {

		smartFox = new SmartFox();
		smartFox.addEventListener(SFSEvent.CONNECTION, this);
		smartFox.addEventListener(SFSEvent.LOGIN, this);
		smartFox.addEventListener(SFSEvent.LOGIN_ERROR, this);
		smartFox.addEventListener(SFSEvent.CONNECTION_LOST, this);
		connect();
	}

	private void connect() {
		isWaitResponse = true;
		smartFox.connect(host, port);
	}

	public void send(String command, ISFSObject resObj) {
		try {
			IRequest request = new ExtensionRequest(command, resObj);
			smartFox.send(request);
			isWaitResponse = true;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		// Debug.d("host:" + host);
		// Debug.d("is connnect:" + smartFox.isConnected());
		// Debug.d("send request :" + command);
		// Debug.d("data :" + resObj.getDump());
	}

	@Override
	public void dispatch(BaseEvent event) throws SFSException {
		isWaitResponse = false;
		if (event.getType().equalsIgnoreCase(SFSEvent.CONNECTION)) {
			onConnection(event);
		} else if (event.getType().equalsIgnoreCase(SFSEvent.LOGIN)) {
			onLogin(event);
		} else if (event.getType().equalsIgnoreCase(SFSEvent.LOGIN_ERROR)) {
			onLoginError(event);
		} else if (event.getType().equalsIgnoreCase(SFSEvent.CONNECTION_LOST)) {
			// Debug.d("lost connect to smartfox");
			onConnectionLost(event);
		}
	}

	private void onConnectionLost(BaseEvent event) {
		Debug.d("lost connect to smartfox");
		connect = false;
	}

	private void onConnection(BaseEvent event) {
		isWaitResponse = false;
		boolean success = (Boolean) event.getArguments().get("success");
		if (success) {
			LoginRequest loginRequest = new LoginRequest("admin", null,
					"CardGameZone");
			smartFox.send(loginRequest);
			connect = true;
			Debug.d("connect to smartfox success");
		} else {
			connect = false;
			Debug.d("connect to smartfox failed");
		}
	}

	private void onLogin(BaseEvent event) {
		// boolean success = (Boolean) event.getArguments().get("success");
		Debug.d("login success");
	}

	private void onLoginError(BaseEvent event) {
		// boolean success = (Boolean) event.getArguments().get("success");
		Debug.d("login error:" + event.toString());

	}

	// public void addList(SmsInfo smsInfo) {
	// listSmsInfo.add(smsInfo);
	// }

	@Override
	public void run() {
		while (running) {
			if (!connect && !isWaitResponse) {
				Debug.d("try reconnect...");
				init();
			}

			// if (!listSmsInfo.isEmpty() && connect) {
			// SmsInfo smsInfo = listSmsInfo.remove(0);
			// send(smsInfo.getCommand(), smsInfo.getInfo());
			// }

			try {
				Thread.sleep(30);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	public boolean isRunning() {
		return running;
	}

	public static void main(String[] args) {
		SmartfoxConnector smartfoxConnector = new SmartfoxConnector(
				"192.168.1.253", 9933);
		smartfoxConnector.start();
	}

}

I use Java API 1.3.2
Can you help me ? Thanks!
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: My client can't receive connnection lost event

Post by Lapo »

HI,
please explain each step of the test you are doing and tell us exactly what is happening in terms of errors/exceptions from both the server and client side.

Also please specify the server version.

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
hhd90
Posts: 67
Joined: 21 Dec 2012, 05:17

Re: My client can't receive connnection lost event

Post by hhd90 »

My test :
- start server
- open client connect to server ( I posted client code above)
- shut down server immediately
- client receive disconnect event and start try to reconnect...
- open server again and client reconnect successfully
- shut down server again, this time client does not receive disconnect event any more.Therefore client does not know to start trying reconnect.
Server version : 2.8.2
Client and Server don't have any exception.
Post Reply