在我现在正在处理的项目中,我需要为连接到 TCP 服务器的每个客户端提供不同的上下文...我将在隔离上下文中在 VM 上运行的代码
var sbox = { Client : require("./Client"), server: {clients:[]}, socket:socket };
vm.createContext(sbox)
vm.runInNewContext(`
let client = new Client(socket);
server.clients.push(client);
`, sbox)
为了更清楚我需要做什么:
- 应用程序的主要组件是核心,应用程序的每个部分都扩展了核心......
-核心需要访问会话对象(现在作为参数)
- 在出现在服务器上的每个连接上,我需要创建一个新会话来处理客户端数据,如登录名、套接字、会话 ID 等......现在我必须通过参数将会话传递给每个新实例继承自核心...
或者
以某种方式创建一个仅存在于新客户端实例中的全局变量,它将处理会话对象,该对象应该可以递归访问我将在客户端实例中调用的所有内容。
//client module
const d = require('./sth');
function client(socket){
glob_ses = new Clien_stession(socket);
let a = new b();
b.start();
}
// end of module
//sth module
function client(socket){
let login = glob_ses.getLogin
}
client.prototype.start() = ()=>{
//start app that makes use of the login or some other staff that session will handle
}
// end of module
server.connection = net.createServer((socket) => {
// code
const client = require('./client');
let C = new client(socket)
server.client.push({clientAdr:`${socket.remoteAddress}:${socket.remotePort}`,clientIsn: C});
//code
});