我知道这是一个着名的问题,但是我的聊天应用程序遇到了问题,事实上它是一个教师,它应该可以工作,但我遇到了问题:
C:\xampp\htdocs\chat>node app.js
info - socket.io started
Express server listening on port 3000
GET /chat 200 210ms - 686
http.js:690
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.res.setHeader (C:\xampp\htdocs\chat\node_modules\express\n
ode_modules\connect\lib\patch.js:59:22)
at next (C:\xampp\htdocs\chat\node_modules\express\node_modules\connect\lib\
proto.js:153:13)
at Function.app.handle (C:\xampp\htdocs\chat\node_modules\express\node_modul
es\connect\lib\proto.js:198:3)
at Server.app (C:\xampp\htdocs\chat\node_modules\express\node_modules\connec
t\lib\connect.js:66:31)
at Manager.handleRequest (C:\xampp\htdocs\chat\node_modules\socket.io\lib\ma
nager.js:564:28)
at Server.<anonymous> (C:\xampp\htdocs\chat\node_modules\socket.io\lib\manag
er.js:118:10)
at Server.emit (events.js:117:20)
at HTTPParser.parser.onIncoming (http.js:2113:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:122:23
)
the app.js which runs the server:
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, chat = require('./routes/chat')
, socketio = require('socket.io')
, http = require('http')
, path = require('path');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/chat', chat.main);
app.get('/users', user.list);
var server = app.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = socketio.listen(server);
var clients = {};
var socketsOfClients = {};
io.sockets.on('connection', function(socket) {
socket.on('set username', function(userName) {
// Is this an existing user name?
if (clients[userName] === undefined) {
// Does not exist ... so, proceed
clients[userName] = socket.id;
socketsOfClients[socket.id] = userName;
userNameAvailable(socket.id, userName);
userJoined(userName);
} else
if (clients[userName] === socket.id) {
// Ignore for now
} else {
userNameAlreadyInUse(socket.id, userName);
}
});
socket.on('message', function(msg) {
var srcUser;
if (msg.inferSrcUser) {
// Infer user name based on the socket id
srcUser = socketsOfClients[socket.id];
} else {
srcUser = msg.source;
}
if (msg.target == "All") {
// broadcast
io.sockets.emit('message',
{"source": srcUser,
"message": msg.message,
"target": msg.target});
} else {
// Look up the socket id
io.sockets.sockets[clients[msg.target]].emit('message',
{"source": srcUser,
"message": msg.message,
"target": msg.target});
}})
socket.on('disconnect', function() {
var uName = socketsOfClients[socket.id];
delete socketsOfClients[socket.id];
delete clients[uName];
// relay this message to all the clients
userLeft(uName);
})
})
function userJoined(uName) {
Object.keys(socketsOfClients).forEach(function(sId) {
io.sockets.sockets[sId].emit('userJoined', { "userName": uName });
}) }
function userLeft(uName) {
io.sockets.emit('userLeft', { "userName": uName });
}
function userNameAvailable(sId, uName) {
setTimeout(function() {
console.log('Sending welcome msg to ' + uName + ' at ' + sId);
io.sockets.sockets[sId].emit('welcome', { "userName" : uName, "currentUsers": JSON.stringify(Object.keys(clients)) });
}, 500);}
function userNameAlreadyInUse(sId, uName) {
setTimeout(function() {
io.sockets.sockets[sId].emit('error', { "userNameInUse" : true });
}, 500);
}
那些是chat.js:
exports.main = function(req, res){
res.render('chat', { "title" : "Chat Sample" });
};
和user.js
/*
* GET users listing.
*/
exports.list = function(req, res){
res.send("respond with a resource");
};
感谢帮助