gpt4 book ai didi

actionscript-3 - ActionScript中的套接字连接关闭事件

转载 作者:行者123 更新时间:2023-12-03 11:56:57 27 4
gpt4 key购买 nike

有人知道我断开wifi连接后为什么未调度关闭事件的原因吗?或通过任何方法确定连接是否处于 Activity 状态。

最佳答案

不确定您是否需要隐式关闭它,但我在close回调上做了

_socket.addEventListener(Event.CLOSE, onClose);

private function onClose(e:Event):void {
_socket.close();
}

您还可以在连接期间进行测试,以确保它尚未连接
if( !_socket.connected ){
try {
_socket.connect(host, port);
this.dispatchEvent( new Event( 'CONNECTING' ) );
} catch (e:Error) {
_socket.close();
this.dispatchEvent( new Event( 'ERROR' ) );
}
}

[编辑]
当与服务器的连接丢失时,将不会触发连接关闭事件,如果要重新连接,则必须对此进行测试。
为此,您需要设置一个计时器以轮询连接。下一课将继续为 socket 供电,如果未连接,它将尝试重新连接。

我剥离并删除了发送功能和非相对信息。
如您所见,我有一个计时器,它可以插在 socket 上,并且在经过多次重试后也会超时。
如果关闭,此类将尝试使您的套接字保持 Activity 状态。
显然,您需要分配端口和主机。
package{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.events.TimerEvent;
import flash.net.Socket;
import flash.system.Security;
import flash.utils.Timer;

public class MySocket extends Socket {
[Event(name="ERROR")]
[Event(name="CONNECTED")]
[Event(name="DISCONNECTED")]
[Event(name="CONNECTING")]
[Event(name="TIMIEDOUT")]

private var host:String;
private var port:Number;
private var connectTimer:Timer;
private var retryCount:int;

public var err:String;

public function MySocket( ){
this.retryCount = 0
Security.allowDomain(this.host);
Security.loadPolicyFile("xmlsocket://"+this.host+":"+this.port);
this.addEventListener(ProgressEvent.SOCKET_DATA, this.onResponse);
this.addEventListener(Event.CONNECT, this.onConnect);
this.addEventListener(Event.CLOSE, this.onClose);
this.addEventListener(IOErrorEvent.IO_ERROR, this.onIOError);
this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError);
this.dispatchEvent( new Event( 'DISCONNECTED' ) );
this.err = '';
this.connectTimer = new Timer( 1000 );
this.connectTimer.addEventListener(TimerEvent.TIMER, this.myConnect );
this.connectTimer.start();
}

private function onResponse(e:ProgressEvent):void {
var read:String = this.readUTFBytes(this.bytesAvailable );
if( read.charAt(0) !='<' ){
if( read ){
// the server response here
}
}else{
// recieved crossdomain policy do nothing
}
}
public function myConnect( e:TimerEvent ):void{
if( !this.connected ){
try {
this.connect(this.host, this.port);
this.dispatchEvent( new Event( 'CONNECTING' ) );
} catch (e:Error) {
this.close();
this.err = e.message;
this.dispatchEvent( new Event( 'ERROR' ) );
}
}
}
private function onConnect(e:Event):void {
this.retryCount = 0
this.err = '';
this.dispatchEvent( new Event( 'CONNECTED' ) );
}
private function onClose(e:Event):void {
this.close();
this.dispatchEvent( new Event( 'DISCONNECTED' ) );
}
private function onIOError(e:IOErrorEvent):void {
++this.retryCount;
if( this.retryCount >= 12 ){
this.connectTimer.stop();
this.dispatchEvent( new Event( 'TIMIEDOUT' ) );
}else{
this.err = 'IO-ERROR-EVENT - ' + e.text + '\r\nAttempting to reconnect';
}
}
private function onSecurityError(e:SecurityErrorEvent):void {
this.err = 'SECURITY-ERROR - ' + e.text;
this.dispatchEvent( new Event( 'ERROR' ) );
}
}
}

关于actionscript-3 - ActionScript中的套接字连接关闭事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6729600/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com