1

我遇到了套接字问题,我发送的数据在客户端(Flash/Flex)被截断。我的服务器发送的数据已满且完好无损,但 flash 不会等待适当的时间,然后触发一个表示读取数据的事件。这导致数据没有被完全读取,并且(因此)之后我无法解析出对象。我们正在探索将对象剥离成更小的块的可能性,但我们的想法是让它尽可能轻量级,因此我们宁愿让它按预期的方式工作,而不是修补临时解决方案。

我不完全确定将其称为接收“缓冲区”是否正确,因为某些计算机能够接收所有数据而其他计算机则将其截断(这意味着缓冲区足够但闪存不等待无论出于何种原因,适当的时间安排活动)......如果有人有任何想法,我将不胜感激!

4

1 回答 1

2

如果没有看到您的实现代码,听起来好像可用的字节数或总字节数没有被考虑在内。

这是我使用的一个实现:

package
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.events.SecurityErrorEvent;
    import flash.net.Socket;

    public class WebRequestService extends EventDispatcher
    {

        //------------------------------
        //  model
        //------------------------------

        protected var hostname:String;

        protected var port:uint;

        public var requestMessage:String;

        public var responseMessage:String;

        private var _socket:Socket;


        //------------------------------
        //  lifecycle
        //------------------------------

        public function WebRequestService(hostname:String, port:uint, requestMessage:String)
        {
            this.hostname = hostname;
            this.port = port;
            this.requestMessage = requestMessage;

            _socket = new Socket();
            _socket.addEventListener(Event.CONNECT, socketConnectHandler);
            _socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
            _socket.addEventListener(Event.CLOSE, socketCloseHandler);
            _socket.addEventListener(IOErrorEvent.IO_ERROR, socketErrorHandler);
            _socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketSecurityErrorHandler);

            _socket.connect(hostname, port);
        }

        protected function socketConnectHandler(event:Event):void
        {
            responseMessage = "";
            _socket.writeUTFBytes(requestMessage);
            _socket.flush();
        }

        protected function socketDataHandler(event:ProgressEvent):void
        {
            while (_socket.bytesAvailable > 4)
            {
                responseMessage += _socket.readUTFBytes(_socket.bytesAvailable);
            }
        }

        protected function socketCloseHandler(event:Event):void
        {
            dispose();
        }

        protected function socketErrorHandler(event:IOErrorEvent):void
        {
            trace("socket error.");
        }

        protected function socketSecurityErrorHandler(event:SecurityErrorEvent):void
        {
            trace("socket security error.");
        }

        public function dispose():void
        {
            if (!_socket)
                return;

            if (_socket.connected)
                _socket.close();

            _socket.removeEventListener(Event.CONNECT, socketConnectHandler);
            _socket.removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
            _socket.removeEventListener(Event.CLOSE, socketCloseHandler);
            _socket.removeEventListener(IOErrorEvent.IO_ERROR, socketErrorHandler);
            _socket.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, socketSecurityErrorHandler);
        }

    }
}
于 2012-04-05T18:12:31.410 回答