Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sbbird.zhu committed Nov 24, 2013
0 parents commit 2bf023f
Show file tree
Hide file tree
Showing 45 changed files with 4,128 additions and 0 deletions.
108 changes: 108 additions & 0 deletions FlashAPI/XMLSocketd/Connection.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
class XMLSocketd.Connection{

private var oSocket:XMLSocket;
private var bConnected:Boolean = false;
private var aRooms:Array;

var sState:String = "disconnected";

var iPort:Number;
var sURL:String;

// Events
var onConnect = false;
var onDisconnect = false;
var onError = false;


function Connection(){

this.aRooms = new Array();
XMLSocket.prototype.oObject = this;
this.oSocket = new XMLSocket();
this.iPort = 2584;

// Base directory for SWF-file
this.sURL = _root._url;
var iLastSlash = this.sURL.lastIndexOf("/");
if(iLastSlash!=-1){
this.sURL = this.sURL.substring(0,iLastSlash);
}


}



function connect(){
this.sState = "connecting";
this.oSocket.onConnect = function (bSuccess){
this.oObject.socketOnConnect(bSuccess);
}
this.oSocket.onXML = function (oXML){
this.oObject.socketOnXML(oXML);
}

if(_root._url.indexOf("http:")==-1){
this.oSocket.connect("127.0.0.1",this.iPort);
}else{
this.oSocket.connect(null,this.iPort);
}
}


function socketOnXML(oXML){
var oFirst=oXML.firstChild;
if( oFirst!=null && oFirst.attributes["room_url"]==this.sURL){
var sRoomId = oFirst.attributes["room_id"];

for(var i=0;i<this.aRooms.length;i++){
if( (this.aRooms[i].sIdentifier+" ")==(sRoomId+" ") ){
this.aRooms[i].incommingMessage(oXML);
}
}

}else{
this.callError("Invalid XML "+oXML);
}
}


function socketOnConnect(bSuccess){
if(bSuccess){
this.sState = "connected";
}else{
this.sState = "disconnected";
}
this.bConnected = bSuccess;
if(this.onConnect){
this.onConnect(bSuccess);
}
}


function addRoom(oRoom){
var iNewId=this.aRooms.length;
this.aRooms[iNewId]=oRoom;
}

function sendMessage(oXML){
if(this.bConnected){
this.oSocket.send(oXML);
return true;
}else{
this.callError("Socket is not connected.");
return false;
}
}


private function callError(sError){
trace("Error "+sError);
if(this.onError){
this.onError(sError);
}
}


}
161 changes: 161 additions & 0 deletions FlashAPI/XMLSocketd/Room.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
class XMLSocketd.Room{

private var oConnection = false;

var sIdentifier:String = "";
var onConnect = false;
var onJoin = false;
var onError = false;
var onMessage = false;
var onUserChange = false;
var sState:String = "disconnected";

var aUsers = false;

var iMyUserId:Number = 0;
var oAuthorizeXML;


function Room( sIdentifier:String,oAuthorizeXML,oConnection ){
this.oAuthorizeXML = oAuthorizeXML;
this.aUsers = new Array();
this.sIdentifier = sIdentifier;

if(oConnection){
this.oConnection = oConnection;
}else{
this.oConnection = new XMLSocketd.Connection();
this.oConnection.currentRoom = this;
this.oConnection.onConnect = function (bSuccess){
if(this.currentRoom.onConnect){
this.currentRoom.onConnect(bSuccess);
}
if(bSuccess){
this.currentRoom.join(this.currentRoom.oAuthorizeXML);
}
}
this.oConnection.onError = function(sError){
this.currentRoom.callError(sError);
}
}

this.oConnection.addRoom(this);
}

function connect(){
this.oConnection.connect();
}

function join(oAuthorizeXML){
this.sState = "joining";

/*
var oXML = new XML();
var oNode = oXML.createElement("room:join");
oNode.attributes["room_id"] = this.sIdentifier;
oNode.attributes["room_url"] = this.oConnection.sURL;
oNode.appendChild(oAuthorizeXML);
oXML.appendChild(oNode);
*/
var oXML = new XML("<room:join room_id=\""+this.sIdentifier+"\" room_url=\""+this.oConnection.sURL+"\">"+oAuthorizeXML+"</room:join>");

if( !this.oConnection.sendMessage(oXML) ){
this.callError("Can not send join-message.");
}

}

function sendMessage(oXML){
if(oXML.firstChild!=null){
if(oXML.firstChild.nodeName=="message"){
oXML.firstChild.attributes["room_id"] = sIdentifier;
oXML.firstChild.attributes["room_url"] = this.oConnection.sURL;

this.oConnection.sendMessage(oXML);

}else{
this.callError("Root-node in message must be \"message\"");
return false;
}
}else{
this.callError("Empty XML");
return false;
}
}

private function callError(sError){
trace(sError);
if(this.onError){
this.onError(sError);
}
}

function incommingMessage(oXML){
var oFirst = oXML.firstChild;
switch(oFirst.nodeName){
case "room:authorized":
this.sState = "authorized";
this.iMyUserId = oFirst.attributes["user_id"];
if(this.onJoin){
this.onJoin( true );
}
break;
case "room:unauthorized":
this.sState="unauthorized";
if(this.onJoin){
this.onJoin( false );
}
break;
case "message":
if(this.onMessage){
this.onMessage(oXML,this.getUser(oFirst.attributes["user_id"]));
}
break;
case "userlist":
this.updateUserlist(oFirst);
if(this.onUserChange){
this.onUserChange();
}
break;
}
}

function getUser(iId){
for(var i=0;i<this.aUsers.length;i++){
if(iId==this.aUsers[i].iId){
return this.aUsers[i];
}
}
return false;
}


function updateUserlist(oXML){
for(var i=0;i<oXML.childNodes.length;i++){
var iUserId:Number = int(oXML.childNodes[i].attributes["id"]);

if(oXML.childNodes[i].nodeName=="add"){
var iNewId=this.aUsers.length;
this.aUsers[iNewId]=new XMLSocketd.User(oXML.childNodes[i],this);
}

if(oXML.childNodes[i].nodeName=="del"){
for(var j=0;j<this.aUsers.length;j++){
if(this.aUsers[j].iId==iUserId){
this.aUsers.splice(j,1);
}
}
}

if(oXML.childNodes[i].nodeName=="change"){
for(var j=0;j<this.aUsers.length;j++){
if(this.aUsers[j].iId==iUserId){
this.aUsers[j].parseXML(oXML.childNodes[i]);
}
}
}

}
}

}
34 changes: 34 additions & 0 deletions FlashAPI/XMLSocketd/User.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class XMLSocketd.User{

var iId:Number = 0;
var oUserPreferences;
var oServerPreferences;

private var oRoom;

function User(oXML,oUserRoom){
this.oRoom = oUserRoom;
this.parseXML(oXML);
}

function parseXML(oXML){
this.iId = int(oXML.attributes["id"]);
for(var i=0;i<oXML.childNodes.length;i++){
if(oXML.childNodes[i].nodeName=="prefs:user"){
this.oUserPreferences = oXML.childNodes[i].firstChild;
}
if(oXML.childNodes[i].nodeName=="prefs:server"){
this.oServerPreferences = oXML.childNodes[i].firstChild;
}
}
}

function isMe(){
if(this.oRoom.iMyUserId==this.iId){
return true;
}else{
return false;
}
}

}
Binary file added FlashAPI/example.fla
Binary file not shown.
21 changes: 21 additions & 0 deletions FlashAPI/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example</title>
</head>
<body bgcolor="#ffffff">
<!--url's used in the movie-->
<!--text used in the movie-->
<!--
<p align="left"><font face="Verdana_9pt_st" size="9" color="#39536b"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></p>
-->
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="640" height="480" id="example" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="example.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="example.swf" quality="high" bgcolor="#ffffff" width="640" height="480" name="example" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</body>
</html>
Binary file added FlashAPI/example.swf
Binary file not shown.
Loading

0 comments on commit 2bf023f

Please sign in to comment.