package cn.cjxo
{import flash.display.Sprite;
import flash.events.DataEvent;
import flash.events.Event;
import flash.external.ExternalInterface;
import flash.net.XMLSocket;
/**
* @author ChenJianxiang
* @deprecated JavaSocket->ActionSocket<-JavaSocketServer
* function SocketConnect(host,port){} //JavaScript CALL ActionScript 连接
* function SocketSend(data){} //JavaScript CALL ActionScript 发送
* function SocketClose(){} //JavaScript CALL ActionScript 关闭
* function SocketOnInit(){} //初始化
* function SocketOnConnect(success){} //ActionScript CALL JavaScript 连接成功
* function SocketOnData(data){} //ActionScript CALL JavaScript 服务器发送数据
* function SocketOnClose(){} //ActionScript CALL JavaScript 服务器发关闭
* 通过flash的xmlSocket client的发送的xml,应该用”\0\n”结尾
*/
public class Main extends Sprite {
private var xmlSocket:XMLSocket = null;
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
//ExternalInterface.call("cal", "被我调到了9!!");
ExternalInterface.addCallback("SocketConnect", _socketConnect);
ExternalInterface.addCallback("SocketSend", _socketSend);
ExternalInterface.addCallback("SocketClose", _socketClose);
//ExternalInterface.addCallback("SocketOnInit", _socketConnect);
xmlSocket.addEventListener(Event.CONNECT, function(e:Event):void{ //ActionScript CALL JavaScript 连接成功
ExternalInterface.call("SocketOnConnect", xmlSocket.connected);
});
xmlSocket.addEventListener(DataEvent.DATA, function(e:Event):void { //ActionScript CALL JavaScript 服务器发送数据
ExternalInterface.call("SocketOnData", "");
});
xmlSocket.addEventListener(Event.CLOSE, function(e:Event):void { //ActionScript CALL JavaScript 服务器发关闭
ExternalInterface.call("SocketOnClose", "");
});
}
private function init(e:Event = null):void{
removeEventListener(Event.ADDED_TO_STAGE, init);
if(xmlSocket == null){
xmlSocket = new XMLSocket();//建立XMLSocket对象
}
}
/**
* 连接
* @param host
* @param port
*/
private function _socketConnect(host:String, port:int):void {
if (xmlSocket.connected) {
ExternalInterface.call("cal", "联上了还联什么"); //如果重复连接
return;
}
xmlSocket.connect(host, port);
}
/**
* 发送信息
* @param message
*/
private function _socketSend(message:String):void {
xmlSocket.send(message);
}
private function _socketClose():void {
xmlSocket.close();
}
}
}
AS3 在线 WEB 聊天器
最新推荐文章于 2024-08-24 21:56:58 发布