使用 Firefox OS 作为网络服务器 [英] Use Firefox OS as web server
问题描述
对于一个艺术项目,我想要多个可以输出声音的分布式设备.Firefox OS 设备似乎是最佳选择.他们带来了必要的硬件,我非常了解 HTML 和 JS.但我还需要一个控制网络服务器.
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.
据我所知,Firefox OS 设备可以充当 WiFi 接入点(共享互联网").但是,它不能作为加入网络的其他设备的小型网络服务器 - 没有任何互联网连接.原生应用的 API 似乎不够强大.
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.
但也许我错了(我想是).那么,Firefox OS 设备能否作为小型网络服务器运行?
But maybe I am mistaken (I would like to be). So, is a Firefox OS device able to run as a small web server?
推荐答案
httpd.js 对我来说不是开箱即用的.但它让我走上了正轨.然后我找到了 this 并经过一些调整和更新的代码,我得到了一个超级简单的服务器解决方案.
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();
需要tcp-socket
权限.
使用此代码,我可以在 Firefox OS 模拟器中启动它,引导我的浏览器打开 http://localhost:8080 并获得答案并登录控制台.
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.
附注.这也适用于真实设备.不幸的是,需要一个单独的接入点.虽然 Firefox OS 本身可以作为热点工作,但在该模式下它既不能作为客户端也不能作为服务器(传出连接没有正确路由,传入连接被拒绝).
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).
这篇关于使用 Firefox OS 作为网络服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!