0
votes

I use express to generate my site(i.e. express mysite); I add socket.io support in my site. But when my socket.io client try to connect server, I found the server has kept receiving 'connection' event(.on('connection', function(socket)...) and client doesn't receive any message...

In my /bin/www

var app = require('../app');
var debug = require('debug')('mysite:server');
var http = require('http');
var https= require('https');
var fs = require('fs');


//Get port from environment and store in Express.
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
app.set('httpsport', 443);

//Create HTTP server.
var options = {
    key: fs.readFileSync('.\file.pem'),
    cert: fs.readFileSync('.\file.crt')
};

var server = http.createServer(app);
var httpsServer = https.createServer(options, app).listen(app.get('httpsport'));

//Listen on provided port, on all network interfaces.
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

//Normalize a port into a number, string, or false.
function normalizePort(val) {
    var port = parseInt(val, 10);

    if (isNaN(port)) {
      // named pipe
      return val;
    }

    if (port >= 0) {
      // port number
      return port;
    }
    return false;
}

//Event listener for HTTP server "error" event.
function onError(error) {
   ..........
}
//Event listener for HTTP server "listening" event.
function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

//socket.io
var io = require('socket.io')(httpsServer);

io.on('connection', function(socket) {    
    //keep printing 'new connection' string in console
    console.log('new connection');
    //client doesn't receive this message
    socket.emit('message', {'message': 'hello world'});
});

my client:

   <html>
     <head>
       <script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>
       <script src="https://code.jquery.com/jquery-3.2.1.min.js"
           integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
           crossorigin="anonymous"></script>
     </head>
     <body>
        <script>
          var URL_SERVER = 'https://localhost:443';
          var socket = io.connect(URL_SERVER);

          socket.on('message', function(data) {
             $('#message').text(data.message);
          });
       </script>
       <div id="message">Message</div>
      </body>
    </html>    

any suggestion?

1

1 Answers

1
votes

You are referencing a very old version of socket.io (1.0.0) in your client. You don't specify which version of socket.io you're using on the server, but ideally the client and server should match.

socket.io will serve its matching client script at the /socket.io/socket.io.js path of your server, or you can use a newer version from a CDN.

https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.js https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js

Your code runs as expected for me with both the latest version from the 1.x branch (1.7.4) and the latest version from the 2.x branch (2.0.4)