2
我使用addon sdk創建了一個firefox擴展,它具有一個用於用戶登錄的小部件,並且在sucesful登錄時創建一個websocket到我控制的套接字服務器,使用socket.io和node.js中Firefox插件中的Socket.io
我不知道如果我繞了正確的方法,但是,在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:
[email protected]://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/[email protected]!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/socket.io.js:1877
[email protected]://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/[email protected]!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/socket.io.js:1627
[email protected]://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/[email protected]!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/socket.io.js:1699
[email protected]://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/[email protected]!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/socket.io.js:1551
[email protected]://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/[email protected]!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/socket.io.js:94
[email protected]://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/[email protected]!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/main.js:25
signin<[email protected]://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/[email protected]!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-hvkic5lw55dv0g-at-jetpack/http/lib/main.js:17
[email protected]://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/[email protected]!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/event/core.js:94
[email protected]://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/v6/jpc7yv2123nbhl7hb8rshygh0000gn/T/tmp3gAPRS.mozrunner/extensions/[email protected]!/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");
我questio ns是:
- 我正在用這種方法正確的方向嗎?
- 我應該嘗試使用瀏覽器websockets(ww/wws)嗎?我嘗試使用ww/wws,但它不會連接到我的節點套接字服務器。我會從服務器收到一條
debug - destroying non-socket.io upgrade
消息,並且如果我將destroy upgrade
設置爲false,那麼我只會收到任何內容,就好像甚至沒有任何請求一樣。 - 如果我正在使用socket.io正確的方式,我正確加載它?這可能是問題嗎?
即使只是推動我/保持正確的方向,任何幫助都會很棒。我目前有點虧損。
所以我發現,因爲我使用的是在服務器端socket.io,我無法從客戶端打開簡單的websocket連接。所以試圖將socket.io客戶端加載到客戶端是前進的方向。我只需要弄清楚如何。 – Simon