var config = {
host: "1.1.1.1",
port: 8888,
userSSL: false,
zone: "ZoneName",
debug: true
};
var loginInfo = {
username: "123456",
password: "",
param: {
param_1: "",
param_2: "",
param_3: ""
}
};
var countInterval = null;
var hasEvent_Connection = false;
var hasEvent_ConnectionLost = false;
/** Connect **/
var createConnect = function(instance){
console.log("Creating a connection to smf server");
if(hasEvent_Connection === false){
instance.addEventListener(SFS2X.SFSEvent.CONNECTION, onConnection, this);
hasEvent_Connection = true;
}
if(hasEvent_ConnectionLost === false){
instance.addEventListener(SFS2X.SFSEvent.CONNECTION_LOST, onConnectionLost, this);
hasEvent_ConnectionLost = true;
}
instance.connect();
};
var onConnection = function(){
console.log("Connected!");
//Login
createLogin(instance);
//countTime(1);
};
var onConnectionLost = function(){
console.log("Connection lost");
//countTime(0);
createConnect(instance);
};
/** Login **/
var createLogin = function(instace){
console.log("Login...");
instace.addEventListener(SFS2X.SFSEvent.LOGIN, loginSuccess, this);
instace.addEventListener(SFS2X.SFSEvent.LOGIN_ERROR, loginUnsuccess, this);
instace.addEventListener(SFS2X.SFSEvent.CONNECTION_LOST, onConnectionLost, this);
instace.addEventListener(SFS2X.SFSEvent.LOGOUT, logout, this);
//send login informations
return instace.send(new SFS2X.Requests.System.LoginRequest(loginInfo.username, loginInfo.password, loginInfo.param, config.zone));
};
var loginSuccess = function(evtParams){
console.log("Login success!");
};
var loginUnsuccess = function(evtParams){
console.log("Login unsuccess, error: "+evtParams.errMessage);
};
var logout = function(evtParams){
console.log("Logout: "+evtParams);
};
var countTime = function(state){
if(state){
time = 0;
countInterval = setInterval(function(){
console.log("Time-on: "+time++);
}, 1000);
} else{
clearInterval(countInterval);
}
};
//Runnnnn
var instance = new SmartFox(config);
if(instance._inited){
//Connect
createConnect(instance);
}
Comments