我目前正在使用包含聊天功能的社交网络Web应用程序,当我在本地主机上时,聊天工作正常。 我遇到的问题是,当我尝试将客户端连接到(托管)服务器时,我从SocketIO中收到错误消息。
错误看起来像这样:GET http://127.0.0.1/socket.io/?EIO=3&transport=polling&t=N1Kh0TS net :: ERR_CONNECTION_REFUSED
现在,我已经尝试使用网络上的随机解决方案解决此问题很长时间了,但是我对此并不太确定。
run.py:
from random import app, socketio
if __name__ == '__main__':
socketio.run(app, debug=True)
在routes.py中,我的连接事件如下:
@socketio.on('user_con')
def receive_connection(data):
# join the desired channel
join_room(data['channel'])
print('User ' + str(data['user']) + ' successfully joined channel : ' + str(data['channel']))
客户端这样连接:
//Connect the socket to our server
var socket = io.connect('http://127.0.0.1:5000');
socket.on('connect', function() {
var connection_data = {
user : '{{ current_user.id }}',
channel : '{{ channel.name }}'
}
//send the connection data
socket.emit('user_con', connection_data);
});
请注意,该代码在本地计算机上确实运行良好,但尚未部署。一切都托管在Linux服务器上。
非常感谢您的帮助。 谢谢。
EDIT2:这是我们的Nginx文件
server {
server_name #censored#;
location /static {
alias /home/admin/Prototyp_1/#censored#/static;
}
location / {
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
proxy_redirect off;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/#censored#/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/#censored#/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = #censored#) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = #censored#) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name #censored#;
return 404; # managed by Certbot
}
edit3:我们在代码中添加了以下内容
location /socket.io {
include proxy_params;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://127.0.0.1:5000/socket.io;
}
有趣的是,当我们将html中的端口更改为5000时,错误为“对接拒绝”,当端口为8000时,错误为“不允许原点”