Answer the question
In order to leave comments, you need to log in
How to create a personal object for each user?
How to do this example so that the new object is not written to Users = [] but is separate for each user. What would not be stored in a common array.
/index
var express = require('express')
, app = express()
, path = require('path')
, server = require('http').createServer(app)
, io = require('socket.io')(server)
, obj = require("./user");
server.listen(80,()=> {
var Users = [];
for(user of [{id:1,name:'Вася'},{id:2,name:'Петя'}]){
Users[user.id] = new obj(user);
Users[user.id].Say('Привет');
}
io.on('connection',(socket) => {
socket.on('set name',(user, res) => {
Users[user.id].Name =user.name;
});
});
});
app.use(express.static(path.join(__dirname, 'publuc')));
module.exports = function (user) {
this.Id = user.id;
this.Name = user.name;
this.Say=(word)=> {
console.log(this.Name+':'+word);
};
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question