title: Firefox add-on drops WebSocket connection to Kodi after a few seconds
body: <p>I have decided to develop an add-on for Firefox to control <a href="https://kodi.tv/" rel="nofollow">Kodi</a> because I couldn't find any that already did everything I wanted. I have chosen to use <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API" rel="nofollow">WebSocket</a> to communicate with Kodi so that I can receive events, but am having problems implementing the WebSocket portion of my add-on. Temporarily, I have created an add-on with a <a href="https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/page-worker" rel="nofollow">page-worker</a> solely for testing the WebSocket functionality. Both my development add-on and the temporary testing add-on exhibit the following behavior:</p>
  
  <p>If I load <code>test.html</code> (see bottom of post for contents) directly in Firefox outside of the add-on, WebSocket works as expected. The connection stays open and receives events as I play or pause Kodi:</p>
  
  <h3>Console output from running test.html outside of the add-on</h3>
  
  <pre><code>Connection opened test.html:12:9
  Object { jsonrpc: "2.0", method: "Player.OnPlay", params: Object } test.html:28:9
  Object { jsonrpc: "2.0", method: "Player.OnPause", params: Object } test.html:28:9
  Object { jsonrpc: "2.0", method: "Player.OnPlay", params: Object } test.html:28:9
  Object { jsonrpc: "2.0", method: "Player.OnPause", params: Object } test.html:28:9
  Object { jsonrpc: "2.0", method: "Player.OnPlay", params: Object } test.html:28:9
  Object { jsonrpc: "2.0", method: "Player.OnPause", params: Object } test.html:28:9
  Object { jsonrpc: "2.0", method: "GUI.OnScreensaverActivated", params: Object } test.html:28:9
  </code></pre>
  
  <p>However, this is not the case when running the add-on from the command line using <code>jpm run</code>. The connection closes after a few seconds (four seconds max). (Note that I can receive events just fine during the time the connection is open):</p>
  
  <h3>Console output from running the add-on using jpm</h3>
  
  <pre><code>C:\Users\User\Desktop\example&gt;jpm run
  JPM [info] Starting jpm run on test
  JPM [info] Creating a new profile
  console.log: test: Connection opened
  console.log: test: {"jsonrpc":"2.0","method":"Player.OnPlay","params":{"data":{"item":{"id":23,"type":"song"},"player":{"playerid":0,"speed":1}},"sender":"xbmc"}}
  console.log: test: {"jsonrpc":"2.0","method":"Player.OnPause","params":{"data":{"item":{"id":23,"type":"song"},"player":{"playerid":0,"speed":0}},"sender":"xbmc"}}
  console.log: test: Websocket error: undefined
  console.log: test: Connection closed: 1006
  </code></pre>
  
  <p>How can I find out why the connection drops inside the add-on, but not outside of the add-on? How can I remedy this strange behavior?</p>
  
  <hr>
  
  <h3>index.js:</h3>
  
  <pre><code>var data = require("sdk/self").data;
  var pageWorker = require("sdk/page-worker");
  
  var pw = pageWorker.Page({
    contentURL: data.url('test.html')
  });
  
  pw.port.on('message', function(message) {
    console.log(message);
  });
  </code></pre>
  
  <h3>test.html:</h3>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en-US"&gt;
    &lt;head&gt;
    &lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;
      &lt;script type=text/javascript&gt;
      var ws = new WebSocket('ws://192.168.2.34:9090/jsonrpc');
  
      ws.onopen = function (event) {
        try {
          addon.port.emit('message', "Connection opened");
        } catch (e) {
          console.log("Connection opened");
        }
      };
  
      ws.onerror = function (event) {
        try {
          addon.port.emit('message', "Websocket error: "+event.data);
        } catch (e) {
          console.log("Websocket error: "+event.data);
        }
      };
  
      ws.onmessage = function (event) {
        try {
          addon.port.emit('message', JSON.parse(event.data));
        } catch (e) {
          console.log(JSON.parse(event.data));
        }
      };
  
      ws.onclose = function (event) {
        try {
          addon.port.emit('message', "Connection closed: "+event.code);
        } catch (e) {
          console.log("Connection closed: "+event.code);
        }
      };
      &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>