AS3 WebSocket库不更新发送消息的readyState

时间:2016-09-03 10:16:36

标签: node.js actionscript-3 flash websocket

我有一个AS3 Websocket客户端,可以成功连接到node-js服务器。

在客户端上,WebSocket类具有以下属性:

  private static const CONNECTING:int = 0;
  private static const OPEN:int = 1;
  private static const CLOSING:int = 2;
  private static const CLOSED:int = 3;

  private var readyState:int = CONNECTING;

当我与服务器建立连接时,客户端触发函数“onSocketData()”(此函数在WebSocket类中)并且必须将参数“readyState”的值更改为“1”,具有此值客户端可以向服务器发送消息。

问题:

函数“onSocketData()”的过程永远不会改变“readyState”的值。

这是onSocketData()函数的代码:

 private function onSocketData(event:ProgressEvent):void {
	  trace(readyState);
    var pos:int = buffer.length;
    socket.readBytes(buffer, pos);
	
    for (; pos < buffer.length; ++pos) {
		trace(buffer.length + "   pos-->" + pos + "  headerState -->" + headerState+"  buferpos -->"+buffer[pos]);
		
      if (headerState < 4) {
        // try to find "\r\n\r\n"
		
        if ((headerState == 0 || headerState == 2) && buffer[pos] == 0x0d) {//this sentence is never fulfilled
          ++headerState;
        } else if ((headerState == 1 || headerState == 3) && buffer[pos] == 0x0a) {//this sentence is never fulfilled
          ++headerState;
		  
        } else {
			
          headerState = 0;
        }
        if (headerState == 4) {
			
          var headerStr:String = readUTFBytes(buffer, 0, pos + 1);
          trace("response header:\n" + headerStr);
          if (!validateHandshake(headerStr)) return;
          removeBufferBefore(pos + 1);
          pos = -1;
          readyState = OPEN;// here must be changue the value
          this.dispatchEvent(new WebSocketEvent("open"));
        }
      } else {
		 
        var frame:WebSocketFrame = parseFrame();
        if (frame) {
		  
		  removeBufferBefore(frame.length);
          pos = -1;
          if (frame.rsv != 0) {
            close(1002, "RSV must be 0.");
          } else if (frame.mask) {
            close(1002, "Frame from server must not be masked.");
          } else if (frame.opcode >= 0x08 && frame.opcode <= 0x0f && frame.payload.length >= 126) {
            close(1004, "Payload of control frame must be less than 126 bytes.");
          } else {
            switch (frame.opcode) {
              case OPCODE_CONTINUATION:
                if (fragmentsBuffer == null) {
                  close(1002, "Unexpected continuation frame");
                } else {
                  fragmentsBuffer.writeBytes(frame.payload);
                  if (frame.fin) {
                    data = readUTFBytes(fragmentsBuffer, 0, fragmentsBuffer.length);
                    try {
                      this.dispatchEvent(new WebSocketEvent("message", encodeURIComponent(data)));
                    } catch (ex:URIError) {
                      close(1007, "URIError while encoding the received data.");
                    }
                    fragmentsBuffer = null;
                  }
                }
                break;
              case OPCODE_TEXT:
                if (frame.fin) {
                var data:String = readUTFBytes(frame.payload, 0, frame.payload.length);
                try {
                  this.dispatchEvent(new WebSocketEvent("message", encodeURIComponent(data)));
                } catch (ex:URIError) {
                  close(1007, "URIError while encoding the received data.");
                }
                } else {
                  fragmentsBuffer = new ByteArray();
                  fragmentsBuffer.writeBytes(frame.payload);
                }
                break;
              case OPCODE_BINARY:
                // See https://github.com/gimite/web-socket-js/pull/89
                // for discussion about supporting binary data.
                close(1003, "Received binary data, which is not supported.");
                break;
              case OPCODE_CLOSE:
                // Extracts code and reason string.
                var code:int = STATUS_NO_CODE;
                var reason:String = "";
                if (frame.payload.length >= 2) {
                  frame.payload.endian = Endian.BIG_ENDIAN;
                  frame.payload.position = 0;
                  code = frame.payload.readUnsignedShort();
                  reason = readUTFBytes(frame.payload, 2, frame.payload.length - 2);
                }
                trace("received closing frame");
                close(code, reason, "server");
                break;
              case OPCODE_PING:
                sendPong(frame.payload);
                break;
              case OPCODE_PONG:
                break;
              default:
                close(1002, "Received unknown opcode: " + frame.opcode);
                break;
            }
          }
        }
      }
    }
  }

句子:

“if((headerState == 0 || headerState == 2)&amp;&amp; buffer [pos] == 0x0d)”

“if((headerState == 1 || headerState == 3)&amp;&amp; buffer [pos] == 0x0a)”

永远不会实现,所以参数“headerState”永远不会添加+1,然后是句子

“if(headerState == 4)”

永远不会实现,readyState永远不会取“1”(1表示开放式连接)

你可以帮我解决这个问题吗?如果我的解释错了,你能告诉我为了使这段代码有效吗?

注意:当我强制readyState的changue为1时,消息被释放,服务器可以读取它,但是当我发送4个或更多消息时,服务器给我一个溢出错误。

0 个答案:

没有答案