mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-21 01:37:50 -05:00
Brought up to date with the private repository (feea4c7)
This commit is contained in:
1 parent
1aa738d692
commit
1b1df1ae48
7925 files changed
+2617237
-20
No files matched your search
@@ -0,0 +1,95 @@
|
||||
package it.gotoandplay.smartfoxserver
|
||||
{
|
||||
import flash.events.Event;
|
||||
|
||||
public class SFSEvent extends Event
|
||||
{
|
||||
|
||||
public static const onAdminMessage:String = "onAdminMessage";
|
||||
|
||||
public static const onBuddyList:String = "onBuddyList";
|
||||
|
||||
public static const onBuddyListError:String = "onBuddyListError";
|
||||
|
||||
public static const onBuddyListUpdate:String = "onBuddyListUpdate";
|
||||
|
||||
public static const onBuddyPermissionRequest:String = "onBuddyPermissionRequest";
|
||||
|
||||
public static const onBuddyRoom:String = "onBuddyRoom";
|
||||
|
||||
public static const onConfigLoadFailure:String = "onConfigLoadFailure";
|
||||
|
||||
public static const onConfigLoadSuccess:String = "onConfigLoadSuccess";
|
||||
|
||||
public static const onConnection:String = "onConnection";
|
||||
|
||||
public static const onConnectionLost:String = "onConnectionLost";
|
||||
|
||||
public static const onCreateRoomError:String = "onCreateRoomError";
|
||||
|
||||
public static const onDebugMessage:String = "onDebugMessage";
|
||||
|
||||
public static const onExtensionResponse:String = "onExtensionResponse";
|
||||
|
||||
public static const onJoinRoom:String = "onJoinRoom";
|
||||
|
||||
public static const onJoinRoomError:String = "onJoinRoomError";
|
||||
|
||||
public static const onLogin:String = "onLogin";
|
||||
|
||||
public static const onLogout:String = "onLogout";
|
||||
|
||||
public static const onModeratorMessage:String = "onModMessage";
|
||||
|
||||
public static const onObjectReceived:String = "onObjectReceived";
|
||||
|
||||
public static const onPrivateMessage:String = "onPrivateMessage";
|
||||
|
||||
public static const onPublicMessage:String = "onPublicMessage";
|
||||
|
||||
public static const onRandomKey:String = "onRandomKey";
|
||||
|
||||
public static const onRoomAdded:String = "onRoomAdded";
|
||||
|
||||
public static const onRoomDeleted:String = "onRoomDeleted";
|
||||
|
||||
public static const onRoomLeft:String = "onRoomLeft";
|
||||
|
||||
public static const onRoomListUpdate:String = "onRoomListUpdate";
|
||||
|
||||
public static const onRoomVariablesUpdate:String = "onRoomVariablesUpdate";
|
||||
|
||||
public static const onRoundTripResponse:String = "onRoundTripResponse";
|
||||
|
||||
public static const onSpectatorSwitched:String = "onSpectatorSwitched";
|
||||
|
||||
public static const onPlayerSwitched:String = "onPlayerSwitched";
|
||||
|
||||
public static const onUserCountChange:String = "onUserCountChange";
|
||||
|
||||
public static const onUserEnterRoom:String = "onUserEnterRoom";
|
||||
|
||||
public static const onUserLeaveRoom:String = "onUserLeaveRoom";
|
||||
|
||||
public static const onUserVariablesUpdate:String = "onUserVariablesUpdate";
|
||||
|
||||
public var params:Object;
|
||||
|
||||
public function SFSEvent(type:String, params:Object)
|
||||
{
|
||||
super(type);
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
override public function clone() : Event
|
||||
{
|
||||
return new SFSEvent(this.type,this.params);
|
||||
}
|
||||
|
||||
override public function toString() : String
|
||||
{
|
||||
return formatToString("SFSEvent","type","bubbles","cancelable","eventPhase","params");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1348
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,213 @@
|
||||
package it.gotoandplay.smartfoxserver.data
|
||||
{
|
||||
public class Room
|
||||
{
|
||||
|
||||
private var id:int;
|
||||
|
||||
private var name:String;
|
||||
|
||||
private var maxUsers:int;
|
||||
|
||||
private var maxSpectators:int;
|
||||
|
||||
private var temp:Boolean;
|
||||
|
||||
private var game:Boolean;
|
||||
|
||||
private var priv:Boolean;
|
||||
|
||||
private var limbo:Boolean;
|
||||
|
||||
private var userCount:int;
|
||||
|
||||
private var specCount:int;
|
||||
|
||||
private var myPlayerIndex:int;
|
||||
|
||||
private var userList:Array;
|
||||
|
||||
private var variables:Array;
|
||||
|
||||
public function Room(id:int, name:String, maxUsers:int, maxSpectators:int, isTemp:Boolean, isGame:Boolean, isPrivate:Boolean, isLimbo:Boolean, userCount:int = 0, specCount:int = 0)
|
||||
{
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.maxSpectators = maxSpectators;
|
||||
this.maxUsers = maxUsers;
|
||||
this.temp = isTemp;
|
||||
this.game = isGame;
|
||||
this.priv = isPrivate;
|
||||
this.limbo = isLimbo;
|
||||
this.userCount = userCount;
|
||||
this.specCount = specCount;
|
||||
this.userList = [];
|
||||
this.variables = [];
|
||||
}
|
||||
|
||||
public function addUser(u:User, id:int) : void
|
||||
{
|
||||
this.userList[id] = u;
|
||||
if(this.game && u.isSpectator())
|
||||
{
|
||||
++this.specCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
++this.userCount;
|
||||
}
|
||||
}
|
||||
|
||||
public function removeUser(id:int) : void
|
||||
{
|
||||
var u:User = this.userList[id];
|
||||
if(this.game && u.isSpectator())
|
||||
{
|
||||
--this.specCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
--this.userCount;
|
||||
}
|
||||
delete this.userList[id];
|
||||
}
|
||||
|
||||
public function getUserList() : Array
|
||||
{
|
||||
return this.userList;
|
||||
}
|
||||
|
||||
public function getUser(userId:*) : User
|
||||
{
|
||||
var i:String = null;
|
||||
var u:User = null;
|
||||
var user:User = null;
|
||||
if(typeof userId == "number")
|
||||
{
|
||||
user = this.userList[userId];
|
||||
}
|
||||
else if(typeof userId == "string")
|
||||
{
|
||||
for(i in this.userList)
|
||||
{
|
||||
u = this.userList[i];
|
||||
if(u.getName() == userId)
|
||||
{
|
||||
user = u;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
public function clearUserList() : void
|
||||
{
|
||||
this.userList = [];
|
||||
this.userCount = 0;
|
||||
this.specCount = 0;
|
||||
}
|
||||
|
||||
public function getVariable(varName:String) : *
|
||||
{
|
||||
return this.variables[varName];
|
||||
}
|
||||
|
||||
public function getVariables() : Array
|
||||
{
|
||||
return this.variables;
|
||||
}
|
||||
|
||||
public function setVariables(vars:Array) : void
|
||||
{
|
||||
this.variables = vars;
|
||||
}
|
||||
|
||||
public function clearVariables() : void
|
||||
{
|
||||
this.variables = [];
|
||||
}
|
||||
|
||||
public function getName() : String
|
||||
{
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public function getId() : int
|
||||
{
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public function isTemp() : Boolean
|
||||
{
|
||||
return this.temp;
|
||||
}
|
||||
|
||||
public function isGame() : Boolean
|
||||
{
|
||||
return this.game;
|
||||
}
|
||||
|
||||
public function isPrivate() : Boolean
|
||||
{
|
||||
return this.priv;
|
||||
}
|
||||
|
||||
public function getUserCount() : int
|
||||
{
|
||||
return this.userCount;
|
||||
}
|
||||
|
||||
public function getSpectatorCount() : int
|
||||
{
|
||||
return this.specCount;
|
||||
}
|
||||
|
||||
public function getMaxUsers() : int
|
||||
{
|
||||
return this.maxUsers;
|
||||
}
|
||||
|
||||
public function getMaxSpectators() : int
|
||||
{
|
||||
return this.maxSpectators;
|
||||
}
|
||||
|
||||
public function setMyPlayerIndex(id:int) : void
|
||||
{
|
||||
this.myPlayerIndex = id;
|
||||
}
|
||||
|
||||
public function getMyPlayerIndex() : int
|
||||
{
|
||||
return this.myPlayerIndex;
|
||||
}
|
||||
|
||||
public function setIsLimbo(b:Boolean) : void
|
||||
{
|
||||
this.limbo = b;
|
||||
}
|
||||
|
||||
public function isLimbo() : Boolean
|
||||
{
|
||||
return this.limbo;
|
||||
}
|
||||
|
||||
public function setUserCount(n:int) : void
|
||||
{
|
||||
this.userCount = n;
|
||||
}
|
||||
|
||||
public function setSpectatorCount(n:int) : void
|
||||
{
|
||||
this.specCount = n;
|
||||
}
|
||||
|
||||
public function setUserList(uList:Array) : void
|
||||
{
|
||||
this.userList = uList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package it.gotoandplay.smartfoxserver.data
|
||||
{
|
||||
public class User
|
||||
{
|
||||
|
||||
private var id:int;
|
||||
|
||||
private var name:String;
|
||||
|
||||
private var variables:Array;
|
||||
|
||||
private var isSpec:Boolean;
|
||||
|
||||
private var isMod:Boolean;
|
||||
|
||||
private var pId:int;
|
||||
|
||||
public function User(id:int, name:String)
|
||||
{
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.variables = [];
|
||||
this.isSpec = false;
|
||||
this.isMod = false;
|
||||
}
|
||||
|
||||
public function getId() : int
|
||||
{
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public function getName() : String
|
||||
{
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public function getVariable(varName:String) : *
|
||||
{
|
||||
return this.variables[varName];
|
||||
}
|
||||
|
||||
public function getVariables() : Array
|
||||
{
|
||||
return this.variables;
|
||||
}
|
||||
|
||||
public function setVariables(o:Object) : void
|
||||
{
|
||||
var key:String = null;
|
||||
var v:* = undefined;
|
||||
for(key in o)
|
||||
{
|
||||
v = o[key];
|
||||
if(v != null)
|
||||
{
|
||||
this.variables[key] = v;
|
||||
}
|
||||
else
|
||||
{
|
||||
delete this.variables[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function clearVariables() : void
|
||||
{
|
||||
this.variables = [];
|
||||
}
|
||||
|
||||
public function setIsSpectator(b:Boolean) : void
|
||||
{
|
||||
this.isSpec = b;
|
||||
}
|
||||
|
||||
public function isSpectator() : Boolean
|
||||
{
|
||||
return this.isSpec;
|
||||
}
|
||||
|
||||
public function setModerator(b:Boolean) : void
|
||||
{
|
||||
this.isMod = b;
|
||||
}
|
||||
|
||||
public function isModerator() : Boolean
|
||||
{
|
||||
return this.isMod;
|
||||
}
|
||||
|
||||
public function getPlayerId() : int
|
||||
{
|
||||
return this.pId;
|
||||
}
|
||||
|
||||
public function setPlayerId(pid:int) : void
|
||||
{
|
||||
this.pId = pid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package it.gotoandplay.smartfoxserver.handlers
|
||||
{
|
||||
import it.gotoandplay.smartfoxserver.SFSEvent;
|
||||
import it.gotoandplay.smartfoxserver.SmartFoxClient;
|
||||
import it.gotoandplay.smartfoxserver.util.ObjectSerializer;
|
||||
|
||||
public class ExtHandler implements IMessageHandler
|
||||
{
|
||||
|
||||
private var sfs:SmartFoxClient;
|
||||
|
||||
public function ExtHandler(sfs:SmartFoxClient)
|
||||
{
|
||||
super();
|
||||
this.sfs = sfs;
|
||||
}
|
||||
|
||||
public function handleMessage(msgObj:Object, type:String) : void
|
||||
{
|
||||
var params:Object = null;
|
||||
var evt:SFSEvent = null;
|
||||
var xmlData:XML = null;
|
||||
var action:String = null;
|
||||
var roomId:int = 0;
|
||||
var xmlStr:String = null;
|
||||
var asObj:Object = null;
|
||||
if(type == SmartFoxClient.XTMSG_TYPE_XML)
|
||||
{
|
||||
xmlData = msgObj as XML;
|
||||
action = xmlData.body.@action;
|
||||
roomId = int(xmlData.body.@id);
|
||||
if(action == "xtRes")
|
||||
{
|
||||
xmlStr = xmlData.body.toString();
|
||||
asObj = ObjectSerializer.getInstance().deserialize(xmlStr);
|
||||
params = {};
|
||||
params.dataObj = asObj;
|
||||
params.type = type;
|
||||
evt = new SFSEvent(SFSEvent.onExtensionResponse,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
else if(type == SmartFoxClient.XTMSG_TYPE_JSON)
|
||||
{
|
||||
params = {};
|
||||
params.dataObj = msgObj.o;
|
||||
params.type = type;
|
||||
evt = new SFSEvent(SFSEvent.onExtensionResponse,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
else if(type == SmartFoxClient.XTMSG_TYPE_STR)
|
||||
{
|
||||
params = {};
|
||||
params.dataObj = msgObj;
|
||||
params.type = type;
|
||||
evt = new SFSEvent(SFSEvent.onExtensionResponse,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package it.gotoandplay.smartfoxserver.handlers
|
||||
{
|
||||
public interface IMessageHandler
|
||||
{
|
||||
|
||||
function handleMessage(param1:Object, param2:String) : void;
|
||||
}
|
||||
}
|
||||
|
||||
+786
@@ -0,0 +1,786 @@
|
||||
package it.gotoandplay.smartfoxserver.handlers
|
||||
{
|
||||
import flash.utils.getTimer;
|
||||
import it.gotoandplay.smartfoxserver.SFSEvent;
|
||||
import it.gotoandplay.smartfoxserver.SmartFoxClient;
|
||||
import it.gotoandplay.smartfoxserver.data.Room;
|
||||
import it.gotoandplay.smartfoxserver.data.User;
|
||||
import it.gotoandplay.smartfoxserver.util.Entities;
|
||||
import it.gotoandplay.smartfoxserver.util.ObjectSerializer;
|
||||
|
||||
public class SysHandler implements IMessageHandler
|
||||
{
|
||||
|
||||
private var sfs:SmartFoxClient;
|
||||
|
||||
private var handlersTable:Array;
|
||||
|
||||
public function SysHandler(sfs:SmartFoxClient)
|
||||
{
|
||||
super();
|
||||
this.sfs = sfs;
|
||||
this.handlersTable = [];
|
||||
this.handlersTable["apiOK"] = this.handleApiOK;
|
||||
this.handlersTable["apiKO"] = this.handleApiKO;
|
||||
this.handlersTable["logOK"] = this.handleLoginOk;
|
||||
this.handlersTable["logKO"] = this.handleLoginKo;
|
||||
this.handlersTable["logout"] = this.handleLogout;
|
||||
this.handlersTable["rmList"] = this.handleRoomList;
|
||||
this.handlersTable["uCount"] = this.handleUserCountChange;
|
||||
this.handlersTable["joinOK"] = this.handleJoinOk;
|
||||
this.handlersTable["joinKO"] = this.handleJoinKo;
|
||||
this.handlersTable["uER"] = this.handleUserEnterRoom;
|
||||
this.handlersTable["userGone"] = this.handleUserLeaveRoom;
|
||||
this.handlersTable["pubMsg"] = this.handlePublicMessage;
|
||||
this.handlersTable["prvMsg"] = this.handlePrivateMessage;
|
||||
this.handlersTable["dmnMsg"] = this.handleAdminMessage;
|
||||
this.handlersTable["modMsg"] = this.handleModMessage;
|
||||
this.handlersTable["dataObj"] = this.handleASObject;
|
||||
this.handlersTable["rVarsUpdate"] = this.handleRoomVarsUpdate;
|
||||
this.handlersTable["roomAdd"] = this.handleRoomAdded;
|
||||
this.handlersTable["roomDel"] = this.handleRoomDeleted;
|
||||
this.handlersTable["rndK"] = this.handleRandomKey;
|
||||
this.handlersTable["roundTripRes"] = this.handleRoundTripBench;
|
||||
this.handlersTable["uVarsUpdate"] = this.handleUserVarsUpdate;
|
||||
this.handlersTable["createRmKO"] = this.handleCreateRoomError;
|
||||
this.handlersTable["bList"] = this.handleBuddyList;
|
||||
this.handlersTable["bUpd"] = this.handleBuddyListUpdate;
|
||||
this.handlersTable["bAdd"] = this.handleBuddyAdded;
|
||||
this.handlersTable["roomB"] = this.handleBuddyRoom;
|
||||
this.handlersTable["leaveRoom"] = this.handleLeaveRoom;
|
||||
this.handlersTable["swSpec"] = this.handleSpectatorSwitched;
|
||||
this.handlersTable["bPrm"] = this.handleAddBuddyPermission;
|
||||
this.handlersTable["remB"] = this.handleRemoveBuddy;
|
||||
this.handlersTable["swPl"] = this.handlePlayerSwitched;
|
||||
}
|
||||
|
||||
public function handleMessage(msgObj:Object, type:String) : void
|
||||
{
|
||||
var xmlData:XML = msgObj as XML;
|
||||
var action:String = xmlData.body.@action;
|
||||
var fn:Function = this.handlersTable[action];
|
||||
if(fn != null)
|
||||
{
|
||||
fn.apply(this,[msgObj]);
|
||||
}
|
||||
else
|
||||
{
|
||||
trace("Unknown sys command: " + action);
|
||||
}
|
||||
}
|
||||
|
||||
public function handleApiOK(o:Object) : void
|
||||
{
|
||||
this.sfs.isConnected = true;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onConnection,{"success":true});
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleApiKO(o:Object) : void
|
||||
{
|
||||
var params:Object = {};
|
||||
params.success = false;
|
||||
params.error = "API are obsolete, please upgrade";
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onConnection,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleLoginOk(o:Object) : void
|
||||
{
|
||||
var uid:int = int(o.body.login.@id);
|
||||
var mod:int = int(o.body.login.@mod);
|
||||
var name:String = o.body.login.@n;
|
||||
this.sfs.amIModerator = mod == 1;
|
||||
this.sfs.myUserId = uid;
|
||||
this.sfs.myUserName = name;
|
||||
this.sfs.playerId = -1;
|
||||
var params:Object = {};
|
||||
params.success = true;
|
||||
params.name = name;
|
||||
params.error = "";
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onLogin,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleLoginKo(o:Object) : void
|
||||
{
|
||||
var params:Object = {};
|
||||
params.success = false;
|
||||
params.error = o.body.login.@e;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onLogin,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleLogout(o:Object) : void
|
||||
{
|
||||
this.sfs.__logout();
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onLogout,{});
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleRoomList(o:Object) : void
|
||||
{
|
||||
var roomXml:XML = null;
|
||||
var params:Object = null;
|
||||
var evt:SFSEvent = null;
|
||||
var roomId:int = 0;
|
||||
var room:Room = null;
|
||||
var oldRoom:Room = null;
|
||||
var roomList:Array = this.sfs.getAllRooms();
|
||||
var tempList:Array = [];
|
||||
for each(roomXml in o.body.rmList.rm)
|
||||
{
|
||||
roomId = int(roomXml.@id);
|
||||
room = new Room(roomId,roomXml.n,int(roomXml.@maxu),int(roomXml.@maxs),roomXml.@temp == "1",roomXml.@game == "1",roomXml.@priv == "1",roomXml.@lmb == "1",int(roomXml.@ucnt),int(roomXml.@scnt));
|
||||
if(roomXml.vars.toString().length > 0)
|
||||
{
|
||||
this.populateVariables(room.getVariables(),roomXml);
|
||||
}
|
||||
oldRoom = roomList[roomId];
|
||||
if(oldRoom != null)
|
||||
{
|
||||
room.setVariables(oldRoom.getVariables());
|
||||
room.setUserList(oldRoom.getUserList());
|
||||
}
|
||||
tempList[roomId] = room;
|
||||
}
|
||||
this.sfs.setRoomList(tempList);
|
||||
params = {};
|
||||
params.roomList = tempList;
|
||||
evt = new SFSEvent(SFSEvent.onRoomListUpdate,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleUserCountChange(o:Object) : void
|
||||
{
|
||||
var params:Object = null;
|
||||
var evt:SFSEvent = null;
|
||||
var uCount:int = int(o.body.@u);
|
||||
var sCount:int = int(o.body.@s);
|
||||
var roomId:int = int(o.body.@r);
|
||||
var room:Room = this.sfs.getAllRooms()[roomId];
|
||||
if(room != null)
|
||||
{
|
||||
room.setUserCount(uCount);
|
||||
room.setSpectatorCount(sCount);
|
||||
params = {};
|
||||
params.room = room;
|
||||
evt = new SFSEvent(SFSEvent.onUserCountChange,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
|
||||
public function handleJoinOk(o:Object) : void
|
||||
{
|
||||
var usr:XML = null;
|
||||
var params:Object = null;
|
||||
var evt:SFSEvent = null;
|
||||
var name:String = null;
|
||||
var id:int = 0;
|
||||
var isMod:Boolean = false;
|
||||
var isSpec:Boolean = false;
|
||||
var pId:int = 0;
|
||||
var user:User = null;
|
||||
var roomId:int = int(o.body.@r);
|
||||
var roomVarsXml:XMLList = o.body;
|
||||
var userListXml:XMLList = o.body.uLs.u;
|
||||
var playerId:int = int(o.body.pid.@id);
|
||||
this.sfs.activeRoomId = roomId;
|
||||
var currRoom:Room = this.sfs.getRoom(roomId);
|
||||
currRoom.clearUserList();
|
||||
this.sfs.playerId = playerId;
|
||||
currRoom.setMyPlayerIndex(playerId);
|
||||
if(roomVarsXml.vars.toString().length > 0)
|
||||
{
|
||||
currRoom.clearVariables();
|
||||
this.populateVariables(currRoom.getVariables(),roomVarsXml);
|
||||
}
|
||||
for each(usr in userListXml)
|
||||
{
|
||||
name = usr.n;
|
||||
id = int(usr.@i);
|
||||
isMod = usr.@m == "1" ? true : false;
|
||||
isSpec = usr.@s == "1" ? true : false;
|
||||
pId = usr.@p == null ? -1 : int(usr.@p);
|
||||
user = new User(id,name);
|
||||
user.setModerator(isMod);
|
||||
user.setIsSpectator(isSpec);
|
||||
user.setPlayerId(pId);
|
||||
if(usr.vars.toString().length > 0)
|
||||
{
|
||||
this.populateVariables(user.getVariables(),usr);
|
||||
}
|
||||
currRoom.addUser(user,id);
|
||||
}
|
||||
this.sfs.changingRoom = false;
|
||||
params = {};
|
||||
params.room = currRoom;
|
||||
evt = new SFSEvent(SFSEvent.onJoinRoom,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleJoinKo(o:Object) : void
|
||||
{
|
||||
this.sfs.changingRoom = false;
|
||||
var params:Object = {};
|
||||
params.error = o.body.error.@msg;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onJoinRoomError,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleUserEnterRoom(o:Object) : void
|
||||
{
|
||||
var roomId:int = int(o.body.@r);
|
||||
var usrId:int = int(o.body.u.@i);
|
||||
var usrName:String = o.body.u.n;
|
||||
var isMod:Boolean = o.body.u.@m == "1";
|
||||
var isSpec:Boolean = o.body.u.@s == "1";
|
||||
var pid:int = o.body.u.@p != null ? int(o.body.u.@p) : -1;
|
||||
var varList:XMLList = o.body.u.vars["var"];
|
||||
var currRoom:Room = this.sfs.getRoom(roomId);
|
||||
var newUser:User = new User(usrId,usrName);
|
||||
newUser.setModerator(isMod);
|
||||
newUser.setIsSpectator(isSpec);
|
||||
newUser.setPlayerId(pid);
|
||||
currRoom.addUser(newUser,usrId);
|
||||
if(o.body.u.vars.toString().length > 0)
|
||||
{
|
||||
this.populateVariables(newUser.getVariables(),o.body.u);
|
||||
}
|
||||
var params:Object = {};
|
||||
params.roomId = roomId;
|
||||
params.user = newUser;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onUserEnterRoom,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleUserLeaveRoom(o:Object) : void
|
||||
{
|
||||
var userId:int = int(o.body.user.@id);
|
||||
var roomId:int = int(o.body.@r);
|
||||
var theRoom:Room = this.sfs.getRoom(roomId);
|
||||
if(theRoom == null)
|
||||
{
|
||||
trace("WARN - Leave Room not found, id: " + roomId);
|
||||
return;
|
||||
}
|
||||
var uName:String = theRoom.getUser(userId).getName();
|
||||
theRoom.removeUser(userId);
|
||||
var params:Object = {};
|
||||
params.roomId = roomId;
|
||||
params.userId = userId;
|
||||
params.userName = uName;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onUserLeaveRoom,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handlePublicMessage(o:Object) : void
|
||||
{
|
||||
var roomId:int = int(o.body.@r);
|
||||
var userId:int = int(o.body.user.@id);
|
||||
var message:String = o.body.txt;
|
||||
var sender:User = this.sfs.getRoom(roomId).getUser(userId);
|
||||
var params:Object = {};
|
||||
params.message = Entities.decodeEntities(message);
|
||||
params.sender = sender;
|
||||
params.roomId = roomId;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onPublicMessage,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handlePrivateMessage(o:Object) : void
|
||||
{
|
||||
var roomId:int = int(o.body.@r);
|
||||
var userId:int = int(o.body.user.@id);
|
||||
var message:String = o.body.txt;
|
||||
var theRoom:Room = this.sfs.getRoom(roomId);
|
||||
var sender:User = null;
|
||||
if(theRoom != null)
|
||||
{
|
||||
sender = theRoom.getUser(userId);
|
||||
}
|
||||
var params:Object = {};
|
||||
params.message = Entities.decodeEntities(message);
|
||||
params.sender = sender;
|
||||
params.roomId = roomId;
|
||||
params.userId = userId;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onPrivateMessage,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleAdminMessage(o:Object) : void
|
||||
{
|
||||
var roomId:int = int(o.body.@r);
|
||||
var userId:int = int(o.body.user.@id);
|
||||
var message:String = o.body.txt;
|
||||
var params:Object = {};
|
||||
params.message = Entities.decodeEntities(message);
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onAdminMessage,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleModMessage(o:Object) : void
|
||||
{
|
||||
var roomId:int = int(o.body.@r);
|
||||
var userId:int = int(o.body.user.@id);
|
||||
var message:String = o.body.txt;
|
||||
var sender:User = null;
|
||||
var room:Room = this.sfs.getRoom(roomId);
|
||||
if(room != null)
|
||||
{
|
||||
sender = room.getUser(userId);
|
||||
}
|
||||
var params:Object = {};
|
||||
params.message = Entities.decodeEntities(message);
|
||||
params.sender = sender;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onModeratorMessage,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleASObject(o:Object) : void
|
||||
{
|
||||
var roomId:int = int(o.body.@r);
|
||||
var userId:int = int(o.body.user.@id);
|
||||
var xmlStr:String = o.body.dataObj;
|
||||
var sender:User = this.sfs.getRoom(roomId).getUser(userId);
|
||||
var asObj:Object = ObjectSerializer.getInstance().deserialize(new XML(xmlStr));
|
||||
var params:Object = {};
|
||||
params.obj = asObj;
|
||||
params.sender = sender;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onObjectReceived,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
public function handleRoomVarsUpdate(o:Object) : void
|
||||
{
|
||||
var params:Object = null;
|
||||
var evt:SFSEvent = null;
|
||||
var roomId:int = int(o.body.@r);
|
||||
var userId:int = int(o.body.user.@id);
|
||||
var theRoom:Room = this.sfs.getRoom(roomId);
|
||||
var changedVars:Array = [];
|
||||
if(theRoom != null)
|
||||
{
|
||||
if(o.body.vars.toString().length > 0)
|
||||
{
|
||||
this.populateVariables(theRoom.getVariables(),o.body,changedVars);
|
||||
}
|
||||
params = {};
|
||||
params.room = theRoom;
|
||||
params.changedVars = changedVars;
|
||||
evt = new SFSEvent(SFSEvent.onRoomVariablesUpdate,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
|
||||
public function handleUserVarsUpdate(o:Object) : void
|
||||
{
|
||||
var changedVars:Array = null;
|
||||
var room:Room = null;
|
||||
var params:Object = null;
|
||||
var evt:SFSEvent = null;
|
||||
var userId:int = int(o.body.user.@id);
|
||||
var varOwner:User = null;
|
||||
var returnUser:User = null;
|
||||
if(o.body.vars.toString().length > 0)
|
||||
{
|
||||
for each(room in this.sfs.getAllRooms())
|
||||
{
|
||||
varOwner = room.getUser(userId);
|
||||
if(varOwner != null)
|
||||
{
|
||||
if(returnUser == null)
|
||||
{
|
||||
returnUser = varOwner;
|
||||
}
|
||||
changedVars = [];
|
||||
this.populateVariables(varOwner.getVariables(),o.body,changedVars);
|
||||
}
|
||||
}
|
||||
params = {};
|
||||
params.user = returnUser;
|
||||
params.changedVars = changedVars;
|
||||
evt = new SFSEvent(SFSEvent.onUserVariablesUpdate,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleRoomAdded(o:Object) : void
|
||||
{
|
||||
var rId:int = int(o.body.rm.@id);
|
||||
var rName:String = o.body.rm.name;
|
||||
var rMax:int = int(o.body.rm.@max);
|
||||
var rSpec:int = int(o.body.rm.@spec);
|
||||
var isTemp:Boolean = o.body.rm.@temp == "1" ? true : false;
|
||||
var isGame:Boolean = o.body.rm.@game == "1" ? true : false;
|
||||
var isPriv:Boolean = o.body.rm.@priv == "1" ? true : false;
|
||||
var isLimbo:Boolean = o.body.rm.@limbo == "1" ? true : false;
|
||||
var newRoom:Room = new Room(rId,rName,rMax,rSpec,isTemp,isGame,isPriv,isLimbo);
|
||||
var rList:Array = this.sfs.getAllRooms();
|
||||
rList[rId] = newRoom;
|
||||
if(o.body.rm.vars.toString().length > 0)
|
||||
{
|
||||
this.populateVariables(newRoom.getVariables(),o.body.rm);
|
||||
}
|
||||
var params:Object = {};
|
||||
params.room = newRoom;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onRoomAdded,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
private function handleRoomDeleted(o:Object) : void
|
||||
{
|
||||
var params:Object = null;
|
||||
var evt:SFSEvent = null;
|
||||
var roomId:int = int(o.body.rm.@id);
|
||||
var roomList:Array = this.sfs.getAllRooms();
|
||||
if(roomList[roomId] != null)
|
||||
{
|
||||
params = {};
|
||||
params.room = roomList[roomId];
|
||||
delete roomList[roomId];
|
||||
evt = new SFSEvent(SFSEvent.onRoomDeleted,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleRandomKey(o:Object) : void
|
||||
{
|
||||
var key:String = o.body.k.toString();
|
||||
var params:Object = {};
|
||||
params.key = key;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onRandomKey,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
private function handleRoundTripBench(o:Object) : void
|
||||
{
|
||||
var now:int = getTimer();
|
||||
var res:int = now - this.sfs.getBenchStartTime();
|
||||
var params:Object = {};
|
||||
params.elapsed = res;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onRoundTripResponse,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
private function handleCreateRoomError(o:Object) : void
|
||||
{
|
||||
var errMsg:String = o.body.room.@e;
|
||||
var params:Object = {};
|
||||
params.error = errMsg;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onCreateRoomError,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
private function handleBuddyList(o:Object) : void
|
||||
{
|
||||
var buddy:Object = null;
|
||||
var myVar:XML = null;
|
||||
var b:XML = null;
|
||||
var bVars:XMLList = null;
|
||||
var bVar:XML = null;
|
||||
var bList:XMLList = o.body.bList;
|
||||
var myVars:XMLList = o.body.mv;
|
||||
var params:Object = {};
|
||||
var evt:SFSEvent = null;
|
||||
if(myVars != null && myVars.toString().length > 0)
|
||||
{
|
||||
for each(myVar in myVars.v)
|
||||
{
|
||||
this.sfs.myBuddyVars[myVar.@n.toString()] = myVar.toString();
|
||||
}
|
||||
}
|
||||
if(bList != null && bList.b.length != null)
|
||||
{
|
||||
if(bList.toString().length > 0)
|
||||
{
|
||||
for each(b in bList.b)
|
||||
{
|
||||
buddy = {};
|
||||
buddy.isOnline = b.@s == "1" ? true : false;
|
||||
buddy.name = b.n.toString();
|
||||
buddy.id = b.@i;
|
||||
buddy.isBlocked = b.@x == "1" ? true : false;
|
||||
buddy.variables = {};
|
||||
bVars = b.vs;
|
||||
if(bVars.toString().length > 0)
|
||||
{
|
||||
for each(bVar in bVars.v)
|
||||
{
|
||||
buddy.variables[bVar.@n.toString()] = bVar.toString();
|
||||
}
|
||||
}
|
||||
this.sfs.buddyList.push(buddy);
|
||||
}
|
||||
}
|
||||
params.list = this.sfs.buddyList;
|
||||
evt = new SFSEvent(SFSEvent.onBuddyList,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
else
|
||||
{
|
||||
params.error = o.body.err.toString();
|
||||
evt = new SFSEvent(SFSEvent.onBuddyListError,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleBuddyListUpdate(o:Object) : void
|
||||
{
|
||||
var buddy:Object = null;
|
||||
var bVars:XMLList = null;
|
||||
var tempB:Object = null;
|
||||
var found:Boolean = false;
|
||||
var it:String = null;
|
||||
var bVar:XML = null;
|
||||
var params:Object = {};
|
||||
var evt:SFSEvent = null;
|
||||
if(o.body.err.toString().length > 0)
|
||||
{
|
||||
params.error = o.body.err.toString();
|
||||
evt = new SFSEvent(SFSEvent.onBuddyListError,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
return;
|
||||
}
|
||||
if(o.body.b != null)
|
||||
{
|
||||
buddy = {};
|
||||
buddy.isOnline = o.body.b.@s == "1" ? true : false;
|
||||
buddy.name = o.body.b.n.toString();
|
||||
buddy.id = o.body.b.@i;
|
||||
buddy.isBlocked = o.body.b.@x == "1" ? true : false;
|
||||
bVars = o.body.b.vs;
|
||||
tempB = null;
|
||||
found = false;
|
||||
for(it in this.sfs.buddyList)
|
||||
{
|
||||
tempB = this.sfs.buddyList[it];
|
||||
if(tempB.name == buddy.name)
|
||||
{
|
||||
this.sfs.buddyList[it] = buddy;
|
||||
buddy.isBlocked = tempB.isBlocked;
|
||||
buddy.variables = tempB.variables;
|
||||
if(bVars.toString().length > 0)
|
||||
{
|
||||
for each(bVar in bVars.v)
|
||||
{
|
||||
buddy.variables[bVar.@n.toString()] = bVar.toString();
|
||||
}
|
||||
}
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(found)
|
||||
{
|
||||
params.buddy = buddy;
|
||||
evt = new SFSEvent(SFSEvent.onBuddyListUpdate,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function handleAddBuddyPermission(o:Object) : void
|
||||
{
|
||||
var params:Object = {};
|
||||
params.sender = o.body.n.toString();
|
||||
params.message = "";
|
||||
if(o.body.txt != undefined)
|
||||
{
|
||||
params.message = Entities.decodeEntities(o.body.txt);
|
||||
}
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onBuddyPermissionRequest,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
private function handleBuddyAdded(o:Object) : void
|
||||
{
|
||||
var bVar:XML = null;
|
||||
var buddy:Object = {};
|
||||
buddy.isOnline = o.body.b.@s == "1" ? true : false;
|
||||
buddy.name = o.body.b.n.toString();
|
||||
buddy.id = o.body.b.@i;
|
||||
buddy.isBlocked = o.body.b.@x == "1" ? true : false;
|
||||
buddy.variables = {};
|
||||
var bVars:XMLList = o.body.b.vs;
|
||||
if(bVars.toString().length > 0)
|
||||
{
|
||||
for each(bVar in bVars.v)
|
||||
{
|
||||
buddy.variables[bVar.@n.toString()] = bVar.toString();
|
||||
}
|
||||
}
|
||||
this.sfs.buddyList.push(buddy);
|
||||
var params:Object = {};
|
||||
params.list = this.sfs.buddyList;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onBuddyList,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
private function handleRemoveBuddy(o:Object) : void
|
||||
{
|
||||
var it:String = null;
|
||||
var params:Object = null;
|
||||
var evt:SFSEvent = null;
|
||||
var buddyName:String = o.body.n.toString();
|
||||
var buddy:Object = null;
|
||||
for(it in this.sfs.buddyList)
|
||||
{
|
||||
buddy = this.sfs.buddyList[it];
|
||||
if(buddy.name == buddyName)
|
||||
{
|
||||
delete this.sfs.buddyList[it];
|
||||
params = {};
|
||||
params.list = this.sfs.buddyList;
|
||||
evt = new SFSEvent(SFSEvent.onBuddyList,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function handleBuddyRoom(o:Object) : void
|
||||
{
|
||||
var roomIds:String = o.body.br.@r;
|
||||
var ids:Array = roomIds.split(",");
|
||||
for(var i:int = 0; i < ids.length; i++)
|
||||
{
|
||||
ids[i] = int(ids[i]);
|
||||
}
|
||||
var params:Object = {};
|
||||
params.idList = ids;
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onBuddyRoom,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
private function handleLeaveRoom(o:Object) : void
|
||||
{
|
||||
var params:Object = null;
|
||||
var evt:SFSEvent = null;
|
||||
var roomLeft:int = int(o.body.rm.@id);
|
||||
var roomList:Array = this.sfs.getAllRooms();
|
||||
if(Boolean(roomList[roomLeft]))
|
||||
{
|
||||
params = {};
|
||||
params.roomId = roomLeft;
|
||||
evt = new SFSEvent(SFSEvent.onRoomLeft,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleSpectatorSwitched(o:Object) : void
|
||||
{
|
||||
var userId:int = 0;
|
||||
var user:User = null;
|
||||
var params:Object = null;
|
||||
var evt:SFSEvent = null;
|
||||
var roomId:int = int(o.body.@r);
|
||||
var playerId:int = int(o.body.pid.@id);
|
||||
var theRoom:Room = this.sfs.getRoom(roomId);
|
||||
if(playerId > 0)
|
||||
{
|
||||
theRoom.setUserCount(theRoom.getUserCount() + 1);
|
||||
theRoom.setSpectatorCount(theRoom.getSpectatorCount() - 1);
|
||||
}
|
||||
if(o.body.pid.@u != undefined)
|
||||
{
|
||||
userId = int(o.body.pid.@u);
|
||||
user = theRoom.getUser(userId);
|
||||
if(user != null)
|
||||
{
|
||||
user.setIsSpectator(false);
|
||||
user.setPlayerId(playerId);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sfs.playerId = playerId;
|
||||
params = {};
|
||||
params.success = this.sfs.playerId > 0;
|
||||
params.newId = this.sfs.playerId;
|
||||
params.room = theRoom;
|
||||
evt = new SFSEvent(SFSEvent.onSpectatorSwitched,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
|
||||
private function handlePlayerSwitched(o:Object) : void
|
||||
{
|
||||
var userId:int = 0;
|
||||
var user:User = null;
|
||||
var params:Object = null;
|
||||
var evt:SFSEvent = null;
|
||||
var roomId:int = int(o.body.@r);
|
||||
var playerId:int = int(o.body.pid.@id);
|
||||
var isItMe:Boolean = o.body.pid.@u == undefined;
|
||||
var theRoom:Room = this.sfs.getRoom(roomId);
|
||||
if(playerId == -1)
|
||||
{
|
||||
theRoom.setUserCount(theRoom.getUserCount() - 1);
|
||||
theRoom.setSpectatorCount(theRoom.getSpectatorCount() + 1);
|
||||
if(!isItMe)
|
||||
{
|
||||
userId = int(o.body.pid.@u);
|
||||
user = theRoom.getUser(userId);
|
||||
if(user != null)
|
||||
{
|
||||
user.setIsSpectator(true);
|
||||
user.setPlayerId(playerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(isItMe)
|
||||
{
|
||||
this.sfs.playerId = playerId;
|
||||
params = {};
|
||||
params.success = playerId == -1;
|
||||
params.newId = playerId;
|
||||
params.room = theRoom;
|
||||
evt = new SFSEvent(SFSEvent.onPlayerSwitched,params);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
|
||||
private function populateVariables(variables:Array, xmlData:Object, changedVars:Array = null) : void
|
||||
{
|
||||
var v:XML = null;
|
||||
var vName:String = null;
|
||||
var vType:String = null;
|
||||
var vValue:String = null;
|
||||
for each(v in xmlData.vars["var"])
|
||||
{
|
||||
vName = v.@n;
|
||||
vType = v.@t;
|
||||
vValue = v;
|
||||
if(changedVars != null)
|
||||
{
|
||||
changedVars.push(vName);
|
||||
changedVars[vName] = true;
|
||||
}
|
||||
if(vType == "b")
|
||||
{
|
||||
variables[vName] = vValue == "1" ? true : false;
|
||||
}
|
||||
else if(vType == "n")
|
||||
{
|
||||
variables[vName] = Number(vValue);
|
||||
}
|
||||
else if(vType == "s")
|
||||
{
|
||||
variables[vName] = vValue;
|
||||
}
|
||||
else if(vType == "x")
|
||||
{
|
||||
delete variables[vName];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function dispatchDisconnection() : void
|
||||
{
|
||||
var evt:SFSEvent = new SFSEvent(SFSEvent.onConnectionLost,null);
|
||||
this.sfs.dispatchEvent(evt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
package it.gotoandplay.smartfoxserver.http
|
||||
{
|
||||
import flash.events.Event;
|
||||
import flash.events.EventDispatcher;
|
||||
import flash.events.IOErrorEvent;
|
||||
import flash.net.URLLoader;
|
||||
import flash.net.URLRequest;
|
||||
import flash.net.URLRequestMethod;
|
||||
import flash.net.URLVariables;
|
||||
|
||||
public class HttpConnection extends EventDispatcher
|
||||
{
|
||||
|
||||
private static const HANDSHAKE:String = "connect";
|
||||
|
||||
private static const DISCONNECT:String = "disconnect";
|
||||
|
||||
private static const CONN_LOST:String = "ERR#01";
|
||||
|
||||
public static const HANDSHAKE_TOKEN:String = "#";
|
||||
|
||||
private static const servletUrl:String = "BlueBox/HttpBox.do";
|
||||
|
||||
private static const paramName:String = "sfsHttp";
|
||||
|
||||
private var sessionId:String;
|
||||
|
||||
private var connected:Boolean = false;
|
||||
|
||||
private var ipAddr:String;
|
||||
|
||||
private var port:int;
|
||||
|
||||
private var webUrl:String;
|
||||
|
||||
private var urlLoaderFactory:LoaderFactory;
|
||||
|
||||
private var urlRequest:URLRequest;
|
||||
|
||||
private var codec:IHttpProtocolCodec;
|
||||
|
||||
public function HttpConnection()
|
||||
{
|
||||
super();
|
||||
this.codec = new RawProtocolCodec();
|
||||
this.urlLoaderFactory = new LoaderFactory(this.handleResponse,this.handleIOError);
|
||||
}
|
||||
|
||||
public function getSessionId() : String
|
||||
{
|
||||
return this.sessionId;
|
||||
}
|
||||
|
||||
public function isConnected() : Boolean
|
||||
{
|
||||
return this.connected;
|
||||
}
|
||||
|
||||
public function connect(addr:String, port:int = 8080) : void
|
||||
{
|
||||
this.ipAddr = addr;
|
||||
this.port = port;
|
||||
this.webUrl = "http://" + this.ipAddr + ":" + this.port + "/" + servletUrl;
|
||||
this.sessionId = null;
|
||||
this.urlRequest = new URLRequest(this.webUrl);
|
||||
this.urlRequest.method = URLRequestMethod.POST;
|
||||
this.send(HANDSHAKE);
|
||||
}
|
||||
|
||||
public function close() : void
|
||||
{
|
||||
this.send(DISCONNECT);
|
||||
}
|
||||
|
||||
public function send(message:String) : void
|
||||
{
|
||||
var vars:URLVariables = null;
|
||||
var urlLoader:URLLoader = null;
|
||||
if(this.connected || !this.connected && message == HANDSHAKE || !this.connected && message == "poll")
|
||||
{
|
||||
vars = new URLVariables();
|
||||
vars[paramName] = this.codec.encode(this.sessionId,message);
|
||||
this.urlRequest.data = vars;
|
||||
if(message != "poll")
|
||||
{
|
||||
trace("[ Send ]: " + this.urlRequest.data);
|
||||
}
|
||||
urlLoader = this.urlLoaderFactory.getLoader();
|
||||
urlLoader.data = vars;
|
||||
urlLoader.load(this.urlRequest);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleResponse(evt:Event) : void
|
||||
{
|
||||
var event:HttpEvent = null;
|
||||
var loader:URLLoader = evt.target as URLLoader;
|
||||
var data:String = loader.data as String;
|
||||
var params:Object = {};
|
||||
if(data.charAt(0) == HANDSHAKE_TOKEN)
|
||||
{
|
||||
if(this.sessionId == null)
|
||||
{
|
||||
this.sessionId = this.codec.decode(data);
|
||||
this.connected = true;
|
||||
params.sessionId = this.sessionId;
|
||||
params.success = true;
|
||||
event = new HttpEvent(HttpEvent.onHttpConnect,params);
|
||||
dispatchEvent(event);
|
||||
}
|
||||
else
|
||||
{
|
||||
trace("**ERROR** SessionId is being rewritten");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(data.indexOf(CONN_LOST) == 0)
|
||||
{
|
||||
params.data = {};
|
||||
event = new HttpEvent(HttpEvent.onHttpClose,params);
|
||||
}
|
||||
else
|
||||
{
|
||||
params.data = data;
|
||||
event = new HttpEvent(HttpEvent.onHttpData,params);
|
||||
}
|
||||
dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleIOError(error:IOErrorEvent) : void
|
||||
{
|
||||
var params:Object = {};
|
||||
params.message = error.text;
|
||||
var event:HttpEvent = new HttpEvent(HttpEvent.onHttpError,params);
|
||||
dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package it.gotoandplay.smartfoxserver.http
|
||||
{
|
||||
import flash.events.Event;
|
||||
|
||||
public class HttpEvent extends Event
|
||||
{
|
||||
|
||||
public static const onHttpData:String = "onHttpData";
|
||||
|
||||
public static const onHttpError:String = "onHttpError";
|
||||
|
||||
public static const onHttpConnect:String = "onHttpConnect";
|
||||
|
||||
public static const onHttpClose:String = "onHttpClose";
|
||||
|
||||
public var params:Object;
|
||||
|
||||
private var evtType:String;
|
||||
|
||||
public function HttpEvent(type:String, params:Object)
|
||||
{
|
||||
super(type);
|
||||
this.params = params;
|
||||
this.evtType = type;
|
||||
}
|
||||
|
||||
override public function clone() : Event
|
||||
{
|
||||
return new HttpEvent(this.evtType,this.params);
|
||||
}
|
||||
|
||||
override public function toString() : String
|
||||
{
|
||||
return formatToString("HttpEvent","type","bubbles","cancelable","eventPhase","params");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package it.gotoandplay.smartfoxserver.http
|
||||
{
|
||||
public interface IHttpProtocolCodec
|
||||
{
|
||||
|
||||
function encode(param1:String, param2:String) : String;
|
||||
|
||||
function decode(param1:String) : String;
|
||||
}
|
||||
}
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package it.gotoandplay.smartfoxserver.http
|
||||
{
|
||||
import flash.events.Event;
|
||||
import flash.events.IOErrorEvent;
|
||||
import flash.net.URLLoader;
|
||||
import flash.net.URLLoaderDataFormat;
|
||||
|
||||
public class LoaderFactory
|
||||
{
|
||||
|
||||
private static const DEFAULT_POOL_SIZE:int = 2;
|
||||
|
||||
private var currentLoaderIndex:int;
|
||||
|
||||
private var responseHandler:Function;
|
||||
|
||||
private var errorHandler:Function;
|
||||
|
||||
public function LoaderFactory(responseHandler:Function, errorHandler:Function, poolSize:int = 2)
|
||||
{
|
||||
super();
|
||||
this.responseHandler = responseHandler;
|
||||
this.errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
public function getLoader() : URLLoader
|
||||
{
|
||||
var urlLoader:URLLoader = new URLLoader();
|
||||
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
|
||||
urlLoader.addEventListener(Event.COMPLETE,this.responseHandler);
|
||||
urlLoader.addEventListener(IOErrorEvent.IO_ERROR,this.errorHandler);
|
||||
urlLoader.addEventListener(IOErrorEvent.NETWORK_ERROR,this.errorHandler);
|
||||
return urlLoader;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package it.gotoandplay.smartfoxserver.http
|
||||
{
|
||||
public class RawProtocolCodec implements IHttpProtocolCodec
|
||||
{
|
||||
|
||||
private static const SESSION_ID_LEN:int = 32;
|
||||
|
||||
public function RawProtocolCodec()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public function encode(sessionId:String, message:String) : String
|
||||
{
|
||||
return (sessionId == null ? "" : sessionId) + message;
|
||||
}
|
||||
|
||||
public function decode(message:String) : String
|
||||
{
|
||||
var decoded:String = null;
|
||||
if(message.charAt(0) == HttpConnection.HANDSHAKE_TOKEN)
|
||||
{
|
||||
decoded = message.substr(1,SESSION_ID_LEN);
|
||||
}
|
||||
return decoded;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package it.gotoandplay.smartfoxserver.util
|
||||
{
|
||||
public class Entities
|
||||
{
|
||||
|
||||
private static var ascTab:Array = [];
|
||||
|
||||
ascTab[">"] = ">";
|
||||
ascTab["<"] = "<";
|
||||
ascTab["&"] = "&";
|
||||
ascTab["\'"] = "'";
|
||||
ascTab["\""] = """;
|
||||
|
||||
private static var ascTabRev:Array = [];
|
||||
|
||||
ascTabRev[">"] = ">";
|
||||
ascTabRev["<"] = "<";
|
||||
ascTabRev["&"] = "&";
|
||||
ascTabRev["'"] = "\'";
|
||||
ascTabRev["""] = "\"";
|
||||
|
||||
private static var hexTable:Array = new Array();
|
||||
|
||||
hexTable["0"] = 0;
|
||||
hexTable["1"] = 1;
|
||||
hexTable["2"] = 2;
|
||||
hexTable["3"] = 3;
|
||||
hexTable["4"] = 4;
|
||||
hexTable["5"] = 5;
|
||||
hexTable["6"] = 6;
|
||||
hexTable["7"] = 7;
|
||||
hexTable["8"] = 8;
|
||||
hexTable["9"] = 9;
|
||||
hexTable["A"] = 10;
|
||||
hexTable["B"] = 11;
|
||||
hexTable["C"] = 12;
|
||||
hexTable["D"] = 13;
|
||||
hexTable["E"] = 14;
|
||||
hexTable["F"] = 15;
|
||||
|
||||
public function Entities()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public static function encodeEntities(st:String) : String
|
||||
{
|
||||
var ch:String = null;
|
||||
var cod:int = 0;
|
||||
var strbuff:String = "";
|
||||
for(var i:int = 0; i < st.length; i++)
|
||||
{
|
||||
ch = st.charAt(i);
|
||||
cod = st.charCodeAt(i);
|
||||
if(cod == 9 || cod == 10 || cod == 13)
|
||||
{
|
||||
strbuff += ch;
|
||||
}
|
||||
else if(cod >= 32 && cod <= 126)
|
||||
{
|
||||
if(ascTab[ch] != null)
|
||||
{
|
||||
strbuff += ascTab[ch];
|
||||
}
|
||||
else
|
||||
{
|
||||
strbuff += ch;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
strbuff += ch;
|
||||
}
|
||||
}
|
||||
return strbuff;
|
||||
}
|
||||
|
||||
public static function decodeEntities(st:String) : String
|
||||
{
|
||||
var strbuff:String = null;
|
||||
var ch:String = null;
|
||||
var ent:String = null;
|
||||
var chi:String = null;
|
||||
var item:String = null;
|
||||
var i:int = 0;
|
||||
strbuff = "";
|
||||
while(i < st.length)
|
||||
{
|
||||
ch = st.charAt(i);
|
||||
if(ch == "&")
|
||||
{
|
||||
ent = ch;
|
||||
do
|
||||
{
|
||||
i++;
|
||||
chi = st.charAt(i);
|
||||
ent += chi;
|
||||
}
|
||||
while(chi != ";" && i < st.length);
|
||||
item = ascTabRev[ent];
|
||||
if(item != null)
|
||||
{
|
||||
strbuff += item;
|
||||
}
|
||||
else
|
||||
{
|
||||
strbuff += String.fromCharCode(getCharCode(ent));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
strbuff += ch;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return strbuff;
|
||||
}
|
||||
|
||||
public static function getCharCode(ent:String) : Number
|
||||
{
|
||||
var hex:String = ent.substr(3,ent.length);
|
||||
hex = hex.substr(0,hex.length - 1);
|
||||
return Number("0x" + hex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
package it.gotoandplay.smartfoxserver.util
|
||||
{
|
||||
public class ObjectSerializer
|
||||
{
|
||||
|
||||
private static var instance:ObjectSerializer;
|
||||
|
||||
private var debug:Boolean;
|
||||
|
||||
private var eof:String;
|
||||
|
||||
private var tabs:String;
|
||||
|
||||
public function ObjectSerializer(debug:Boolean = false)
|
||||
{
|
||||
super();
|
||||
this.tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
|
||||
this.setDebug(debug);
|
||||
}
|
||||
|
||||
public static function getInstance(debug:Boolean = false) : ObjectSerializer
|
||||
{
|
||||
if(instance == null)
|
||||
{
|
||||
instance = new ObjectSerializer(debug);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private function setDebug(b:Boolean) : void
|
||||
{
|
||||
this.debug = b;
|
||||
if(this.debug)
|
||||
{
|
||||
this.eof = "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.eof = "";
|
||||
}
|
||||
}
|
||||
|
||||
public function serialize(o:Object) : String
|
||||
{
|
||||
var result:Object = {};
|
||||
this.obj2xml(o,result);
|
||||
return result.xmlStr;
|
||||
}
|
||||
|
||||
public function deserialize(xmlString:String) : Object
|
||||
{
|
||||
var xmlData:XML = new XML(xmlString);
|
||||
var resObj:Object = {};
|
||||
this.xml2obj(xmlData,resObj);
|
||||
return resObj;
|
||||
}
|
||||
|
||||
private function obj2xml(srcObj:Object, trgObj:Object, depth:int = 0, objName:String = "") : void
|
||||
{
|
||||
var i:String = null;
|
||||
var ot:String = null;
|
||||
var t:String = null;
|
||||
var o:* = undefined;
|
||||
if(depth == 0)
|
||||
{
|
||||
trgObj.xmlStr = "<dataObj>" + this.eof;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(this.debug)
|
||||
{
|
||||
trgObj.xmlStr += this.tabs.substr(0,depth);
|
||||
}
|
||||
ot = srcObj is Array ? "a" : "o";
|
||||
trgObj.xmlStr += "<obj t=\'" + ot + "\' o=\'" + objName + "\'>" + this.eof;
|
||||
}
|
||||
for(i in srcObj)
|
||||
{
|
||||
t = typeof srcObj[i];
|
||||
o = srcObj[i];
|
||||
if(t == "boolean" || t == "number" || t == "string" || t == "null")
|
||||
{
|
||||
if(t == "boolean")
|
||||
{
|
||||
o = Number(o);
|
||||
}
|
||||
else if(t == "null")
|
||||
{
|
||||
t = "x";
|
||||
o = "";
|
||||
}
|
||||
else if(t == "string")
|
||||
{
|
||||
o = Entities.encodeEntities(o);
|
||||
}
|
||||
if(this.debug)
|
||||
{
|
||||
trgObj.xmlStr += this.tabs.substr(0,depth + 1);
|
||||
}
|
||||
trgObj.xmlStr += "<var n=\'" + i + "\' t=\'" + t.substr(0,1) + "\'>" + o + "</var>" + this.eof;
|
||||
}
|
||||
else if(t == "object")
|
||||
{
|
||||
this.obj2xml(o,trgObj,depth + 1,i);
|
||||
if(this.debug)
|
||||
{
|
||||
trgObj.xmlStr += this.tabs.substr(0,depth + 1);
|
||||
}
|
||||
trgObj.xmlStr += "</obj>" + this.eof;
|
||||
}
|
||||
}
|
||||
if(depth == 0)
|
||||
{
|
||||
trgObj.xmlStr += "</dataObj>" + this.eof;
|
||||
}
|
||||
}
|
||||
|
||||
private function xml2obj(x:XML, o:Object) : void
|
||||
{
|
||||
var nodeName:String = null;
|
||||
var node:XML = null;
|
||||
var objName:String = null;
|
||||
var objType:String = null;
|
||||
var varName:String = null;
|
||||
var varType:String = null;
|
||||
var varVal:String = null;
|
||||
var i:int = 0;
|
||||
var nodes:XMLList = x.children();
|
||||
for each(node in nodes)
|
||||
{
|
||||
nodeName = node.name().toString();
|
||||
if(nodeName == "obj")
|
||||
{
|
||||
objName = node.@o;
|
||||
objType = node.@t;
|
||||
if(objType == "a")
|
||||
{
|
||||
o[objName] = [];
|
||||
}
|
||||
else if(objType == "o")
|
||||
{
|
||||
o[objName] = {};
|
||||
}
|
||||
this.xml2obj(node,o[objName]);
|
||||
}
|
||||
else if(nodeName == "var")
|
||||
{
|
||||
varName = node.@n;
|
||||
varType = node.@t;
|
||||
varVal = node.toString();
|
||||
if(varType == "b")
|
||||
{
|
||||
o[varName] = varVal == "0" ? false : true;
|
||||
}
|
||||
else if(varType == "n")
|
||||
{
|
||||
o[varName] = Number(varVal);
|
||||
}
|
||||
else if(varType == "s")
|
||||
{
|
||||
o[varName] = varVal;
|
||||
}
|
||||
else if(varType == "x")
|
||||
{
|
||||
o[varName] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function encodeEntities(s:String) : String
|
||||
{
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user