Flash作为javascript的套接字网关

时间:2009-10-23 10:01:05

标签: javascript flash sockets

我刚刚完成了基于ajax / php的基于Web的聊天应用程序。但这个应用程序的问题是它必须不断轮询服务器以检查新消息,如果许多人同时使用此应用程序,这反过来会使服务器超载。    现在我想在JavaScript中实现基于套接字的聊天应用程序。我知道JavaScript中没有对套接字的支持所以我决定使用“ Flash作为JavaScript的套接字网关”我正在使用Linux和新的flash。有人可以帮我解决这个问题。

基本上, 1)我想创建一个只处理套接字逻辑的小SWF对象(最小宽度和高度,所以我可以使用-ve margin轻松隐藏它。 2)我想用JavaScript访问这个swf对象

我在actionscript(来自互联网)中获得了简单套接字的代码,但我无法使用mxmlc(免费的Flash编译器)进行编译。 继承代码...

    myXML = new XMLSocket; 
    myXML.onConnect = handleConnect; 
    myXML.onXML = handleXML; 
    myXML.onClose = handleDisconnect; 
    myXML.connect("http://www.yourServer.com", 12345); 
    function handleConnect(connectionStatus){
    connectionStatus ? trace("Connected.") : trace("Connection failed.");
    }
    function handleXML(xmlObject){
    trace("Object recieved:: "+xmlObject);
    }
function sendXML(textToSend){
myXML.send(new XML('"+textToSend+""));
}
function handleDisconnect(){
trace("Connection lost.");
}
function closeConnection(){
trace("Closing connection to server.");
myXML.close();
}

我有一个更好的代码,但这也没有编译

package
{
import flash.errors.*;
import flash.events.*;
import flash.net.Socket;

public class ChatSocket extends Socket
{
  public var host:String;
  public var port:uint;
  private var socket:Socket;
  public static var SOCK_CONNECTED:String = "onSockConnect";
  public static var SOCK_IOERROR:String = "onSockIOError";

  function ChatSocket(h:String, p:uint)
  {
    host = h;
    port = p;
    socket = this;
    super(host, port);
    initListeners();
  }
  public function sendMessage(str:String):void
  {
    if(connected)
    {
      socket.writeUTFBytes(str + "\n");
    }
    else
    {
      trace("Not connected, message not sent!");
    }
  }
  public function readMessage():void
  {
    if(connected)
    {
      var str:String = socket.readUTFBytes(socket.bytesAvailable);
      trace("Socket Server Response: " + str);
    }
     else
     {
       trace("No message read, not connected!");
     }
  }
  private function initListeners():void
  {
     socket.addEventListener(Event.CLOSE, closeHandler);
     socket.addEventListener(Event.CONNECT, connectHandler);
     socket.addEventListener(IOErrorEvent.IO_ERROR,
    ioErrorHandler);
  }
  private function closeHandler(event:Event):void
  {
     trace("Connection to [" + host + "] closed");
  }
  private function ioErrorHandler(event:IOErrorEvent):void
  {
     dispatchEvent(new Event(SOCK_IOERROR));
  }
  private function connectHandler(event:Event):void
  {
     trace("Connected to [" + host + "]");
     dispatchEvent(new Event(SOCK_CONNECTED));
  }
  private function socketDataHandler(event:ProgressEvent):void
  {
     readMessage();
  }
}

}

var sock:ChatSocket;
sock = new ChatSocket('127.0.0.1', 9990);
sock.addEventListener(ChatSocket.SOCK_CONNECTED, connected);
sock.addEventListener(ChatSocket.SOCK_IOERROR, ioError);
function ioError(e:Event):void
{
  trace("Cant connect to " + sock.host + " on port " + sock.port);
}
function connected(e:Event):void
{
  sock.sendMessage("are you hungry?");
}

错误是:

localhost bin]$ ./mxmlc ChatSocket.as
Loading configuration file /home/lk/Documents/flex_sdk_3.4/frameworks/flex-config.xml
/home/lk/Documents/flex_sdk_3.4/bin/ChatSocket.as: Error: A file found in a source-path can not have more than one externally visible definition. ChatSocket;sock;ioError;connected

2 个答案:

答案 0 :(得分:6)

您可以查看gimite的web-socket-js。这是一个符合正在进行的工作Web Socket API的套接字网关,因此将来随着浏览器实现本机WebSocket,它将自动切换到无Flash替代方案。

答案 1 :(得分:0)

以下代码位于classpackage {}块之外。这是不允许的。

var sock:ChatSocket;
sock = new ChatSocket('127.0.0.1', 9990);
sock.addEventListener(ChatSocket.SOCK_CONNECTED, connected);
sock.addEventListener(ChatSocket.SOCK_IOERROR, ioError);
function ioError(e:Event):void
{
  trace("Cant connect to " + sock.host + " on port " + sock.port);
}
function connected(e:Event):void
{
  sock.sendMessage("are you hungry?");
}

声明一个文档类(扩展Sprite)并将ioErrorconnected方法移动到它。使sock成为实例变量而不是局部变量,并将sock的声明部分添加到其构造函数中。

//DocClass.as
package
{
  public class DocClass
  {
    private var sock:ChatSocket;
    public function DocClass()
    {
      sock = new ChatSocket('127.0.0.1', 9990);
      sock.addEventListener(ChatSocket.SOCK_CONNECTED, connected);
      sock.addEventListener(ChatSocket.SOCK_IOERROR, ioError);
    }
    private function ioError(e:Event):void
    {
      trace("Cant connect to " + sock.host + " on port " + sock.port);
    }
    private function connected(e:Event):void
    {
      sock.sendMessage("are you hungry?");
    }
  }
}