flex聊天室程序

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">


<mx:Script>
<![CDATA[
import flash.events.SecurityErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;

import flash.net.Socket;

import flash.utils.ByteArray;

private var socket:Socket = new Socket();
internal function initApp():void
{
//socket.addEventListener(Event.CLOSE,closeHandler);
socket.addEventListener(Event.CONNECT,connectHandler);
socket.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
socket.addEventListener(ProgressEvent.SOCKET_DATA,socketDataHandler);
}

internal function closeHandler(evt:Event):void
{
output("connect fail and close");
}

internal function connectHandler(evt:Event):void
{
output("connect success");
}

internal function ioErrorHandler(evt:IOErrorEvent):void
{
output("io error:"+evt.text);
}

internal function securityErrorHandler(evt:SecurityErrorEvent):void
{
output("security error :"+evt.text);
}

internal function doConnect():void
{
var server:String = server_txt.text;
var port:Number =Number(port_txt.text);

socket.connect(server,port);

}

internal function socketDataHandler(evt:ProgressEvent):void
{
var msg:String;
while(socket.bytesAvailable)
msg+=socket.readMultiByte(socket.bytesAvailable,"utf8");
var arr:Array = msg.split('\n');
for(var i:int=0;i<arr.length;i++)
{
if(arr[i].length>1)
{
var myPattern:RegExp=/\r/;
arr[i]=arr[i].replace(myPattern,'');
output(arr[i]);
}
}
}

internal function sendMessage(msg:String):void
{

var message:ByteArray= new ByteArray();
message.writeMultiByte(msg, "gb2312"); //指定字符编码集
message.length = 32;
socket.writeBytes(message);
socket.flush();
}

internal function output(msg:String):void
{
msg=msg+"\n";
output_txt.text+=msg;
}

]]>
</mx:Script>
<mx:Panel x="362" y="26" width="360" height="336" layout="absolute" title="输出信息" fontSize="12">
<mx:TextArea x="0" y="73" width="100%" height="213" id="output_txt" fontSize="12"/>
<mx:TextInput x="23" y="37" id="input_txt" text="输入信息" enter="sendMessage(input_txt.text)" fontSize="12"/>
<mx:Button x="246" y="37" label="发送" click="sendMessage(input_txt.text)" fontSize="12"/>

</mx:Panel>
<mx:Panel x="57" y="38" width="250" height="200" layout="absolute" title="登陆窗口" fontSize="12">
<mx:Label x="10" y="10" text="服务器地址" fontSize="12"/>
<mx:TextInput x="73" y="8" width="147" id="server_txt" text="127.0.0.1" fontSize="12"/>
<mx:Label x="10" y="51" text="端口" fontSize="12"/>
<mx:TextInput x="73" y="49" width="147" id="port_txt" text="8600" fontSize="12"/>
<mx:Button x="73" y="109" label="连接" click="doConnect()" fontSize="12"/>
</mx:Panel>
<mx:VSlider x="82" y="286" enabled="true"/>
</mx:Application>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值