Heim > Fragen und Antworten > Hauptteil
#!/usr/bin/env node var debug = require('debug')('my-application'); var app = require('../app'); app.set('port', process.env.PORT || 3000); var server = app.listen(app.get('port'), function() { debug('Express server listening on port ' + server.address().port); }); var chat = require('socket.io')(server); console.log(chat) io.on('connection', function(socket){ console.log('a user connected');})
我用的是express3 在www文件里添加了
var chat = require('socket.io')(server); console.log(chat) io.on('connection', function(socket){ console.log('a user connected');})
为什么说io未定义呢??
三叔2016-11-15 16:22:02
按照你的写法,你应该用
chat.on('connection',......
标准的文档示范
Using with the Express framework
Server (app.js)
var app = require('express').createServer(); var io = require('socket.io')(app); app.listen(80); app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); io.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); });
Client (index.html)