天天看点

FMS简单聊天室(一)共享对象

    偶也刚学这个,只会简单的。将自己的想法分享出来。聊天室可以用共享对象做也可以用call广播。这里先分享用共享对象做。

    这里我只贴出源代码和源文件。

    新建一个Main.as

package {

import flash.display.Sprite;

import flash.net.NetConnection;

import flash.net.SharedObject;

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 sayRight:Boolean=false;

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.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);

private function statusHandler(e:NetStatusEvent) {

trace(e.info.code);

if (e.info.code=="NetConnection.Connect.Success") {

so=SharedObject.getRemote("happyChat",nc.uri,false);

so.connect(nc);

so.addEventListener(SyncEvent.SYNC,syncHandler);

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!="") {

so.setProperty("aname",apanel.nameTxt.text);

so.setProperty("achat",achatPanel.inTxt.text);

achatPanel.inTxt.text="";

private function syncHandler(e:SyncEvent) {

if (sayRight==false) {

sayRight=true;

return;

//achatPanel.outTxt.text=so.data.achat;

achatPanel.outTxt.appendText(so.data.aname+":"+so.data.achat+"\n");

}

}