I created an node app for websocket type project. There are no issues in my local machine, everything is working, e.g. connecting, sending and receiving but when upload to server, client is not connecting at all, it returns
WebSocket is closed before the connection is established.
below is my server code
require('dotenv').config();
const path = require('path');
const fs = require('fs');
const options = {
key: fs.readFileSync(path.join(__dirname,'../'+process.env.SSL_KEY)),
cert: fs.readFileSync(path.join(__dirname,'../'+process.env.SSL_CRT)),
ca: process.env.SSL_CA
};
const http = require('https').createServer(options);
const io = require('socket.io')(http,{
cors : {
origin : '*'
}
});
// middleware
io.use((socket, next) => {
let token = socket.handshake.query.token;
if( token == process.env.TOKEN ){
return next();
}
return next(new Error('authentication error'));
});
http.listen(3800, () => {
console.log('listening on *:3700');
});
and below is the client
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.4/socket.io.js" integrity="sha512-aMGMvNYu8Ue4G+fHa359jcPb1u+ytAF+P2SCb+PxrjCdO3n3ZTxJ30zuH39rimUggmTwmh2u7wvQsDTHESnmfQ==" crossorigin="anonymous"></script>
<script>
var socket = io('mydomain.com:3800?token=myapp2021',{
transports : ['websocket'],
secure : true
});
socket.on('connect', function() {
console.log( socket.connected );
});
</script>
Any help, ideas, suggestions, are greatly appreciated.
question from:
https://stackoverflow.com/questions/65598695/socketio-failed-websocket-is-closed-before-the-connection-is-established-on-pro 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…