Firefox 插件中的 Socket.io
Posted
技术标签:
【中文标题】Firefox 插件中的 Socket.io【英文标题】:Socket.io in Firefox add-on 【发布时间】:2014-04-10 12:09:39 【问题描述】:我正在使用具有用户登录小部件的插件 sdk 创建一个 firefox 扩展,并且在成功登录时,使用 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
作为一个仅供参考,请参阅下面我尝试使用 ws 的尝试,但也没有成功:
let window = require("sdk/addon/window");
let exampleSocket = new window.WebSocket("ws://someip:port");
我的问题是:
-
我采用这种方法是否朝着正确的方向前进?
我应该尝试改用浏览器 websockets (ww/wws) 吗?我尝试使用 ww/wws,但它无法连接到我的节点套接字服务器。我会收到来自服务器的
debug - destroying non-socket.io upgrade
消息,如果我将destroy upgrade
设置为false,我将什么也得不到,就好像没有发出任何请求一样。
如果我使用 socket.io 以正确的方式进行操作,我是否正确加载它?会不会是这个问题?
任何帮助都会很棒,即使它只是推动我/保持正确的方向。我现在有点茫然。
【问题讨论】:
所以我发现当我在服务器端使用 socket.io 时,我无法从客户端打开普通的 websocket 连接。所以尝试将socket.io客户端加载到客户端是前进的方向。我只需要弄清楚如何。 【参考方案1】:我终于得到了使用 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>
【讨论】:
以上是关于Firefox 插件中的 Socket.io的主要内容,如果未能解决你的问题,请参考以下文章