简体   繁体   English

node.js的一个奇怪功能

[英]A strange feature of node.js

Noticed a strange feature of node.js . 注意到node.js一个奇怪功能。

For example: 例如:

Let's say, I've got some variable on node.js server: var user_id = '1'; 假设,我在node.js服务器上有一些变量: var user_id = '1'; , in which the stored user id which is connected to the server. ,其中存储了连接到服务器的用户ID。

user1 have var user = 1; user1var user = 1;

user2 have var user = 2; user2var user = 2;

... ...

user99 have var user = 99; user99var 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. 但是,如果在某个时候我会要求服务器变量user -我将返回重写她的最后一个用户的ID。

Is that right? 那正确吗? So it should be? 应该这样吗? I thought, node.js for each user creates a flow/process ... 我以为每个用户的node.js都会创建一个flow/process ...

Thanks for your answer! 感谢您的回答!

index.js index.js

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

im.js 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. 似乎您的所有连接都共享一个全局IM对象。


You can set/get a value for each socket by using socket.set and socket.get 您可以使用socket.setsocket.get为每个套接字设置/获取一个值

Node.js is single thread one context, it does not create any isolated context for users like in PHP. Node.js是单线程一个上下文,它不会像PHP中那样为用户创建任何隔离的上下文。

Everything inside is shared and cross-acessible. 内部的所有内容都是共享的并且可以交叉访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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