当前位置:  开发笔记 > 编程语言 > 正文

Flash作为javascript的套接字网关

如何解决《Flash作为javascript的套接字网关》经验,为你挑选了1个好方法。

我刚刚完成了基于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

bobince.. 6

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



1> bobince..:

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

推荐阅读
kikokikolove
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有