Use Firefox OS as web server

拜拜、爱过 提交于 2019-12-25 14:00:13

问题


For an art project, I'd like to have multiple distributed devices that can output sound. Firefox OS devices seem optimal. They bring the necessary hardware and I know HTML and JS very well. But I also need a control web server.

From my understanding, a Firefox OS device can act as an WiFi access point ("Share Internet"). However, it cannot act as a small web server for other devices that join the network – without any internet connection. The APIs for native apps seem just not to be powerful enough.

But maybe I am mistaken (I would like to be). So, is a Firefox OS device able to run as a small web server?


回答1:


httpd.js did not work out-of-the-box for me. But it brought me on the right track. I then found this and after a little bit of tweaking and updating of the code, I got a super-simple server solution.

function startListen(){
  console.log("Initializing server");
  var socketServer = navigator.mozTCPSocket.listen(8080);

  socketServer.onconnect = function(conn){
    console.log("connected", conn, conn.ondata);
    conn.ondata = function(ev){
      console.log("Got request: ", ev);   
      conn.send("Ok. Got client on port " + conn.port);
      conn.close();
    };
    conn.onclose = function(ev){
      console.log("Client left:", ev);
    }
  };
  socketServer.onerror = function(ev){
    console.log("Failed to start: ", ev);
  };
}
startListen();

The tcp-socket permission is needed.

With this code, I was able to start this in the Firefox OS simulator, direct my browser to open http://localhost:8080 and get an answer and logs in the console.

PS. This also works on a real device. Unfortunately, a separate access point is needed. While Firefox OS can work as a hotspot itself, it can neither be client or server in that mode (outgoing connections are not routed properly and incoming connections are refused).




回答2:


You should try httpd.js. This library is for FirefoxOS 2.0.

// create a server object
server = new HttpServer();

// configure /sdcard/public as document root
server.get("/", "/sdcard/public");

// launch on port 3000
server.start(3000);



回答3:


I don't think that you need a server for this task, you can do master-slave communication with WebRTC and handle the execution of the sound client side.




回答4:


I recently wrote an article on the Mozilla Hacks blog demonstrating how to implement this:

Embedding an HTTP Web Server in Firefox OS



来源:https://stackoverflow.com/questions/28062801/use-firefox-os-as-web-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!