我正在使用addon sdk创建一个firefox扩展,它有一个用于用户登录的小部件,并且在sucesful登录时,使用socket.io和node.js创建一个websocket到我控制的套接字服务器。
我不确定我是否采用正确的方法,但是,在lib / main.js中,我有以下代码:
function start_socket(){
console.log("trying to open websocket!");
var io = require("socket.io.js");
socket = io.connect("https://someip:port");
socket.on('connect', function() {
console.log('connected to server');
});
}
如果我运行它,我会收到以下错误:
console.log: http: trying to open websocket!
console.error: http:
Message: TypeError: global.location is undefined
Stack:
Socket.prototype.isXDomain@resource://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/jid1-HVkiC5LW55dv0g@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/socket.io.js:1877
Socket.prototype.handshake@resource://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/jid1-HVkiC5LW55dv0g@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/socket.io.js:1627
Socket.prototype.connect@resource://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/jid1-HVkiC5LW55dv0g@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/socket.io.js:1699
Socket@resource://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/jid1-HVkiC5LW55dv0g@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/socket.io.js:1551
io.connect@resource://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/jid1-HVkiC5LW55dv0g@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/socket.io.js:94
start_socket@resource://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/jid1-HVkiC5LW55dv0g@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/main.js:25
signin<.onComplete@resource://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/jid1-HVkiC5LW55dv0g@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/main.js:17
emit@resource://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/jid1-HVkiC5LW55dv0g@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/event/core.js:94
onreadystatechange@resource://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/jid1-HVkiC5LW55dv0g@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/request.js:98
作为一个FYI,请参阅下面我尝试使用ws,这也不起作用:
let { window } = require("sdk/addon/window");
let exampleSocket = new window.WebSocket("ws://someip:port");
我的问题是:
debug - destroying non-socket.io upgrade
消息,如果我将destroy upgrade
设置为false,我就什么也得不到,就好像没有请求一样。任何帮助都会很棒,即使它只是推动我/保持正确的方向。我目前有点亏。
答案 0 :(得分:4)
我终于得到了socket.io工作的websockets。以下显示了如何。
我的main.js文件:
var pageWorkers = require("sdk/page-worker");
var self = require("sdk/self");
pageWorkers.Page({
contentScriptFile: self.data.url("worker.js"),
contentURL: self.data.url("worker.html"),
contentScriptFileWhen: "ready",
onMessage: function(message) {
console.log(message);
}
});
和worker.html文件:
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="http://domain:port/socket.io/socket.io.js"></script>
<script>
var socket = io.connect("http://domain:port");
socket.on('connect', function(){
socket.on('event', function(data){});
socket.on('error', function(data){console.log("error: " + error)});
socket.on('disconnect', function(){});
});
</script>
</head>
<body>
</body>
</html>