有没有人知道为什么在我断开wifi连接后没有发送关闭事件?或者以任何方式确定连接是否处于活动状态。
答案 0 :(得分:3)
不确定是否需要隐含关闭它,但我会在关闭回调
_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' ) );
}
}
[编辑]
当连接丢失到服务器时,连接关闭事件将不会触发,如果要重新连接,则必须对此进行测试
为此,您需要设置一个计时器来轮询连接。下面的类将继续对插座进行限制,如果发现没有连接,它将尝试重新连接
我剥离并删除了发送功能和非相关信息
正如你所看到的那样,我有一个定时器可以对插座进行限制,并且在重试之后也会超时
如果它关闭,这个类将尝试让你的套接字保持活着状态
显然你需要分配端口和主机。
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' ) );
}
}
}
答案 1 :(得分:2)
来自docs:
Dispatched when the server closes the socket connection.
服务器通过发送FIN
数据包并等待ACK
来关闭连接。我看,你只是放下你的wifi,所以远程服务器没有机会发送FIN
。