1

i want to update time when client side is disconnected from a socket. however, the code below gives me "has no method 'indexOf'" from line "find({where: {room_id: roomId}})". anybody know why? thanks!

var roomId;
roomId = socket.on('updateStartTime', function(roomId) {
    //some code
    return roomId
});

socket.on('disconnect', function() {
    var date = new Date();

    Room.find({
        where: {
            room_id: roomId
        }
    }).complete(function(err, room) {
        if (err) console.log(err);

        room.updateAttributes({
            end_time: date
        }).success(function(room) {
            console.log("end_time: " + room.end_time);
        });
    });

    socket.broadcast.emit('updateChat', 'SERVER', socket.username + ' has disconnected...');
});

meanwhile, it works when a event is fired from socket.emit from a client side.

     roomId = socket.on('updateStartTime', function(roomId) {
    var date = new Date();
    Room.find({
        where: {
            room_id: roomId
        }
    }).complete(function(err, room) {
        if (err) console.log(err);
        room.updateAttributes({
            start_time: date
        }).success(function(st) {})
        return roomId;
    });
});
yaoxing
  • 4,003
  • 2
  • 21
  • 30
shangsunset
  • 1,585
  • 4
  • 22
  • 38
  • Dude, you misunderstood how event driven model works. are you trying to record when all the clients disconnect from a room? – yaoxing May 05 '14 at 05:22
  • @yaoxing Hi, right now the chat is only one one one. so i want to record when the only client disconnects from a room. how did i misunderstand the event driven model? – shangsunset May 05 '14 at 17:04

1 Answers1

1

basically event driven model is that you run into a certain closure (if you don't know what is a closure or how it works, have a look at this thread) when some event happens. So your code:

roomId = socket.on('updateStartTime', function (roomId) {
    // this is where the closure starts
    return roomId;
    // closure ends here
}

It seems like you are trying to get roomId from out side the closure. But this is not gonna work. The mistake in your code is:
the "return roomId" goes nowhere although you are expecting to assign it to the global roomId. Think about it, in a event driven model you never know when will this function(roomId) be executed. Thus when you assign the socket.on result to global roomId, how could it give you the roomId if it's not executed yet? And it's not gonna block there to wait for the result because nodejs' non-blocking model. Actually if you look into the document of socket.on, it returns itself immediately, which is a 'socket'. That's why you get your error.

The 2nd peace of your code works because it's inside the closure. the roomId refers to the local roomId which is an expected value. This code still doesn't give you want though because it's not executed when someone disconnected. Here's something you can try:

socket.on('updateStartTime', function (roomId) {
    socket.on('disconnect', function() {
        // your 'Room' operation here.
        socket.broadcast.emit('updateChat', 'SERVER', socket.username + ' has disconnected...');
    });
});

Keep in mind in a event driven model, it's not executed sequentially. Anything can happen at anytime. You need to do the right thing in the right place.

Community
  • 1
  • 1
yaoxing
  • 4,003
  • 2
  • 21
  • 30
  • thanks for your reply. this may also explains why i got TypeError: Converting circular structure to JSON when i tried to print out roomId with JSON.stringify in socket.on(disconnect). – shangsunset May 06 '14 at 13:42
  • i think i need to figure out a better way to obtain roomId. i need to open a new thread for this. btw, Room is a database model that will store chat info in my database. and roomId is a primary key auto_increment. – shangsunset May 06 '14 at 13:51
  • also, i have a socket.emit('updateStartTime', roomId) on client side will fire when the chat starts. so does it mean we could know when the roomId is getting passed to server side with socket.on()? – shangsunset May 06 '14 at 13:56
  • You can put the socket.on("disconnect") in the socket.on('updateStartTime') as I showed in my modified answer – yaoxing May 06 '14 at 14:51
  • thanks.your method works! thanks for helping! i think i understand event driven and closure better now :) – shangsunset May 06 '14 at 16:20