我有一个简单的闪存插座,用来连接IRC服务器。它有一个open
、close
和send
方法,可供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
}