【问题标题】:Flex Sockets not sending data on WindowsFlex Sockets 不在 Windows 上发送数据
【发布时间】:2011-04-13 15:55:23
【问题描述】:

我真的希望有人能对此有所了解。我正在使用 PHP 和 Flex 构建一个非常简单的聊天服务器/客户端。我学习了很多教程,并且在 PHP 中有一个工作服务器,在 Flex 中有一个工作客户端,除了如果我在 Windows 上使用客户端,我无法发送任何消息。

当我在我的 Mac 上发送消息时,它会通过服务器并发送到所有客户端,包括我可能连接的任何 Windows 客户端。该消息实际上也会显示在 Windows 客户端上,只是当我尝试从 Windows 发送时它不起作用。

我尝试添加一个由 PHP 脚本在正确端口上侦听的 crossdomain.xml 文件,但似乎没有一个客户端要求它,如果客户端在 Mac 上运行,我认为它应该在 Windows 上运行.顺便说一句,我将项目导出为 AIR 文件(不知道这是否会有所不同)。

我找不到任何关于此的信息,所以我想知道我是否在某个地方很愚蠢,或者我是否需要对 Windows 客户端采取一些特殊措施?

我在下面粘贴了我的客户端代码(我从未在 Stack Overflow 上发过帖子,因此我为缺少代码格式而道歉,如果可能的话,有人可以解释如何解决它吗?)。

    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.MouseEvent;
    import flash.events.ProgressEvent;
    import flash.events.SecurityErrorEvent;
    import flash.net.Socket;

    public var socket:Socket;

    // Run on windowComplete
    protected function init():void {
      btnConnect.addEventListener(MouseEvent.CLICK, onBtnConnectClick);
    }

    // Run on closing
    protected function deinit():void {
      if ( (socket != null) && (socket.connected) ) {
        socket.close();
      }
    }

    // Called when the "Connect" button is clicked
    protected function onBtnConnectClick(e:MouseEvent):void {
      if (txtServer.text != '') {
        socket = new Socket();

        socket.addEventListener(Event.CONNECT, onSocketConnect);
        socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketDataProgress);
        socket.addEventListener(IOErrorEvent.IO_ERROR, onSocketIOError);
        socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSocketSecurityError);

        socket.connect(txtServer.text, new uint(txtPort.text));
        btnSend.addEventListener(MouseEvent.CLICK, onBtnSendClick);
      }
    }

    // Called when the "Send" button is clicked
    protected function onBtnSendClick(e:MouseEvent):void {
      if (txtChatMessage.text != '') {
        txtChatWindow.text += 'You: '+ txtChatMessage.text +"\n";
        socket.writeUTFBytes(txtUsername.text +': '+ txtChatMessage.text);
      }

      txtChatMessage.text = '';
    }

    // Called when the socket is connected
    protected function onSocketConnect(e:Event):void {
      txtChatWindow.text += 'Connected\n\n';
      btnConnect.label = "Disconnect";

      btnConnect.removeEventListener(MouseEvent.CLICK, onBtnConnectClick);
      btnConnect.addEventListener(MouseEvent.CLICK, onSocketDisconnect);
    }

    // Called when the socket recieves data
    protected function onSocketDataProgress(e:ProgressEvent):void {
      var data:String = socket.readUTFBytes(socket.bytesAvailable);

      txtChatWindow.text += data +"\n";
    }

    // Called when there is an IO Error on the socket
    protected function onSocketIOError(e:IOErrorEvent):void {
      txtChatWindow.text += e.text;
    }

    // Called when there is a security error on the socket
    protected function onSocketSecurityError(e:SecurityErrorEvent):void {
      txtChatWindow.text += e.text;
    }

    // Called when the "Disconnect" button is clicked
    protected function onSocketDisconnect(e:MouseEvent):void {
      if ( (socket != null) && (socket.connected) ) {
        socket.close();
      }

      btnConnect.label = "Connect";
      btnConnect.addEventListener(MouseEvent.CLICK, onBtnConnectClick);
    }

【问题讨论】:

  • 与您的问题没有直接关系,但是您是否尝试过使用weborb for php?使用套接字从头开始实现您的消息传递系统是一项艰巨的任务。

标签: php windows apache-flex sockets client-server


【解决方案1】:

啊哈!!我想到了。经过大量搜索后,我注意到一条小评论证实了我的怀疑,即 Windows 需要一些额外的代码,确切地说是一行。

在 Mac 和 Linux 中,flush() 在执行帧之间被隐式调用,但是,在 Windows 上,除非您调用 flush(),否则永远不会发送数据。

所以 onBtnSendClick 函数现在看起来像这样:

protected function onBtnSendClick(e:MouseEvent):void {
  if (txtChatMessage.text != '') {
    txtChatWindow.text += 'You: '+ txtChatMessage.text +"\n";
    socket.writeUTFBytes(txtUsername.text +': '+ txtChatMessage.text);
    socket.flush();
  }

  txtChatMessage.text = '';
}

【讨论】:

    猜你喜欢
    • 2019-07-10
    • 2014-07-08
    • 1970-01-01
    • 2014-09-30
    • 2011-08-06
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多