如何使用Express 4与Socket.io 0.9.x?

时间:2014-07-27 07:50:35

标签: node.js express socket.io

我是ios开发人员,Objc-Socket.io库仍然支持socket.io 0.9.x

我在我的nodejs服务器上使用Express 4.0:

server.js

var express = require('express')();
var io = require('socket.io');

io.listen(express);

但我在终端运行它,它给我一个错误:

% node test

Socket.IO's `listen()` method expects an `http.Server` instance
as its first parameter. Are you migrating from Express 2.x to 3.x?
If so, check out the "Socket.IO compatibility" section at:
https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x

1 个答案:

答案 0 :(得分:2)

这段代码可以解决问题:

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

io.on('connection', function(socket) {
    //emit and listen messages
});

app.get('/', function(req, res) {
    //do something
});