Reputation: 2168
I am using Node.js, with Socket.IO for communications to the client on my server...
For example:
On my server, I have a User
class, which contains basic information and functions about each user. Each time someone connects, a new User
object will be created and added to a users
array, with the Socket.IO client
object parsed to it. Here is the code:
// Set up server, Socket.IO, etc.
users = [];
var User = function(client) {
this.value = "some random value";
this.client = client;
this.client.on("event",function(data) {
// Do stuff with data
});
}
socket.on("connection", function(client) {
users.push(new User(client));
});
My problem is this: when receiving messages with Socket.IO .on()
, I want to do stuff the User
object which client
is owned by. But the problem is that accessing this
doesn't access the User
object, but rather the client
object (or at least I think so, but it isn't the User
but rather some Socket.IO object). Even when I refer the .on()
function to call a function in my object, like this.event
in my User
object, I still can't access my User
object with this
. I tried creating a local variable within each object called self
, and setting it to this
, like this: self = this;
, but I can't edit this
, but only self
.
Any ideas?
Upvotes: 1
Views: 399