出于某种原因,socket.io 在它应该只发出一条消息时发出了多条消息。我认为这也导致了脱节。任何想法为什么会发生这种情况?
var Campfire = require("../lib/campfire").Campfire,
express = require("express"),
app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(1337);
app.set('views', __dirname + '/tpl');
app.set('view engine', "jade");
app.engine('jade', require('jade').__express);
app.use(express.static(__dirname + '/public'));
app.get("/", function(req, res){
res.render("page");
});
// catch all random errors and print them out
process.on('uncaughtException', function (err){
console.error(err);
});
var instance = new Campfire({
ssl : true,
token : "MYTOKEN",
account : "MYACCOUNT"
});
//////////////////
// EMPLOYEES
//////////////////
var lukasz = {name: "Lukasz", id: 1216806, status: ""};
var dana = {name: "Dana", id: 1054576, status: ""};
var users = [lukasz, dana];
function returnUsernameUsingID(userID){
for (var j=0; j<users.length; j++) {
if (users[j].id == userID) return users[j].name;
}
return -1;
}
function wordIsInSentence(theWord, theSentence){
if (theSentence.indexOf(theWord) > -1) {
return true;
} else {
return false;
}
}
////////////////
// Campfire API
////////////////
instance.join(571821, function(error, room) {
room.listen(function(message) {
var username = returnUsernameUsingID(message.userId);
if (wordIsInSentence("wfh", message.body)) {
room.speak(username+" is working from home");
io.sockets.emit('message', { message: username+",wfh" });
}else if (wordIsInSentence("back", message.body)) {
room.speak(username+" is back");
io.sockets.emit('message', { message: username+",back" });
}else if (wordIsInSentence("out", message.body)) {
room.speak(username+" is out");
io.sockets.emit('message', { message: username+",out" });
}else if (wordIsInSentence("help", message.body)) {
console.log(message);
room.speak("your user ID is: "+message.userId+"\n/wfh - working from home \n/back - in office and available \n/headsdown - in office but busy \n/sick - outsick \n/vacation - on vacation \n/out - out for the day");
}else if (wordIsInSentence("id", message.body)) {
console.log(message);
room.speak("your user ID is: "+message.userId);
}else {
console.log("Received unknown message:");
console.log(message);
}
});
});
https://gist.github.com/hilukasz/6526969
有时没关系,有时它会发送多条消息,如下所示:
debug - websocket writing 5:::{"name":"message","args":[{"message":"Lukasz,back"}]}
debug - websocket writing 5:::{"name":"message","args":[{"message":"-1,back"}]}
debug - websocket writing 5:::{"name":"message","args":[{"message":"-1,back"}]}
debug - websocket writing 5:::{"name":"message","args":[{"message":"-1,back"}]}
debug - websocket writing 5:::{"name":"message","args":[{"message":"-1,back"}]}