Answer the question
In order to leave comments, you need to log in
Why not websocket node.js SSL connection?
Why is the websocket not connecting? The problem is that everything works locally, the ports are open. I turn on a regular VPN in Opera. Throws an error to the console net::ERR_TUNNEL_CONNECTION_FAILED
if I disable the VPN connection to the socket is established. But the most interesting thing is that if I turn on the VPN connection back to the socket, it does not break and the socket works normally, it receives and transmits everything. What could be the problem? Where am I making a mistake?
Here is the socket itself:
var mysql = require('mysql');
var fs = require( 'fs' );
const ssl ={
key: fs.readFileSync('../crt/private.key'),
cert: fs.readFileSync('../crt/domain_name.crt'),
ca: fs.readFileSync('../crt/chain.crt'),
requestCert: false,
rejectUnauthorized: false
};
const express = require('express'),
app = express(),
https = require('https').createServer(ssl,app),
io = require('socket.io').listen(https);
const host = 'site.ru';
const port = 441;
let clients = [];
io.on('connection', socket => {
//Получение инфы о пользователи
var regexp = /user=([^&]+)/;
var GetValue = '';
if (!!regexp.exec(socket.handshake.url))
GetValue = regexp.exec(socket.handshake.url)[1];
var arr =GetValue.split('_'),
userId =arr[0],
room=arr[1];
console.log({'connected':userId,"roomNew":room});
io.sockets.emit('message', {'connected':userId,"roomNew":room})
clients.push(userId);
var conn = mysql.createConnection({
database: 'game',
host: "localhost",
user: "root",
password: "Kx2H5LdJumlqzkxd"
});
var sql1 = "UPDATE `user` SET `ontime`=1 WHERE `uid` ="+userId+"";
conn.query(sql1, function(err, results) {
if (err) throw err;
socket.emit({'connected':userId,"roomNew":room});
console.log("Online-"+userId);
conn.end();
});
//Отправка сообщений всем
socket.emit('message', message => io.socket.emit({'connected':userId,"roomNew":room}));
//socket.on('message', message => io.socket.emit({'message': 'HI'}));
socket.on('message', message => console.info('message', message));
//Отключение пользователя
socket.on('disconnect', () => {
var conn = mysql.createConnection({
database: 'game',
host: "localhost",
user: "root",
password: "Kx2H5LdJumlqzkxd"
});
var sql1 = "UPDATE `user` SET `ontime`=0 WHERE `uid` ="+userId+"";
conn.query(sql1, function(err, results) {
if (err) throw err;
console.log("Ofline-"+userId);
conn.end();
});
clients.splice(clients.indexOf(socket.id), 1);
io.sockets.emit('message', {'discconnected':userId,"roomOut":room});
console.log({"discconnected":userId,"roomOut":room});
})
});
io.on('message', socket => {console.info("ss")});
app.use(express.static(__dirname));
app.get('/', (req, res) => res.render('index'));
//получение количества активных клиентов
app.get('/clients-count', (req, res) => {
res.json({count: io.clients().server.engine.clientsCount})
});
//отправка сообщения конкретному клиенту по его id
app.post('/client/:id', (req, res) => {
if (clients.indexOf(req.params.id) !== -1) {
io.sockets.connected[req.params.id].emit('private message', `Message to client with id ${req.params.id}`);
return res.status(200).json({ message: `Message was sent to client with id ${req.params.id}` })
} else return res.status(404).json({ message: 'Client not found' })
});
https.listen(port, host, () => console.log(`Server listens https://${host}:${port}`));
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question