G
G
Galdar Turin2019-09-19 09:21:12
Node.js
Galdar Turin, 2019-09-19 09:21:12

How to connect client via socket.io?

This is how the client connection to the socket server is described in the documentation, but it does not suit me.
In the query string I write https://domain.ru:9081
The HTML document says

<script src="/socket.io/socket.io.js"></script>
<script>
        $(function () {
            var socket = io.connect('https://domain.ru:9081');    
            socket.emit('data', JSON.stringify({"user": "", "index":""}) );

        });
</script>

I want to do it like this, but it doesn’t work, I can’t find similar options in Google
Specify in the request https://domain.ru
AND in HTML CLIENT
client
<script src="/socket.io/socket.io.js"></script>
<script>
        $(function () {
            var socket = io.connect('https://domain.ru/connect/');    
            socket.emit('data', JSON.stringify({"user": "", "index":""}) );
        });
</script>

SERVER
socket
var express    = require('express'),
    app        = express(),
    server     = require('http').createServer(app),
    io         = require('socket.io')(server),
    log4js     = require('log4js');

var httpPort   = 9081;

var qStatus    = false;

log4js.configure({
// Error
  appenders: { error: { type: 'file', filename: 'socket-error.log' } },
  categories: { default: { appenders: ['error'], level: 'error' } }, 
// Trace
    appenders: { info: { type: 'file', filename: 'socket-info.log' } },
    categories: { default: { appenders: ['info'], level: 'trace' } },
});

const logger = log4js.getLogger('PATH: '+__filename);

const connects = [];

app.set('views', __dirname + '/views')
app.set('view engine', 'pug')
app.use(express.static(__dirname + '/public'))

server.listen(httpPort, function () {
    logger.info('Server listening at port', httpPort); 
});

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});

io.attach(server);

io.on('connection', function(user) {

    user.on('data', function(data){
      ...
    });


    user.on('disconnect', function () {
         ...
     });

    });
});

I specify https://domain.ru/connect/ because this is written in the NGINX config
nginx
upstream backend_hosts {
  server XXX.XXX.XX.XX:9081;
}

server {
    server_name domain.ru;

    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    listen 443 ssl;
    root /var/www/TEST;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    index index.php index.html index.hml index.phtml;
    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;
    location / {
      try_files $uri $uri/ /index.php?q=$uri & $args;
    }
  
    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
      access_log off;
      expires max;
    }
  
    location ~ /\.ht {
      deny  all;
    }
  
    location ~ \.php$ {
      fastcgi_index index.php;
      fastcgi_keep_conn on;
      include /etc/nginx/fastcgi_params;
      fastcgi_pass unix:/run/php/php7.1-fpm.sock;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location /connect/ {
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_read_timeout 86400;
      proxy_pass http://backend_hosts;
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Skibin, 2019-09-19
@Galdar

io         = require('socket.io')(server, {
      "path": "/connect"
    }),

everything else should be picked up on what is

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question