My client can't receive connnection lost event
Posted: 08 May 2014, 05:50
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
I use Java API 1.3.2
Can you help me ? Thanks!
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();
}
}
Can you help me ? Thanks!