简体   繁体   中英

Firefox SDK PageWorker: Addon is not defined

i have no clue, why this Problem occurs, i followed this guide: MDN Guide

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. But i do not see the problem why. Does anybody see my mistake?

Here is the PageWorker initiation part in my " main.js "

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 :

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

the 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 . But why?

Thank you (at least for reading) :)

it was the timing problem. this is my websockethtmlscript.js now:

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

another thing i altered was the pageworker constructor itself:

var pw = pageWorker.Page({

});

it is only initialized at the beginning.

in my page-mod onAttach-callback i then added

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. sometimes you just look at the wrong end for errors :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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