gpt4 book ai didi

javascript - 无法使用 Lighttpd 访问 Raspberry Pi 上的 socket.io.js [Node.JS & Socket.IO]

转载 作者:太空宇宙 更新时间:2023-11-04 01:06:23 25 4
gpt4 key购买 nike

从昨天开始我对 Node.JS 和 Socket.IO 完全陌生。

我尝试让 Node.JS 和 Socket.IO 在我的 Raspberry Pi 上工作,但似乎不起作用。 我无法访问<myip>:1337/socket.io/socket.io.js .我已关注this tutorial所以我的 Lighttpd.conf 文件看起来像这样:

$HTTP["host"] == "<myURLtomywebsite>" {
proxy.server = (" " => ((
"host" => "<myIP>",
"port" => 1337)
)
)

我的server.js看起来像这样:

var http = require('http');

httpServer = http.createServer(function(req, res) {
res.end('Hello World!');
});

httpServer.listen(1337);

var io = require('socket.io').listen(httpServer);
var clients = 0;

io.sockets.on('connection', function(socket) {
++clients;

socket.on('disconnect', function(data) {
--clients;
io.sockets.emit('disusr', clients);
});

io.sockets.emit('newusr', clients);
});

我绑定(bind)到 disusrnewusr我的事件client.js显示 div 中已连接用户的数量.

我的 localhost 一切看起来都很好但是,在生产环境中,我无法链接到我的 socket.io.js文件 1337港口。说实话,我什至不知道该使用什么地址? (我网站的 URL 附加了 :1337localhost ,我会创建其他一些地址吗?)

任何帮助将不胜感激。谢谢!

最佳答案

我解决了我的问题!

我链接了socket.io.js像这样:<script type="text/javascript" src="/socket.io/socket.io.js"></script>

我使用了 HAProxy 而不是 this question 中指定的 Lighttpd mod_proxy

这是我的conf文件(根据您的配置修改<...>):

# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
uid 99
gid 99
daemon

defaults
log global
mode http
option httplog
option dontlognull
retries 3
option http-use-proxy-header
option redispatch
option http-server-close
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000

frontend public
bind *:80
acl is_example hdr_end(host) -i <URL.toyourwebsite.com>
acl is_websocket hdr(Upgrade) -i WebSocket
acl is_websocket path_beg -i /websockets
use_backend ws if is_websocket is_example
default_backend www

backend ws
balance roundrobin
option forwardfor # This sets X-Forwarded-For
timeout queue 5000
timeout server 86400000
timeout connect 86400000
server apiserver localhost:<PORT> weight 1 maxconn 1024 check

我让 Lighttpd 监听 8080 端口(否则 HAProxy 将无法启动)。

提醒没有必要使用 mod_proxy,因为它与 websockets 不兼容。请改用 HAProxy。

关于javascript - 无法使用 Lighttpd 访问 Raspberry Pi 上的 socket.io.js [Node.JS & Socket.IO],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22605821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com