【IT168技术文档】最近时间,其实很空闲,只是有一些事情还是没有做出来,所以感觉很郁闷。 这两天看pop3 和stmp的协议,看看flex能否开发一个邮件发送的程序。把最近写一些类记录下来,看看日后是否有用。
写一个connection 继承了 EventDispatcher 类,利用这个类可以进行事件分派。然后封装一下Socket ,可以进行对其类发送数据,和读取数据。
通过两个方法:
public function SendMessage(msg:String):void
public function ReadBytes(type:String="utf-8"):String
两个方法,进行对其收发信息,当然这种做法还是不最好的,只是对某一些情况下很实用,最好还是写一个接口。然后实现了他们吧。
package com.net
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.Socket;
import flash.utils.ByteArray;
import mx.controls.Alert;
public class connetion extends EventDispatcher
{
private var socket:Socket;
private var port:int=110;
private var host:String="pop.126.com";
public function connetion()
{
socket=new Socket();
socket.connect(host,port);
socket.addEventListener(Event.CONNECT,onConnect);
socket.addEventListener(IOErrorEvent.IO_ERROR,onError);
socket.addEventListener(ProgressEvent.SOCKET_DATA,onData);
socket.addEventListener(Event.CLOSE,onClose);
}
public function get MySocket():Socket
{
return socket;
}
public function set Host(name:String):void
{
host=name;
}
public function set Port(name:int):void
{
port=name;
}
private function onConnect(event:Event):void
{
Alert.show("链接成功");
}
private function onError(event:Event):void
{
Alert.show("失败错误");
}
private function onData(event:ProgressEvent):void
{
dispatchEvent(new Event("socketdata"));
}
private function onClose(event:Event):void
{
Alert.show("关闭了");
}
public function SendMessage(msg:String):void
{
var byte:ByteArray=new ByteArray();
byte.writeUTFBytes(msg+String.fromCharCode(13)+String.fromCharCode(10));
socket.writeBytes(byte);
socket.flush();
}
public function ReadBytes(type:String="utf-8"):String
{
return socket.readMultiByte(socket.bytesAvailable,type)
}
public function close():void
{
socket.close();
}
}
}
利用flex进行测试:其实就是一个简单的pop3信息收发,但是功能很薄弱,甚至程序还没有成型。 不过让我感觉兴奋的事情,那就是知道了socket的威力是如此的强大。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:TextInput id="username" x="123" y="122"/>
<mx:TextInput id="userpwd" x="123" y="163" displayAsPassword="true"/>
<mx:Label x="90" y="124" text="帐号"/>
<mx:Label x="90" y="165" text="密码"/>
<mx:Button x="291" y="163" label="登陆" width="68" click="login()"/>
<mx:TextArea id="textarea" x="123" y="242" width="446" height="275"/>
<mx:Label x="90" y="214" text="命令"/>
<mx:TextInput id="command" x="123" y="212"/>
<mx:Button x="291" y="212" label="发送" width="68" click="SendMessage()"/>
<mx:Script>
<!--[CDATA[
import com.net.connetion;
import mx.controls.Alert;
private var con:connetion;
private function init():void
{
con=new connetion();
con.addEventListener("socketdata",ondata);
}
private function ondata(event:Event):void
{
var msg:String="";
while(con.MySocket.bytesAvailable)
{
msg+=con.ReadBytes();
trace(msg);
}
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,'');//删除回车
textarea.text=arr[i]+"\n";
}
}
}
private function login():void
{
if(username.text!="" && userpwd.text!="")
{
trace(username.text);
con.SendMessage("USER "+username.text+"@126.com");
con.SendMessage("PASS "+userpwd.text);
}
else
{
Alert.show("用户或密码不能为空!");
}
}
private function SendMessage():void
{
con.SendMessage(command.text);
}
]]-->
</mx:Script>
<mx:Label x="291" y="124" text="@126.com"/>
</mx:Application>