package { /* Author: Max Maurer Stellt Methoden zur Spiele Verwaltung zur Verfügung und muss in Verbindung mit dem FlashGameServer verwendet werden. */ import flash.display.Sprite; import flash.net.XMLSocket; import flash.events.*; /** * This class provides an interface to the FlashGameCommunicator * Server used in the Blockparktikum MultimediaProgrammierung 2008 * at the Media Informatics Group * at the Ludwig-Maximilians-Universität in Munich, Germany. *

The server as well as this API was written and * is copyrighted by Max Maurer. The server application as well as the API may * only be used with written permission of the author. * Feel free to contact me at: max.maurer (at) stud.ifi.lmu.de *

*

The only thing needed to communicate with the FlashGameCommunicator-Server is an instance * of this class. One can issue commands on the server by calling the public methods of this * class. Answers from the serer are reported as Events to six different types of EventListeners * that can be added to this class.

*

The first method to call after creating the class and setting up the EventListeners * is the connect method. For the usage of all the other methods see below. *

*

Each supported Method has a ACTION_...-Parater assigned to it. The * parameter is returned each time a Event from the server is fired and can be read out * of the EventObject.

*

A simple call sequence to an exisiting server could look like this:

* */ public class FlashGameCommunicator extends Sprite { /** Action asscoiacted with the sendName command * @see FlashGameCommunicator#sendName() */ public static var ACTION_SEND_NAME:uint=1; /** Action asscociated with a connection the is currently established * @see FlashGameCommunicator#connect() */ public static var ACTION_CONNECTING:uint=2; /** Action associated with the createRoom command * @see FlashGameCommunicator#createRoom() */ public static var ACTION_ROOM_CREATE:uint=3; /** Action asscoiated with the roomList command * @see FlashGameCommunicator#roomList() */ public static var ACTION_ROOM_LIST:uint=4; /** Action asscoiated with the leaveRoom command * @see FlashGameCommunicator#leaveRoom() */ public static var ACTION_ROOM_LEAVE:uint=5; /** Action asscoiated with the joinRoom command * @see FlashGameCommunicator#joinRoom() */ public static var ACTION_ROOM_JOIN:uint=6; /** Action asscoiated with the lockRoom command * @see FlashGameCommunicator#lockRoom() */ public static var ACTION_ROOM_LOCK:uint=7; /** Action asscoiated with the unlockRoom command * @see FlashGameCommunicator#unlockRoom() */ public static var ACTION_ROOM_UNLOCK:uint=8; /** Action asscoiated with the userList command * @see FlashGameCommunicator#userList() */ public static var ACTION_USER_LIST:uint=9; /** Action asscoiated with the userListRoom command * @see FlashGameCommunicator#userListRoom() */ public static var ACTION_USER_LIST_ROOM:uint=10; /** Action asscoiated with the roomInformation command * @see FlashGameCommunicator#roomInformtion() */ public static var ACTION_ROOM_INFORMATION:uint=11; /** Action asscoiated with the sendMessageUser command * @see FlashGameCommunicator#sendMessageUser() */ public static var ACTION_MESSAGE_USER:uint=12; /** Action asscoiated with the sendMessageRoom command * @see FlashGameCommunicator#sendMessageRoom() */ public static var ACTION_MESSAGE_ROOM:uint=13; /** Action asscoiated with the sendMessage command * @see FlashGameCommunicator#sendMessage() */ public static var ACTION_MESSAGE:uint=14; /** Action asscoiated with the sendMessageLobby command * @see FlashGameCommunicator#sendMessageLobby() */ public static var ACTION_MESSAGE_LOBBY:uint=15; /** This action is always associated when a kicked event is reported * @see GameEvent#KICKED */ public static var ACTION_KICKED:uint=16; /** This action is always associated when a disconnected event is reported * @see GameEvent#DISCONNECTED */ public static var ACTION_DISCONNECTED:uint=17; /** Attached to messageType in the GameEvent-Class for all incmoing broadcast Messages */ public static var MESSAGE_BROADCAST:uint=100; /** Attached to messageType in the GameEvent-Class for all incmoing lobby Messages */ public static var MESSAGE_LOBBY:uint=101; /** Attached to messageType in the GameEvent-Class for all incmoing room Messages */ public static var MESSAGE_ROOM:uint=102; /** Attached to messageType in the GameEvent-Class for all incmoing personal Messages */ public static var MESSAGE_PERSONAL:uint=103; /** Attached to an incoming information that a user joined the room */ public static var INFO_USER_JOINED:uint=201; /** Attached to an incoming information that a user left the room */ public static var INFO_USER_LEFT:uint=202; private var hostName:String; private var debug:Boolean; private var port:uint; private var socket:XMLSocket; private var myId:uint; private var connected:Boolean; private var currentAction:uint; private var roomId:uint; private var roomName:String; private var dataArray:Array; // Array used if a command gives a list of values private var dataLines:Number; // The number of lines the system is still waiting for /** * Create a new FlashGameCommunicator object to comunicate with a * FlashGameCommunicator Server */ public function FlashGameCommunicator():void { connected=false; socket = new XMLSocket(); configureListeners(socket); roomId=0; roomName=""; dataLines = -1; debug=false; } public function setDebug(b:Boolean):void { debug=b; } /** * Connect to a host and port where a FlashGameCommuncicator-Server is listening * After a successfull connection attempt the connected-Event will be fired * @param host A String containing an IP-Adress or hostName of a host to conncet to * @param port A port number bigger than 1024 where the server is listening on * @throws Error Throws an Error if you try to call this function when a connection has already been established */ public function connect(host:String,port:uint):void { if (connected) throw new Error("Already connected"); this.hostName=host; this.port = port; currentAction = ACTION_CONNECTING; if (debug) trace("CONNECTING...."); socket.connect(hostName, port); } /** * This shows if the object is currently connected to a server * @return true if connected to a server or false if disconnected */ public function getConnected():Boolean { return connected; } /** * Shows if the object is connected and the user has joined a room or if he is still in the lobby * @return true if the user is still in the lobby * @throws Error An error is thrown if there is no connection to the server */ public function isInRoom():Boolean { if (!getConnected()) throw new Error("Not connected"); return roomId>0; } /** * Returns the name of the room the user is in * @return the name of the room the user is in or null if the user is not connected to a room or the room name can not be identified * @throws Error An error is thrown if there is no connection to the server */ public function getRoomName():String { if (!getConnected()) throw new Error("Not connected"); if (roomId==0) return null; return roomName; } /** * Returns the id of the room the user is in * @return the id of the room the user is in or 0 if the user is not connected to any room * @throws Error An error is thrown if there is no connection to the server */ public function getRoomId():Number { if (!getConnected()) throw new Error("Not connected"); return roomId; } /** * Change the name of the user on the server. After a connection to the server * the user name is not defined and will be given as a String containing "null" each time a userName is requested *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @param name The new name for this player * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @throws Error An error is thrown if there is no connection to the server */ public function sendName(name:String):void { if (!connected) throw new Error("Not connectded"); name = Helper.escapeMessage(name); currentAction = ACTION_SEND_NAME; send("name_set;"+name); } /** * Create and join a new room as a master player. After the success full creation the room will show * up on the room list. A room exists as long as the master player does not leave it. * If he does the room is closed and all other players in the room are kicked. *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @param name A name for this room * @param capacity How many players should be in the room. The room master count as a player too. That's why 1 is the minimum here. Than the room is immedialty full. * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @throws Error An error is thrown if there is no connection to the server * @throws Error An Error is also thrown if the player still is in a room */ public function createRoom(name:String,capacity:Number):void { if (!connected) throw new Error("Not connectded"); if (isInRoom()) throw new Error("You are in a room already. Leave it first!"); name = Helper.escapeMessage(name); currentAction = ACTION_ROOM_CREATE; roomName = name; send("room_create;"+capacity+";"+name); } /** * Disconnects immedialty from the server. There will be no SUCCESS or FAILED event on this. * Normally a disconnect event should be fired immediately after this action took place. * @throws Error An error is thrown if there is no connection to the server */ public function disconnect():void { if (!connected) throw new Error("Not connectded"); currentAction = 0; send("quit"); } /** * Get a list from the server with all rooms avaiable * The list ist returned in the dataArray of the GameEvent. For more Information see the * description of dataArray in the GameEvent class. *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#dataArray * @throws Error An error is thrown if there is no connection to the server */ public function roomList():void { if (!connected) throw new Error("Not connectded"); currentAction = ACTION_ROOM_LIST; send("room_list"); } /** * Leave the room the user is currenly in. If the user is the master user of this room, the room will * be closed and all other players will be kicked. *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @throws Error An error is thrown if there is no connection to the server * @throws Error An Error is also thrown if the player is not in a room */ public function leaveRoom():void { if (!connected) throw new Error("Not connectded"); if (!isInRoom()) throw new Error("You are not in a room you can leave it!"); currentAction = ACTION_ROOM_LEAVE; send("room_leave"); } /** * Lock the room. * If a room is locked nobody else can join it. Locking is only available * to the player who created the room. If any other player tries to lock the room the * lock will fail. Use unlockRoom() to revert this. To see wether a room is locked or * not you can use roomInformation(). *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @see FlashGameCommunicator#unlockRoom() * @see FlashGameCommunicator#roomInformation() * @throws Error An error is thrown if there is no connection to the server * @throws Error An Error is also thrown if the player is not in a room */ public function lockRoom():void { if (!connected) throw new Error("Not connectded"); if (!isInRoom()) throw new Error("You are not in a room!"); currentAction = ACTION_ROOM_LOCK; send("room_lock"); } /** * Unlock a room that has been previously locked. * If a room is locked nobody else can join it. Locking is only available * to the player who created the room. If any other player tries to lock the room the * lock will fail. Use unlockRoom() to revert this. To see wether a room is locked or * not you can use roomInformation(). *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @see FlashGameCommunicator#unlockRoom() * @see FlashGameCommunicator#roomInformation() * @throws Error An error is thrown if there is no connection to the server * @throws Error An Error is also thrown if the player is not in a room */ public function unlockRoom():void { if (!connected) throw new Error("Not connectded"); if (!isInRoom()) throw new Error("You are not in a room!"); currentAction = ACTION_ROOM_UNLOCK; send("room_unlock"); } /** * Get a list of all the users in the lobby (that means users that are not in a room). * The list ist returned in the dataArray of the GameEvent. For more Information see the * description of dataArray in the GameEvent class. *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#dataArray * @throws Error An error is thrown if there is no connection to the server */ public function userList():void { if (!connected) throw new Error("Not connectded"); currentAction = ACTION_USER_LIST; send("user_list"); } /** * Get a list of all players in the current room. The first player on the list is * always the master player of this room. * The list is returned in the dataArray of the GameEvent. For more Information see the * description of dataArray in the GameEvent class. *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @param roomId The id of a room on the server if no id is specified the information of the current room the user is in will be returned * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#dataArray * @throws Error An error is thrown if there is no connection to the server * @throws Error An Error is also thrown if the player is not in a room */ public function userListRoom(roomId:Number):void { if (!connected) throw new Error("Not connectded"); //if (!isInRoom()) throw new Error("You are not in a room!"); currentAction = ACTION_USER_LIST_ROOM; send("user_list_room;"+roomId); } /** * Get more information on a certain room. * The GameEvent returned on this command will have set the parameters roomName, roomCapacity, roomSize and roomLocked. *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @param roomId The id of a room on the server if no id is specified the information of the current room the user is in will be returned * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#roomName * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#roomCapacity * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#roomSize * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#roomLocked * @throws Error An error is thrown if there is no connection to the server */ public function roomInformation(roomId:Number):void { if (!connected) throw new Error("Not connectded"); currentAction = ACTION_ROOM_INFORMATION; send("room_information;"+roomId); } /** * Join a specific room that exists already. The join will fail if the capacity of this * room has already been reached or if the room is locked. * When the join is successfull the roomId and roomName will be set in the GameEvent-Object. *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @param roomId The roomId of the room the user wants to join * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#roomId * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#roomName * @throws Error An error is thrown if there is no connection to the server * @throws Error An Error is also thrown if the player still is in a room */ public function joinRoom(roomId:Number):void { if (!connected) throw new Error("Not connectded"); if (isInRoom()) throw new Error("Leave room first!"); currentAction = ACTION_ROOM_JOIN; send("room_join;"+roomId); } /** * Send a broadcast message to all players on the server. Every player will receive it * even if the player is in locked room. Broadcast messages should be used thoughtfully. * The player himself will not receive the message again. Instead he will receive a success * event, when the message has been delivered completly. *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @param message The message to be sent * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @throws Error An error is thrown if there is no connection to the server */ public function sendMessage(message:String):void { if (!connected) throw new Error("Not connected"); currentAction = ACTION_MESSAGE; message = Helper.escapeMessage(message); send("message;"+message); } /** * Send a message only to everyone in the lobby. * The player himself will not receive the message again. Instead he will receive a success * event, when the message has been delivered completly. *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @param message The message to be sent * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @throws Error An error is thrown if there is no connection to the server */ public function sendMessageLobby(message:String):void { if (!connected) throw new Error("Not connected"); currentAction = ACTION_MESSAGE_LOBBY; message = Helper.escapeMessage(message); send("message_lobby;"+message); } /** * Send a message to all the players in the same room * The player himself will not receive the message again. Instead he will receive a success * event, when the message has been delivered completly. *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @param message The message to be sent * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @throws Error An error is thrown if there is no connection to the server * @throws Error An Error is also thrown if the player is not in a room */ public function sendMessageRoom(message:String):void { if (!connected) throw new Error("Not connected"); if (!isInRoom()) throw new Error("Enter room first!"); currentAction = ACTION_MESSAGE_ROOM; message = Helper.escapeMessage(message); send("message_room;"+message); } /** * Sent a message only to a specific user on the server. * The user only needs to be online. Its not necessary that he is in * the same room and he will receive the message even if the room he is in is locked. * The player himself will not receive the message again. Instead he will receive a success * event, when the message has been delivered completly. *

This method has no return value instead a SUCCESS or FAILED event is fired depending on what * the server answer to this request was. The GameEvent always contains the corresponding ACTION_...-Type * in the messageType Parameter.

* @param message The message to be sent * @param userId The Id of the user who should receive the message * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#FAILED * @see de.maxmaurer.FlashGameCommunicator.Events.GameEvent#SUCCESS * @throws Error An error is thrown if there is no connection to the server */ public function sendMessageUser(message:String,userId:Number):void { if (!connected) throw new Error("Not connected"); currentAction = ACTION_MESSAGE_USER; message = Helper.escapeMessage(message); send("message_user;"+userId+";"+message); } // PRIVVATE FUNCTIONS (Really private) ;-) private function dispatchNow(evt:Event):void { if (debug) trace("dispatching event: "+evt); dispatchEvent(evt); } private function dataHandler(event:DataEvent):void { if (debug) trace("Data was received: " + event.data); var pattern:RegExp = /([^\\]);/m; // split all values with a semikolon not preceeded by a backslash, since we are lossing this value we put it in partehesis to keep it one value upper. Now we got to join every x and x+1 value in the array together var doubleFields:Array = event.data.split(pattern); var fields:Array = new Array(); for (var i:Number=0;i=doubleFields.length) fields.push(doubleFields[i]); else fields.push(doubleFields[i]+""+doubleFields[i+1]); } if (debug) trace("the split up values: "+fields); var code:Number = Number(fields[0]); if (code == 101) { myId = Number(fields[1]); dispatchNow(new GameEvent(GameEvent.CONNECTED,{myId:myId})); } else if (code==140) { // send name dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_SEND_NAME})); } else if (code==121) { // room create roomId = Number(fields[1]); dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_ROOM_CREATE,roomId:roomId,roomName:roomName})); } else if (code==127) { // room list if (dataLines==-1) { dataArray = new Array(); dataLines = Number(fields[1]); if (dataLines<=0) { dataLines=-1; dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_ROOM_LIST,dataArray:dataArray})); } return; } dataLines--; var roomName:String = Helper.unescapeMessage(fields[2]); dataArray.push({roomId:Number(fields[1]), roomName:roomName}); if (dataLines<=0) { dataLines=-1; dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_ROOM_LIST,dataArray:dataArray})); } } else if (code==128) { // room leave roomId=0; roomName=""; dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_ROOM_LEAVE})); } else if (code==126 && Number(fields[1])==1) { // room lock dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_ROOM_LOCK})); } else if (code==126 && Number(fields[1])==0) { // room unlock dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_ROOM_UNLOCK})); } else if (code==111) { // list users if (dataLines==-1) { dataArray = new Array(); dataLines = Number(fields[1]); if (dataLines<=0) { dataLines=-1; dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_USER_LIST,dataArray:dataArray})); } return; } dataLines--; var userName:String = Helper.unescapeMessage(fields[2]); dataArray.push({userId:Number(fields[1]), userName:userName}); if (dataLines<=0) { dataLines=-1; dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_USER_LIST,dataArray:dataArray})); } } else if (code==112) { // list rooms if (dataLines==-1) { dataArray = new Array(); dataLines = Number(fields[1]); if (dataLines<=0) { dataLines=-1; dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_USER_LIST_ROOM,dataArray:dataArray})); } return; } dataLines--; var userNameRoom:String = Helper.unescapeMessage(fields[2]); dataArray.push({userId:Number(fields[1]), userName:userNameRoom}); if (dataLines<=0) { dataLines=-1; dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_USER_LIST_ROOM,dataArray:dataArray})); } } else if (code==123) { // room information var b:Boolean = false; var n:Number = Number(fields[1]); if (n==1) b= true; var name:String = Helper.unescapeMessage(fields[4]); dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_ROOM_INFORMATION,roomLocked:b,roomCapacity:Number(fields[2]),roomSize:Number(fields[3]),roomName:name})); } else if (code==122) { // room join roomId = Number(fields[1]); roomName = fields[2]; dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_ROOM_JOIN,roomId:roomId,roomName:roomName})); } else if ((code==225 || code==224)) { // 225=FULL, 224 locked dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_ROOM_JOIN})); } else if (code==130) { // send message dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_MESSAGE})); } else if (code==131) { // send room dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_MESSAGE_ROOM})); } else if (code==133) { // send lobby dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_MESSAGE_LOBBY})); } else if (currentAction == ACTION_MESSAGE_USER && code==100) { dispatchNow(new GameEvent(GameEvent.SUCCESS,{messageType:ACTION_MESSAGE_USER})); } else if (code==530) { // Broadcast message var senderId:Number = Number(fields[1]); var senderName:String = Helper.unescapeMessage(fields[2]); var message:String = Helper.unescapeMessage(fields[3]); dispatchNow(new GameEvent(GameEvent.MESSAGE,{messageType:MESSAGE_BROADCAST,senderId:senderId,senderName:senderName,message:message})); } else if (code==531) { // ROOM message var senderId1:Number = Number(fields[1]); var senderName1:String = Helper.unescapeMessage(fields[2]); var message1:String = Helper.unescapeMessage(fields[3]); dispatchNow(new GameEvent(GameEvent.MESSAGE,{messageType:MESSAGE_ROOM,senderId:senderId1,senderName:senderName1,message:message1})); } else if (code==532) { // Personal message var senderId2:Number = Number(fields[1]); var senderName2:String = Helper.unescapeMessage(fields[2]); var message2:String = Helper.unescapeMessage(fields[3]); dispatchNow(new GameEvent(GameEvent.MESSAGE,{messageType:MESSAGE_PERSONAL,senderId:senderId2,senderName:senderName2,message:message2})); } else if (code==533) { // Lobby message var senderId3:Number = Number(fields[1]); var senderName3:String = Helper.unescapeMessage(fields[2]); var message3:String = Helper.unescapeMessage(fields[3]); dispatchNow(new GameEvent(GameEvent.MESSAGE,{messageType:MESSAGE_LOBBY,senderId:senderId3,senderName:senderName3,message:message3})); } else if (code==200 || code==400) { dispatchNow(new GameEvent(GameEvent.FAILED,{message:fields[1]})); } else if (code==221) { dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_ROOM_CREATE})); } else if (code==222) { dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_ROOM_JOIN})); } else if (code==223) { dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_ROOM_INFORMATION})); } else if (code==226 && Number(fields[1])==1) { dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_ROOM_LOCK})); } else if (code==226 && Number(fields[1])==0) { dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_ROOM_UNLOCK})); } else if (code==227) { dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_ROOM_LIST})); } else if (code==230) { dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_MESSAGE})); } else if (code==231) { dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_MESSAGE_ROOM})); } else if (code==232) { dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_MESSAGE_USER})); } else if (code==233) { dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_MESSAGE_LOBBY})); } else if (code==240) { dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:ACTION_SEND_NAME})); } else if (code==150) { // user joined the room var userId1:Number = Number(fields[1]); var userName1:String = Helper.unescapeMessage(fields[2]); dispatchNow(new GameEvent(GameEvent.USER_JOINED, {messageType:INFO_USER_JOINED,userId:userId1,userName:userName1})); } else if (code==151) { // user left the room var userId2:Number = Number(fields[1]); var userName2:String = Helper.unescapeMessage(fields[2]); dispatchNow(new GameEvent(GameEvent.USER_LEFT, {messageType:INFO_USER_LEFT, userId:userId2, userName:userName2})); } else if (code==301) { roomId=0; roomName=""; dispatchNow(new GameEvent(GameEvent.KICKED,{messageType:ACTION_KICKED})); } } private function ioErrorHandler(event:IOErrorEvent):void { if (debug) trace("XMLSocket received a ioErrorHandler event: " + event); dispatchNow(new GameEvent(GameEvent.FAILED,{messageType:currentAction})); } private function progressHandler(event:ProgressEvent):void { if (debug) trace("XMLSocket received a progressHandler event loaded:" + event.bytesLoaded + " total: " + event.bytesTotal); } private function securityErrorHandler(event:SecurityErrorEvent):void { if (debug) trace("XMLSocket received a securityErrorHandler event: " + event); } private function send(data:String):void { if (!connected) throw new Error("Not connected, please connect first"); if (debug) ("Sending data:\n"+data); socket.send(data); } private function configureListeners(dispatcher:IEventDispatcher):void { dispatcher.addEventListener(Event.CLOSE, closeHandler); dispatcher.addEventListener(Event.CONNECT, connectHandler); dispatcher.addEventListener(DataEvent.DATA, dataHandler); dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler); dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); } private function closeHandler(event:Event):void { if (debug) trace("XMLSocket received a closeHanlder event: " + event); connected=false; roomId=0; roomName=""; dispatchNow(new GameEvent(GameEvent.DISCONNECTED,{messageType:ACTION_DISCONNECTED})); } private function connectHandler(event:Event):void { if (debug) trace("XMLSocket receive a connectHandler event: " + event); socket.send("hello"); connected=true; } } }