@kr_ilya

Как правильно настроить socket.io?

Перепробовал разные варианты. Никак не получается настроить socket.io.

Проблема в том, что бекенд подключается в режиме Polling и постоянно шлет xhr запросы на бекенд. WS подключения нет.

Скрины

6027a61fef096678188374.png
В это время на бекенде
6027a6820c8c2153025061.png

Если на клиенте в опициях указать transports: ["websocket"], то соединение хоть и создается, но не постоянное и переподключается каждые 30 секунд.
вот

6027af643a105796959745.png
На беке
6027afa055a18759724989.png

Я предполагаю, что это из-за того, что не отправляется пинг запрос, но я не понимаю, почему он не отправляется и в чем проблема.

client:
import Vue from 'vue'
import VueSocketIOExt from 'vue-socket.io-extended';
import io from 'socket.io-client';

const socket = io('//194.00.100.00', { path:'/socket/', autoConnect: true, });

export default ({ store }) => {
    Vue.use(VueSocketIOExt, socket, { store });
  }
// socket.open();


server:
'use strict'
const Server = require('../express/server.js')

const io = require('socket.io')(Server.server, { path: '/', });

io.on('connection', async (socket) => {

    console.log('IO Connected');

	socket.on('disconnect', () => {

    console.log('socket disconnected');
	});

});


const Socket = {};

module.exports = Socket;


nginx
user                 www-data;
pid                  /run/nginx.pid;
worker_processes     auto;
# worker_rlimit_nofile 65535;

events {
    multi_accept       on;
    worker_connections 65535;
}

http {
    # charset              utf-8;
    sendfile             on;
    tcp_nopush           on;
    tcp_nodelay          on;
    #server_tokens        off;
    # log_not_found        off;
    keepalive_timeout 65;
    types_hash_max_size  2048;
    client_max_body_size 64M;

    # MIME
    include              mime.types;
    default_type         application/octet-stream;

    # Logging
    access_log           /var/log/nginx/access.log;
    error_log            /var/log/nginx/error.log warn;

    # Load configs
    include              /etc/nginx/conf.d/*.conf;
    include              /etc/nginx/sites-enabled/*;
}


nginx/site-enabled
map $sent_http_content_type $expires {
    "text/html"                 epoch;
    "text/html; charset=utf-8"  epoch;
    default                     off;
}

# HTTP redirect
server {
    listen      80;
    # listen      [::]:80;
    # server_name 194.0.100.00 "";
    # root /root/alikAdmin;

    # security
    #include                 nginxconfig.io/security.conf;
    # additional config
    if ($host ~* www\.(.*)) {
    set $host_without_www $1;
    rewrite ^(.*)$ http://$host_without_www$1 permanent;
}

location / {
    #return 503;

    # expires $expires;

    proxy_redirect                      off;
    proxy_set_header Host               $host;
    proxy_set_header X-Real-IP          $remote_addr;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto  $scheme;
    proxy_read_timeout          30m;
    proxy_connect_timeout       30m;
    send_timeout                30m;
    proxy_pass                          http://127.0.0.1:8081; # set the adress of the Node.js instance here
}

location /socket/ {
    #return 503;

    expires $expires;

    proxy_redirect                      off;
    proxy_set_header Host               $host;
    proxy_set_header X-Real-IP          $remote_addr;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto  $scheme;
    proxy_set_header                    Upgrade $http_upgrade; #для сокетов
    proxy_set_header                    Connection "upgrade"; #для сокетов
    proxy_http_version 1.1;
    proxy_read_timeout          30m;
    proxy_connect_timeout       30m;
    send_timeout                30m;
    proxy_pass                          http://127.0.0.1:3000; # set the adress of the Node.js instance here
    

    
    # proxy_ssl_server_name on;
}

error_page 500 502 503 504 /50x.html;

location = /50x.html { 
    root /var/www/errors;
}


# favicon.ico
location = /favicon.ico {
    log_not_found off;
    access_log    off;
}

# robots.txt
location = /robots.txt {
    log_not_found off;
    access_log    off;
}

# gzip
gzip            on;
gzip_vary       on;
gzip_proxied    any;
gzip_comp_level 6;
gzip_types      text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

    # reverse proxy
    #include    nginxconfig.io/proxy.conf;
}
  • Вопрос задан
  • 101 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы