AS3 - 闪光/ AIR socket通信writeUTFBytes只能一次 [英] AS3 - Flash/AIR Socket Communication writeUTFBytes only works once

查看:242
本文介绍了AS3 - 闪光/ 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.

我的code是下面,在这种情况下,数据的发送和接收完全没有问题。然而,例如,如果我加用一个简单的按钮:

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();

socket.writeUTFBytes("Message"); 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 =新的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");

如果你看一下你的服务器日志,你会发现,被做了第一次连接时出现在&LT的形式要求;政策性文件的请求/&GT; 向服务器发出的。
一旦该请求已完成,该文件传送到客户端则连接将被客户端总是封闭。
在客户端一旦跨域收到并关闭连接,那么你可以重新连接并发送的意愿。

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

我改变了你的codeA位自动重连
但是在服务器上的端口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 - 闪光/ AIR socket通信writeUTFBytes只能一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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