Question

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;
    });
});
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top