简体   繁体   中英

Socket.io not working in node.js express

I made an app with express generator, I'm using socket.io in my app but it's not working for me. I use socket.io in bin/www file and after the server created and listened to the port. after load the page that want to connect to the socket, browser's console show this error:

'socket.emmit is not a function'

here's the codes:

var app = require('../app');
var debug = require('debug')('server3:server');
var http = require('http');


var port = normalizePort(process.env.PORT || '8585');
app.set('port', port);


var server = http.createServer(app);

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);


function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
  return val;
  }

  if (port >= 0) {

  return port;
  }

  return false;
};

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
};


function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
};



var io = require('socket.io').listen(server);

io.on('connection', function(socket){
  console.log('socket is on...');

});

It's the summary of my socket codes. the main codes are here. anyway it's not working!

该方法的正确名称是socket.emit (单“ m”)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM