Question

I'm creating a firefox extension using the addon sdk that has a widget for user login, and on sucesful login, creates a websocket to a socket server I control, using socket.io and node.js.

I'm not sure if i'm going about it the correct way, however, in lib/main.js, I have the following code:

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

If I run it, I get the following error:

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

As an FYI, see below my attempt at using ws, which also did not work:

let { window } = require("sdk/addon/window");
let exampleSocket = new window.WebSocket("ws://someip:port");

My questions are:

  1. Am I going in the correct direction with this approach?
  2. Should I be trying using browser websockets instead (ww/wws)? I tried with using ww/wws, however it wouldn't connect to my node socket server. I would get an debug - destroying non-socket.io upgrade message from the server, and If I set destroy upgrade to false, I would just get nothing through, as if no request was even made.
  3. If I am going about it the correct way using socket.io, am I loading it in correctly? Could this be the problem?

Any help would be great, even if it's just push me/keep in the right direction. I'm currently at a bit of a loss.

Was it helpful?

Solution

I finally got websockets with socket.io working. The following shows how.

My main.js file:

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

and the worker.html file:

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top