views:

628

answers:

2

hi everyone, i just completed a web based chat application based on ajax/php. But the problem with this app is that it has to continuously poll server to check for new messages, which in turn overloads the server if many people are using this app simultaneously. now i want to implement a socket based chat app in JavaScript. I know there is no support for sockets in JavaScript so i decided to use "Flash as a socket gateway for JavaScript" i am using Linux and and new to flash. can someone help me with how to achieve this.

basically, 1) I want to make a small SWF object that just handles socket logic(minimum width and height so i can hide it easily with -ve margin. 2) I want to access this swf object with JavaScript

i got a code for simple socket in actionscript (from internet) but i cannot get it to compile using 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 got a better code but this also does not 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?");
}

ERROR IS:

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
A: 

The following code lies outside the class and package {} blocks. That is not allowed.

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 a document class (that extends Sprite) and move ioError and connected methods to it. Make sock an instance variable instead of a local variable and add the declaration part of sock into its 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?");
    }
  }
}
Amarghosh
thank for the help... can you tell me if its possible to create a small swf(that will not show on front-end) file which will just handle socket logic and could i access it using javascript ...thanks in advance
Pragati Sureka
+3  A: 

You may wish to check out gimite's web-socket-js. This is a socket gateway that conforms to the work-in-progress Web Socket API, so in future as browsers implement native WebSocket it will automatically switch over to the Flash-free alternative.

bobince
thanks for the links.... very helpful
Pragati Sureka
That's exactly what I was looking for. Thanks!
Rene Saarsoo