socket.io使用nginx代理遇到的问题

ayue728

-----------后端------------
use Workerman\Worker;
use PHPSocketIO\SocketIO;
require_once __DIR__ . '/../Workman/vendor/autoload.php';
$io = new SocketIO(7728);
$io->on('connection', function($socket)use($io){
$socket->emit('server','已连接');
});
Worker::runAll();

---------------nginx----------------
location /sio
{
proxy_pass http://127.0.0.1:7728;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
}
---------------前端----------------
<script>
// 如果服务端不在本机,请把127.0.0.1改成服务端ip
var socket = io('https://xxxx.com/sio');
// 当连接服务端成功时触发connect默认事件
socket.on('connect', function(){
console.log('connect success');
});
socket.on('server', function(msg){
console.log(msg);
});
</script>
无法连接,我开了开发者选项发现,它一直连接的是https://xxxx.com/socket.io/*****
这个/socket.io/是哪来的呀, 我蒙了 ,求指教

2464 1 0
1个回答

walkor

nginx配置用

location /socket.io
{
    proxy_pass http://127.0.0.1:7728;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header X-Real-IP $remote_addr;
}

/socket.io/ 是socket.io客户端发起连接固定使用的路径,不要试图改变它。

前端连的时候还是正常连,不要加路径

  • 暂无评论
年代过于久远,无法发表回答
🔝