简体   繁体   中英

A strange feature of node.js

Noticed a strange feature of node.js .

For example:

Let's say, I've got some variable on node.js server: var user_id = '1'; , in which the stored user id which is connected to the server.

user1 have var user = 1;

user2 have var user = 2;

...

user99 have var user = 99;

But if at some point I will demand from the server variable user - I will return the id of the last user who rewrote her.

Is that right? So it should be? I thought, node.js for each user creates a flow/process ...

Thanks for your answer!

index.js

dNode({
    connect: function(data, callback) {
        IM.iUserId = data.user_id;
        IM.start();
    }
});

im.js

var IM = {
    iUserId: false,
    start: function() {
        console.log(this.iUserId);
    }
};

It seems like you have one global IM objects that all of your connections are sharing.


You can set/get a value for each socket by using socket.set and socket.get

Node.js is single thread one context, it does not create any isolated context for users like in PHP.

Everything inside is shared and cross-acessible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM