pablo.meier
pablo.meier

Reputation: 2359

Actionscript Socket Events not firing?

I'm writing a SWF that I'd like to communicate with a Java process via Sockets. This is usually quite easy with standard Sockets, but for some reason the events described in the Socket documentation aren't firing when all signs say they should be.

On the Java side, I've set up a ServerSocket that's listening on port 8080. Using netcat I've confirmed it works as designed.

On the Flash side, however, I'm setting up per the examples in the docs:

public function connectToPort(port : int):void
{   
    m_socket = new Socket();

    addEventListener(Event.CLOSE, onClose);
    addEventListener(Event.CONNECT, onConnect);
    addEventListener(IOErrorEvent.IO_ERROR, onIoError);
    addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
    addEventListener(ProgressEvent.SOCKET_DATA, onData);

    m_socket.connect("localhost", port);

    // trace() doens't work for the command-line :(
    m_debug.text = "Called connect!";
}

When I run the resulting SWF, all I get is "Called connect!" on the stage, and none of the events ever fire. Even more strangely, when I investigate the communication from the ServerSocket on the Java end, it receives and accepts a connection. When I close the SWF the code calling my Server completes as normal -- meaning it was hanging on a connection made with my SWF.

I'm left with a few questions...

Any help would be appreciated, I've spent most of my day on this and I'm very frustrated that I can't get basic socket communication to work :-/

Upvotes: 1

Views: 970

Answers (2)

pho
pho

Reputation: 25500

These lines will add the event listeners to this, not m_socket

addEventListener(Event.CLOSE, onClose);
addEventListener(Event.CONNECT, onConnect);
addEventListener(IOErrorEvent.IO_ERROR, onIoError);
addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
addEventListener(ProgressEvent.SOCKET_DATA, onData);

To add them to m_socket use

with(m_socket) {
    addEventListener(Event.CLOSE, onClose);
    addEventListener(Event.CONNECT, onConnect);
    addEventListener(IOErrorEvent.IO_ERROR, onIoError);
    addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
    addEventListener(ProgressEvent.SOCKET_DATA, onData);
}

Upvotes: 0

Joe Ward
Joe Ward

Reputation: 759

It looks like you are adding the event listeners to the "this" object, not the socket.

try this:

m_socket.addEventListener(Event.CLOSE, onClose);
m_socket.addEventListener(Event.CONNECT, onConnect);
m_socket.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
m_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
m_socket.addEventListener(ProgressEvent.SOCKET_DATA, onData);

And you should start seeing socket events.

Upvotes: 2

Related Questions