Firefox SDK PageWorker:未定义插件

时间:2015-02-22 23:49:35

标签: javascript sdk websocket firefox-addon worker

我不知道为什么会出现这个问题,我遵循了这个指南:MDN Guide

但仍然得到错误:

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

收到来自websocket的消息,一切都很好,但我无法向服务器发送消息,因为websocket.html没有“听到”我因为它无法应用端口监听器。但我没有看到问题的原因。 有人看到我的错误吗?

以下是我的“ 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); 
    }
});

websocket.html

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

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

所以 addon.port.emit 有效,而 addon.port.on 。但为什么呢?

谢谢(至少阅读):)

1 个答案:

答案 0 :(得分:0)

时间问题。这是我的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);
}

所以我添加了一个在建立连接时调用的函数 - 直到那时才添加端口监听器并告诉main.js一切正常。 -

我改变的另一件事是pageworker构造函数本身:

var pw = pageWorker.Page({

});

它仅在开始时初始化。

在我的page-mod onAttach-callback中,我添加了

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

我不知道早些时候有可能。

所以现在它运行没有错误。感谢8472指出时间问题。 有时你只是看错了错误的一端:)