从JS调用flash方法时,函数不存在

Function does not exist when calling flash method from JS

本文关键字:函数 不存在 方法 JS 调用 flash      更新时间:2023-09-26

我有一个简单的闪存插座,用来连接IRC服务器。它有一个openclosesend方法,可供JS通过ExternalInterface使用,分别用于打开连接、关闭连接和发送消息。套接字在收到消息时调用JS中的IRC.io.receive,JS将消息解析为有用的内容。

不幸的是,每当从JS调用任何flash方法时,它们都会返回一个"__不是函数"错误。

这是(淡化的)AS,其中IRC是文档类:

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

和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>

对在ExternalInterface中注册的任何函数的任何调用都会引发"函数不存在"异常。我做错什么了吗?

当swf准备好接收呼叫时,尝试从它发出信号。

例如,在ActionScript:中

ExternalInterface.call('initIRQ');

在您的JavaScript中:

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