2014-04-10 63 views
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是:

  1. 我正在用這種方法正確的方向嗎?
  2. 我應該嘗試使用瀏覽器websockets(ww/wws)嗎?我嘗試使用ww/wws,但它不會連接到我的節點套接字服務器。我會從服務器收到一條debug - destroying non-socket.io upgrade消息,並且如果我將destroy upgrade設置爲false,那麼我只會收到任何內容,就好像甚至沒有任何請求一樣。
  3. 如果我正在使用socket.io正確的方式,我正確加載它?這可能是問題嗎?

即使只是推動我/保持正確的方向,任何幫助都會很棒。我目前有點虧損。

+0

所以我發現,因爲我使用的是在服務器端socket.io,我無法從客戶端打開簡單的websocket連接。所以試圖將socket.io客戶端加載到客戶端是前進的方向。我只需要弄清楚如何。 – Simon

回答

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>