1

何らかの理由で、socket.io が複数のメッセージを送信するはずですが、1 つだけを送信しています。これも切断の原因になると思います。なぜこれが起こっているのですか?

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"}]}
4

0 に答える 0