AS3 - Flash/AIR Socket 通信 writeUTFBytes 只工作一次 [英] AS3 - Flash/AIR Socket Communication writeUTFBytes only works once

查看:40
本文介绍了AS3 - Flash/AIR Socket 通信 writeUTFBytes 只工作一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个套接字服务器监听 2 个端口,1 个端口用于套接字服务器,1 个端口用于策略服务器.

I have a socket server listening on 2 ports, 1 port for the socket server and 1 port for the policy server.

我的代码如下,在这种情况下,数据发送和接收都很好.但是,例如,如果我添加一个带有简单按钮的按钮:

My code is below, in this scenario, data is sent and received perfectly fine. however, for example if I add a button with a simple:

<代码>socket.writeUTFBytes("消息");socket.flush();

初始连接后,它似乎没有向我的服务器发送任何数据(我让我的服务器将所有数据传输打印到控制台进行检查)初始连接工作正常,如下所示:

after the initial connection, it doesn't seem to send any data to my server (I have my server printing all data transmissions to the console for checking) Initial connections work fine, as seen below:

<代码>//首先使用socket服务器进行身份验证:var xmlSocket = new XMLSocket();xmlSocket.connect("192.xx.xx.xx", 843);

//authenticate with socket server first: var xmlSocket = new XMLSocket(); xmlSocket.connect("192.xx.xx.xx", 843);

try {
    Security.loadPolicyFile("xmlsocket://192.xx.xx.xx:843");
} catch (e:IOError) {
    //tbOutput.text += e.text;
}


var socket:Socket = new Socket();
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(Event.CLOSE, onClose);
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);

try {
socket.connect("192.xx.xx.xx", 4444);
} catch (e:IOError) {
    //error traced
}


function onConnect(e:Event):void {
    //initial message to socket server:
    var Message:String;
    //message contains something
    socket.writeUTFBytes(Message.toString() + "<EOF>");
    socket.flush();

    }

推荐答案

你基本上是对的,只是错过了一些东西.
即使您指定了另一个端口,它也会为 crossdoamin 文件保存初始连接,它会首先尝试从 4444 获取文件,如果失败,则它将通过默认端口 (843) 转到主服务器.
这是您的违规行

You basically have it right you just missed something.
The initial connection is saved for the crossdoamin file even is you specify another port it will try to get the file from 4444 first and if that fails then it will go for the master on the default port(843).
Here is your offending line

Security.loadPolicyFile("xmlsocket://192.xx.xx.xx:843");

如果您查看您的服务器日志,您会发现在建立第一次连接时,向服务器发出了 形式的请求.
一旦该请求完成并将文件传递给客户端,则客户端将始终关闭连接.
在客户端收到跨域并关闭连接后,您可以重新连接并随意发送.

If you look at your server logs you will find that when the first connection was made there was a request in the form of <policy-file-request/> made to the server.
Once that request is completed and the file delivered to the client then the connection will be closed by the client always.
On the client side once the crossdomain is received and the connection closed you can then reconnect and send at will.


总结一下.
您的 Flash 应用已连接到服务器.
然后你的应用请求跨域并坐等
在连接超时之前,您继续通过连接发送某种数据
仍在等待跨域的应用收到您服务器的响应
由于从服务器收到的数据不是 crossdomain.xml 文件,因此您的应用程序关闭了连接并且不允许重新连接


So to recap.
Your flash app made a connection to the server.
Then your app requested the crossdomain and sat and waited
Before the connection timed out you then proceeded to send data of some sort over your the connection
The app still waiting for the cross domain received a response from your server
Since the data received from the server is not the crossdomain.xml file your app closed the connection and will not allow reconnects

我稍微更改了您的代码以自动重新连接
但是,当有请求时,端口 4444 上的服务器应该返回跨域文件.

I changed your code a bit to auto reconnect
However your server on port 4444 should return the crossdomain file when there is a request for it.

try {
  Security.allowDomain('192.xx.xx.xx');
  Security.loadPolicyFile("xmlsocket://192.xx.xx.xx:4444");
} catch (e:IOError) {
    //tbOutput.text += e.text;
}


var socket:Socket = new Socket();
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(Event.CLOSE, onClose);
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);

function connect( ){
  if( !socket.connected ){
    try {
      socket.connect("192.xx.xx.xx", 4444);
    } catch (e:IOError) {
      //error traced
    }
  }
}

function onConnect(e:Event):void {
    //initial message to socket server:
    var Message:String;
    //message contains something
    //socket.writeUTFBytes(Message.toString() + "<EOF>");// EOF bad
    socket.writeUTFBytes(Message.toString() + String.fromCharCode(0) ); // NULL good
    socket.flush();

}

function onResponse(e:ProgressEvent):void {
  var read:String = this.readUTFBytes(this.bytesAvailable );

  // I test for a < since my server will never return a "<" as the first character
  // unless it is the crossdomain.xml file
  // you may need to change this for your needs
  if( read.charAt(0) !='<' ){
    if( read ){
      // so something with your response
    }
  }else{
    // recieved crossdomain policy nothing to really do here it is handled internally
  }
}


var connectTimer:Timer = new Timer( 1000 );
connectTimer.addEventListener(TimerEvent.TIMER, connect );
connectTimer.start();

不要忘记要使您的服务器在端口 4444 上工作需要返回该端口上的跨域文件

Don't forget for this to work your server on port 4444 needs to return the crossdomain file on that port

这篇关于AS3 - Flash/AIR Socket 通信 writeUTFBytes 只工作一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆