[ActionScript 3.0] AS3 socket示例(官方示例)
下例对套接字执行读写操作,并输出在套接字事件期间传输的信息。 该示例的要点遵循:
- 该构造函数创建名为
socket的CustomSocket实例,并将主机名localhost和端口 80 作为参数传递。 由于CustomSocket扩展了 Socket,因此在调用super()时将调用 Socket 的构造函数。 - 然后,该示例调用了
configureListeners()方法,该方法可为 Socket 事件添加侦听器。 - 最后,调用套接字
connect()方法,其中使用localhost作为主机名并使用 80 作为端口号。
注意:若要运行该示例,您需要一台与 SWF 位于同一个域(在本例中为 localhost)服务器并侦听端口 80。
package
{
import flash.display.Sprite; /**
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @create 2015-8-21 下午2:53:03
*
*/
public class SocketExample extends Sprite
{
public function SocketExample()
{
var socket:CustomSocket = new CustomSocket("localhost", 80); }
}
}
import flash.errors.*;
import flash.events.*;
import flash.net.Socket; class CustomSocket extends Socket {
private var response:String; public function CustomSocket(host:String = null, port:uint = 0) {
super();
configureListeners();
if (host && port) {
super.connect(host, port);
}
} private function configureListeners():void {
addEventListener(Event.CLOSE, closeHandler);
addEventListener(Event.CONNECT, connectHandler);
addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
} private function writeln(str:String):void {
str += "\n";
try {
writeUTFBytes(str);
}
catch(e:IOError) {
trace(e);
}
} private function sendRequest():void {
trace("sendRequest");
response = "";
writeln("GET /");
flush();
} private function readResponse():void {
var str:String = readUTFBytes(bytesAvailable);
response += str;
} private function closeHandler(event:Event):void {
trace("closeHandler: " + event);
trace(response.toString());
} private function connectHandler(event:Event):void {
trace("connectHandler: " + event);
sendRequest();
} private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
} private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
} private function socketDataHandler(event:ProgressEvent):void {
trace("socketDataHandler: " + event);
readResponse();
}
}
[ActionScript 3.0] AS3 socket示例(官方示例)的更多相关文章
- [ActionScript 3.0] AS3 Socket安全沙箱策略文件
当与一个主机建立一个Socket连接时,Flash Player要遵守如下安全沙箱规则: 1.Flash的.swf文件和主机必须严格的在同一个域名,只有这样才可以成功建立连接: 2.一个从网上发布的. ...
- [ActionScript 3.0] as3处理xml的功能和遍历节点
as3比as2处理xml的功能增强了N倍,获取或遍历节点非常之方便,类似于json对像的处理方式. XML 的一个强大功能是它能够通过文本字符的线性字符串提供复杂的嵌套数据.将数据加载到 XML 对象 ...
- [ActionScript 3.0] AS3.0 动态加载显示内容
可以将下列任何外部显示资源加载到 ActionScript 3.0 应用程序中: 在 ActionScript 3.0 中创作的 SWF 文件 — 此文件可以是 Sprite.MovieClip 或扩 ...
- [ActionScript 3.0] AS3 ServerSocket示例(官方示例)
下面的示例创建一个套接字服务器.要使用该服务器,可将套接字绑定到本地端口,然后从其他应用程序连接到该端口.该服务器仅识别 UTF-8 字符串. package { import flash.displ ...
- [ActionScript 3.0] AS3.0 简单封装Socket的通信
Socket服务器 package com.controls.socket { import com.models.events.AppEvent; import com.models.events. ...
- [ActionScript 3.0] AS3.0 Socket通信实例
以下类是充当Socket服务器的例子 package { import flash.display.Sprite; import flash.events.Event; import flash.ev ...
- [ActionScript 3.0] AS3.0 本机鼠标指针
Flash Player 10.2添加了内置的本机鼠标指针(native mouse cursor)支持,虽然在之前的版本里我们可以侦听MouseEvent事件来模拟鼠标指针,但是在有了原生的本机鼠标 ...
- [ActionScript 3.0] AS3 深入理解Flash的安全沙箱Security Domains
简介 如果你还没有与复杂的的安全域(security domain)和应用程序域(application domain)问题打过交道,那么你真是个幸运的家伙.当你在加载外部内容(然后他们开始播放)的时 ...
- [ActionScript 3.0] AS3调用百度天气预报查询API
接口说明 根据经纬度/城市名查询天气的结果 接口示例 http://api.map.baidu.com/telematics/v3/weather?location=成都&output=jso ...
随机推荐
- ShaderLab内置变量
[ShaderLab内置变量]
- Git回滚到历史节点(SourceTree篇)
转自:http://blog.csdn.net/u010416101/article/details/78142697.https://www.zhihu.com/question/48178380 ...
- Git blame
一.简介 git blame可以将文件中的每一行的作者.最新的变更提交和提交时间展示出来. 二.实例 http://blog.csdn.net/hudashi/article/details/76 ...
- Linux发送邮件
以下是自己收集的实用Linux下简单配置外部邮箱发送邮件的方法: 1.配置/etc/mail.rc,使用mail命令 # vim /etc/mail.rc ###调用外部邮箱 set from=t ...
- Maven远程发布项目到tomcat
向tomcat发布项目,每次都要打包传送再运行,非常麻烦.偶然一天发现maven有插件可以直接发布到tomcat.今天把大体过程介绍给大家. 首先在pom中配置tomcat插件: <plugin ...
- Spring下集成ActiveMQ推送
本文是将ActiveMQ消息制造者集成进spring,通过spring后台推送消息的实现. 首先是spring的applicationContext的配置,如下 <?xml version=&q ...
- Windows下如何安装MongoDB
下载地址: http://www.mongodb.org/downloads 我下载的是:mongodb-win32-x86_64-2008plus-2.6.6 解压到:D:\soft 同时在该目录下 ...
- Lua的文件IO操作
Lua 标准库 - 输入输出处理(input and output facilities) 转载于:http://blog.csdn.net/duanxuyun 文本Tag: Lua [IT168 技 ...
- 解决jeesite开发java.lang.String cannot be cast to com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm$Principal问题
解决jeesite问题java.lang.String cannot be cast to SystemAuthorizingRealm问题 这些天在jeesite项目上进行二次开发,遇到许多莫名其妙 ...
- Paint的ColorFilter
一.简介 setColorFilter(ColorFilter filter) 设置颜色过滤,这个方法需要我们传入一个ColorFilter参数同样也会返回一个ColorFilter实例.我们在set ...