2

I am trying to write a simple firefox mobile addon that talks with my server side code using Websocket.

I have my code working for Desktop Firefox Addon but I am having difficulty with one for Firefox mobile.

function connectToServer(aWindow) {

var ws = new MozWebSocket("ws://ipaddress:8887");  // LINE 20
// var ws = new WebSocket("ws://ipaddress:8887");
ws.onopen = function() {
    showToastMsg(aWindow, 'Sending');
    ws.send('data');
}   
ws.onmessage = function (evt) { 
    showToastMsg(aWindow, 'Display')
};  
ws.onclose = function() { 
};  

I have tried both MozWebSocket and WebSocket, but both of them gives me error similar to the following :

E/GeckoConsole(15569): [JavaScript Error: "ReferenceError: MozWebSocket is not defined" {file: "resource://gre/modules/XPIProvider.jsm -> jar:file:///data/data/org.mozilla.firefox/files/mozilla/sq4c77hi.default/extensions/view-source@mydomain.org.xpi!/bootstrap.js" line: 20}]

Anyone know what I need to import or do to be able to reference WebSocket? I just want to send data back and forth from my Firefox Android addon with my server side code using websocket. Any suggestions?

I am just confused because I have this setup running on Firefox Desktop Addon with very similar code.

Any help would be very appreciated thank you!

Young
  • 21
  • 4
  • @apsillers I have tried WebSocket and it gave me the same error so I searched online and it suggested using MozWebSocket and that is another one that I tried. Both are still not working and giving me the same referenceError. – Young Apr 01 '13 at 14:16

2 Answers2

2

Try next solution

var ws = new Services.appShell.hiddenDOMWindow.WebSocket("ws://ipaddress:8887");
Misha
  • 433
  • 4
  • 10
  • This works, but if calling this on starting Firefox, will get an error about controller is not ready, I think it's due to window is not ready. So I use Promise and setTimeout to retry until it loads successfully. – Xiaohui Zhang Jun 22 '17 at 14:34
1

Are you using the Add-on SDK? Which file is this code going into?

First off, Mozilla 'un-prefixed' MozWebsocket to Websocket some time ago:

https://www.evernote.com/shard/s1/sh/59230d89-52f6-4f23-81de-75ab88f38c22/f9f1c0c64959ee44bdc833707efe10a9

...however the Websocket api is only available in actually web documents. What I've doen in the past is I've used the page-worker api to load a document in the background and connect to a Websocket server from the worker page:

https://github.com/canuckistani/Jetpack-Websocket-Example

For more in the page-worker api:

https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/page-worker.html

In the future we have plans to expose HTML5 apis more directly to add-on developers.

therealjeffg
  • 5,790
  • 1
  • 23
  • 24
  • Thank you for your quick response. The problem I am having is getting WebSocket to work on Firefox Mobile Addon I tried using Fennec and now using Just Firefox Android. But none of them works for me. I have a short code written at [link to github](https://bitbucket.org/ykim320/kayo). The desktop firefox extension works fine. But I have having trouble getting mobile version of addon to send message and connect to server. If you have anyway you can connect to the server using firefox mobile addon any pointers will be appreciated. Thank you again. – Young Apr 08 '13 at 18:52
  • If possible can you please help me with any directions I can take with this firefox mobile addon? – Young Apr 23 '13 at 18:23