这个是用广播做得聊天室。就是call来call去,用broadcastMsg广播给客户端。
贴出源码和源文件。大家都能明白的。
package {
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.SharedObject;
import flash.net.Responder;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.SyncEvent;
public class Main extends Sprite {
public var apanel:panel;
public var achatPanel:chatPanel;
public var nc:NetConnection;
public var so:SharedObject;
public var myRespond:Responder;
public function Main():void {
init();
}
private function init() {
apanel=new panel();
addChild(apanel);
apanel.x=stage.stageWidth/2-apanel.width/2;
apanel.y=stage.stageHeight/2-apanel.height/2;
apanel.btn.buttonMode=true;
apanel.btn.mouseChildren=false;
apanel.btn.addEventListener(MouseEvent.CLICK,clickOneHandler);
}
private function clickOneHandler(e:MouseEvent) {
if (apanel.nameTxt.text=="") {
apanel.txt.text="如果要聊天,请输入昵称!";
} else {
apanel.txt.text="你的昵称是:"+apanel.nameTxt.text;
removeChild(apanel);
net();
}
}
private function net() {
nc=new NetConnection();
nc.connect("rtmp://localhost/chatFMS");
nc.client=this;
nc.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
}
private function statusHandler(e:NetStatusEvent) {
trace(e.info.code);
if (e.info.code=="NetConnection.Connect.Success") {
achatPanel=new chatPanel();
addChild(achatPanel);
achatPanel.x=stage.stageWidth/2-achatPanel.width/2;
achatPanel.y=stage.stageHeight/2-achatPanel.height/2;
achatPanel.inTxt.multiline = true ;//是否显示多行文本
achatPanel.outTxt.multiline = true;
achatPanel.inTxt.wordWrap=true;// 是否自动换行
achatPanel.outTxt.wordWrap=true;
achatPanel.sendBtn.buttonMode=true;
achatPanel.sendBtn.mouseChildren=false;
achatPanel.sendBtn.addEventListener(MouseEvent.CLICK,clickTwoHandler);
}
}
private function clickTwoHandler(e:MouseEvent) {
if (achatPanel.inTxt.text!="") {
myRespond=new Responder(success,failed);
nc.call("inChat",myRespond,achatPanel.inTxt.text,apanel.nameTxt.text);
}
achatPanel.inTxt.text="";
}
private function success(result:Object) {
//trace("aaa:"+result.toString());
}
private function failed(result:Object) {
//trace("aaa:"+result.toString());
}
public function outChat(chat,chatName) {
//achatPanel.outTxt.text=so.data.achat;
achatPanel.outTxt.appendText(chatName+":"+chat+"\n");
}
}
}