quocvuongdn

#javascript: Init, connect, login SmartFox server example

Dec 17th, 2014
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var config = {
  2.     host: "1.1.1.1",
  3.     port: 8888,
  4.     userSSL: false,
  5.     zone: "ZoneName",
  6.     debug: true
  7. };
  8.  
  9. var loginInfo = {
  10.     username: "123456",
  11.     password: "",
  12.     param: {
  13.         param_1: "",
  14.         param_2: "",
  15.         param_3: ""
  16.     }
  17. };
  18.  
  19. var countInterval = null;
  20. var hasEvent_Connection = false;
  21. var hasEvent_ConnectionLost = false;
  22.  
  23. /** Connect **/
  24. var createConnect = function(instance){
  25.     console.log("Creating a connection to smf server");
  26.     if(hasEvent_Connection === false){
  27.         instance.addEventListener(SFS2X.SFSEvent.CONNECTION, onConnection, this);
  28.         hasEvent_Connection = true;
  29.     }
  30.     if(hasEvent_ConnectionLost === false){
  31.         instance.addEventListener(SFS2X.SFSEvent.CONNECTION_LOST, onConnectionLost, this);
  32.         hasEvent_ConnectionLost = true;
  33.     }
  34.     instance.connect();
  35. };
  36.  
  37. var onConnection = function(){
  38.     console.log("Connected!");
  39.     //Login
  40.     createLogin(instance);
  41.     //countTime(1);
  42. };
  43.  
  44. var onConnectionLost = function(){
  45.     console.log("Connection lost");
  46.     //countTime(0);
  47.     createConnect(instance);
  48. };
  49.  
  50. /** Login **/
  51. var createLogin = function(instace){
  52.     console.log("Login...");
  53.     instace.addEventListener(SFS2X.SFSEvent.LOGIN, loginSuccess, this);
  54.     instace.addEventListener(SFS2X.SFSEvent.LOGIN_ERROR, loginUnsuccess, this);
  55.     instace.addEventListener(SFS2X.SFSEvent.CONNECTION_LOST, onConnectionLost, this);
  56.     instace.addEventListener(SFS2X.SFSEvent.LOGOUT, logout, this);
  57.     //send login informations
  58.     return instace.send(new SFS2X.Requests.System.LoginRequest(loginInfo.username, loginInfo.password, loginInfo.param, config.zone));
  59. };
  60.  
  61. var loginSuccess = function(evtParams){
  62.     console.log("Login success!");
  63. };
  64.  
  65. var loginUnsuccess = function(evtParams){
  66.     console.log("Login unsuccess, error: "+evtParams.errMessage);
  67. };
  68.  
  69. var logout = function(evtParams){
  70.     console.log("Logout: "+evtParams);
  71. };
  72.  
  73. var countTime = function(state){
  74.     if(state){
  75.         time = 0;
  76.         countInterval = setInterval(function(){
  77.             console.log("Time-on: "+time++);
  78.         }, 1000);
  79.     } else{
  80.         clearInterval(countInterval);
  81.     }
  82. };
  83.  
  84.  
  85.  
  86. //Runnnnn
  87. var instance = new SmartFox(config);
  88. if(instance._inited){
  89.     //Connect
  90.     createConnect(instance);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment