简体   繁体   中英

Meaning of socket.io listen a express server?

I am reading socket.io's how to, this follow code I can not understand :

Server (app.js)

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

server.listen(80);

what is the meaning of io = require('socket.io').listen(server); , Is it just use same configurations with socket.io and express?

The listen function takes as argument an http event handler such as the ones you get from http.Server (it can also accept a port, in which case the listen functions creates the http server ).

The http.createServer function creates an http Server from a request listener. And that's what's an express application : a request listener, as can be seen here :

function createApplication() {
  var app = function(req, res, next) {
    app.handle(req, res, next);
  };
  ...
  return app;
}

Of course you don't need express to use socket.io, you may simply pass to listen a port or any instance of http.Server .

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