Flash as a gateway to javascript

I just finished ajax / php based web chat application. But the problem with this application is that it must constantly poll the server to check for new messages, which in turn overloads the server if many people use this application at the same time. now I want to implement a socket based chat application in JavaScript. I know there is no socket support in JavaScript, so I decided to use " Flash as a socket gateway for JavaScript ". I am using Linux and new to flash. can someone help me with how to achieve this.

basically, 1) I want to create a small SWF object that just handles the socket logic (min width and height, so I can hide it with the -ve field. 2) I want to access this SWF object using JavaScript

I have some code for a simple socket in actionscript (from the internet) but I cannot compile it with mxmlc (free flash compiler). heres the code ...

    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();
}

      

I have better code but it also doesn't compile

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?");
}

      

MISTAKE:

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


source to share


2 answers


You might want to check out gimite web-socket-js . It is a socket gateway that corresponds to a WebSocket API pending process , so in the future when browsers implement a native WebSocket it will automatically switch to a non-Flash alternative.



+6


source


The following code is outside the class

and blocks package

{}

. This is not permitted.

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?");
}

      



Declare the document class (which extends Sprite

) and move the methods ioError

and connected

. Make the sock an instance variable instead of a local variable and add the declaration part sock

to your constructor.

//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?");
    }
  }
}

      

0


source







All Articles