简体   繁体   English

Firefox SDK PageWorker:附件未定义

[英]Firefox SDK PageWorker: Addon is not defined

i have no clue, why this Problem occurs, i followed this guide: MDN Guide 我不知道为什么会出现此问题,我遵循了本指南: MDN指南

But still get the Error: 但是仍然出现错误:

JavaScript error: resource://jid1-rcm3stktbkcg3q-at-jetpack/ff/data/websockethtmlscript.js, line 23: ReferenceError: addon is not defined

Messages FROM the websocket are received and everything is fine, but i cannot send messages TO the server as the websocket.html doesn't "hear" me because it cannot apply the port listener. 收到了来自websocket的消息,一切都很好,但是我无法将消息发送到服务器,因为websocket.html不会“听到”我的消息,因为它无法应用端口侦听器。 But i do not see the problem why. 但是我不明白为什么会这样。 Does anybody see my mistake? 有人看到我的错误吗?

Here is the PageWorker initiation part in my " main.js " 这是我的“ main.js ”中的PageWorker初始化部分

var buttons = require('sdk/ui/button/action'); 
var tabs = require("sdk/tabs");
var pageMod = require("sdk/page-mod");
var pageWorker = require("sdk/page-worker");
var self = require("sdk/self");
var settings = require("sdk/simple-prefs");
var activeTABURL = "no";
var about_config = require("sdk/preferences/service");
about_config.set("network.websocket.allowInsecureFromHTTPS", true);

//create page-worker to load trusted content
var pw = pageWorker.Page({
    contentURL: './websocket.html'
});

pw.port.on('loaded', function() { 
//donothing 
});

//add portlistener to receive messages from websocketserver
pw.port.on('eyeTrackerData', function(message) {
    if (currentPort != null) {
        //currentport is the worker port for the pageMod
        //here, the messages from the websocketserver are forwarded
        currentPort.emit('eyeTrackerData',message); 
    }
});

and the websocket.html : websocket.html

<!doctype html>
<html>
  <body>
    <script type="text/javascript" src="websockethtmlscript.js"></script>
  </body>
</html>

the websockethtmlscript.js : websockethtmlscript.js

websocket.onmessage = function(evt) { onMessage(evt); };
websocket.onopen = function(evt) { websocket.send("HI.") };
websocket.onerror = function(evt) { onError(evt); };

function disconnect(){
    websocket.close();
}

function onError(evt) {
    console.log("An error occured in the websocket.html - is the WebSocket Server running?");
}

function onMessage(evt) {
    addon.port.emit('eyeTrackerData',evt.data);       
}

function sendMessage(message){
    //addon.port.emit('message','message_sent');
    websocket.send(message);
}
//add port listener to send messages to the websocketserver
addon.port.on("toEyetracker",function (message){
    console.log("websocket.html sending to websocketserver: "+message);
    sendMessage(message);
});

So addon.port.emit works, while addon.port.on does not . 因此, addon.port.emit有效,而addon.port.on 无效 But why? 但为什么?

Thank you (at least for reading) :) 谢谢(至少阅读):)

it was the timing problem. 时间问题。 this is my websockethtmlscript.js now: 现在是我的websockethtmlscript.js

var websocket = new WebSocket('ws://localhost:6777');
websocket.onmessage = function(evt) { messageReceived(evt); };
websocket.onopen = function(evt) { conReady(); };
websocket.onerror = function(evt) { onError(evt); };

function disconnect(){
    websocket.close();
}

function onError(evt) {
    console.log("An error occured in the websocket.html - is the WebSocket Server running?");
}

function messageReceived(evt) { 

    addon.port.emit('eyeTrackerData',evt.data);       

}

function conReady() {
    console.log("connection established");
    console.log("adding port listener");
    addon.port.on("toEyetracker",function (message){
        console.log("websocket.html sending to websocketserver: "+message);
        try{
            sendMessage(message);
        } catch (err){
            console.log("Error in Websocket Page-Worker: "+err.name);
        }
    });
    addon.port.emit('conReady',"ready");
    websocket.send("HI.");
}

function sendMessage(message){
    websocket.send(message);
}

so i added a function that is called when the connection is established - not till then the port listener is added and tells the main.js that everything went fine. 所以我添加了一个在建立连接时调用的函数-直到那时才添加端口侦听器,并告诉main.js一切正常。 - -

another thing i altered was the pageworker constructor itself: 我更改的另一件事是pageworker构造函数本身:

var pw = pageWorker.Page({

});

it is only initialized at the beginning. 它仅在开始时初始化。

in my page-mod onAttach-callback i then added 然后在我的page-mod onAttach-callback中添加

pw.contentURL = './websocket.html';

i didn't know that was possible earlier. 我不知道那是不可能的。

so now it runs with no errors. 所以现在它可以正常运行了。 thanks to the8472 for pointing out the timing issue. 感谢8472指出了时间问题。 sometimes you just look at the wrong end for errors :) 有时您只是看错了错误的一端 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM