Monchoman45
Monchoman45

Reputation: 547

Function does not exist when calling flash method from JS

I have a simple flash socket that I use to connect to IRC servers. It has an open, close, and send method made available to JS through ExternalInterface, for opening connections, closing connections, and sending messages respectively. The socket calls IRC.io.receive in JS whenever it gets a message, which is parsed by JS into something useful.

Unfortunately, whenever any of the flash methods are called from JS, they return a "__ is not a function" error.

Here's the (watered down) AS, where IRC is the document class:

public class IRC extends MovieClip {
    public static function open(url:String, port:int) {/* code */}
    public static function close(port:int) {/* code */}
    public static function send(port:int, message:String) {/* code */}

    public function IRC() {
        ExternalInterface.addCallback('send', IRC.send);
        ExternalInterface.addCallback('open', IRC.open);
        ExternalInterface.addCallback('close', IRC.close);
    }
}

And HTML/JS:

<html>
    <head>
        <script type="text/javascript">
            window.IRC = {
                io: {}
            };
            IRC.io.receive = function(message) {/* code */}
            IRC.io.send = function(port, str) {
                document.getElementById('socket').send(port, str);
            }
            IRC.io.open = function(url, port) {
                document.getElementById('socket').open(url, port);
            }
            IRC.io.close = function(port) {
                document.getElementById('socket').close(port);
            }
        </script>
    </head>
    <body>
        <!-- ui -->
        <embed src="socket.swf" quality="high" allowscriptsaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowfullscreen="false" style="display:none;">
    </body>
<html>

Any call to any of the functions registered with ExternalInterface throws a "function does not exist" exception. Did I do something wrong?

Upvotes: 0

Views: 311

Answers (1)

david.emilsson
david.emilsson

Reputation: 2331

Try signalling from your swf when it's ready to receive calls.

For example, in your ActionScript:

ExternalInterface.call('initIRQ');

And in your JavaScript:

function initIRQ() {
    //Begin communication with swf
}

Upvotes: 1

Related Questions