📄 connector.as
字号:
package cn.humanmonth.chat{
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.XMLSocket;
/**
*
* @author Piterlin
* 处理链接的细节
*/
public class Connector {
// private var domain:String="192.168.20.2";
private var domain:String="humanmonth.cn";
// private var domain:String="localhost";
private var socket:XMLSocket;
private var initEvent:Function;
private var onDataEvent:Function;
private var ioErrorEvent:Function;
private var httpIOErrorEvent:Function;
private var shouldSendKeepAlive:Boolean=false;
public function Connector(initEvent:Function,onDataEvent:Function,ioErrorEvent:Function,httpIOErrorEvent:Function) {
this.initEvent=initEvent;
this.onDataEvent=onDataEvent;
this.ioErrorEvent=ioErrorEvent;
this.httpIOErrorEvent=httpIOErrorEvent;
this.connect();
}
public function connect():void {
var loader:URLLoader=new URLLoader();
loader.addEventListener(IOErrorEvent.IO_ERROR,httpIOErrorEvent);
loader.addEventListener(Event.COMPLETE,onComplete);
loader.load(new URLRequest("http://"+domain+"/ChatServerServlet.do"));
}
private function onComplete(event:Event):void {
var loader:URLLoader=URLLoader(event.target);
var port:Number=loader.data;
socket=new XMLSocket() ;
socket.addEventListener(Event.CONNECT,initEvent);
socket.addEventListener(DataEvent.DATA,onDataEvent);
socket.addEventListener(IOErrorEvent.IO_ERROR,ioErrorEvent);
socket.connect(domain,port);
}
public function send(tag:String,value:String):void {
try {
this.setShouldSendKeepAliveFalse();
socket.send("<"+tag+">"+value+"</"+tag+">\r\n");
} catch (er:Error) {
this.ioErrorEvent(null);
}
}
public function setShouldSendKeepAliveFalse():void{
this.shouldSendKeepAlive=false;
}
public function getShouldSendKeepAlive():Boolean{
return this.shouldSendKeepAlive;
}
public function setShouldSendKeepAliveTrue():void{
this.shouldSendKeepAlive=true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -