最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling functions on socket message (socket.ionode.js) - Stack Overflow

programmeradmin0浏览0评论

When I start a new instance of GameServer, it sets up the socket and listeners as follows.

var GameServer = function() {
    this.player = new Player();
    var that = this;

    // Setup sockets and listeners
    var socket = io.listen(8000);
    socket.sockets.on('connection', function(client) {
        client.on('message', that.onSocketMessage);
        client.on('disconnect', that.onSocketDisconnect);
    });
}

I then have two prototypes GameServer.prototype.onSocketMessage & onSocketDisconnect.

I have two problems with the current code:

  1. Using that = this and the closure? function. Looks ugly.

  2. When onSocketMessage is called, the idea is it works out what the message is then calls another function within GameServer. Only this isn't possible as now this belongs to the socket.io system. See below:

...

function onSocketMessage() {
    this.player.move();
}

this.player is no longer available as this. is no longer part of GameServer.

Should my socket setup and message passing be handled outside of GameServer function and prototypes?

Or how else could I resolve this?

Cheers

EDIT

Ok so I have tried this and it works but looks pretty ugly I think:

var socket = io.listen(8000);
socket.sockets.on('connection', function(client) {
    client.on('message', function (data) {
        that.onSocketMessage(this, that, data);
    });
    client.on('disconnect', function() {
        that.onSocketDisconnect(this, that);
    });
});

Can it be improved upon?

When I start a new instance of GameServer, it sets up the socket and listeners as follows.

var GameServer = function() {
    this.player = new Player();
    var that = this;

    // Setup sockets and listeners
    var socket = io.listen(8000);
    socket.sockets.on('connection', function(client) {
        client.on('message', that.onSocketMessage);
        client.on('disconnect', that.onSocketDisconnect);
    });
}

I then have two prototypes GameServer.prototype.onSocketMessage & onSocketDisconnect.

I have two problems with the current code:

  1. Using that = this and the closure? function. Looks ugly.

  2. When onSocketMessage is called, the idea is it works out what the message is then calls another function within GameServer. Only this isn't possible as now this belongs to the socket.io system. See below:

...

function onSocketMessage() {
    this.player.move();
}

this.player is no longer available as this. is no longer part of GameServer.

Should my socket setup and message passing be handled outside of GameServer function and prototypes?

Or how else could I resolve this?

Cheers

EDIT

Ok so I have tried this and it works but looks pretty ugly I think:

var socket = io.listen(8000);
socket.sockets.on('connection', function(client) {
    client.on('message', function (data) {
        that.onSocketMessage(this, that, data);
    });
    client.on('disconnect', function() {
        that.onSocketDisconnect(this, that);
    });
});

Can it be improved upon?

Share Improve this question edited Jan 6, 2012 at 5:22 Chris Evans asked Jan 6, 2012 at 4:30 Chris EvansChris Evans 9932 gold badges13 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Two things that may help. Thing the first:

You can modify a function's vision of this using the bind method.

socket.sockets.on('connection', function(client) {
    client.on('message', this.onSocketMessage);
    client.on('disconnect', this.onSocketDisconnect);
}.bind(this));

Notice the call to bind(this) at the end of the function; this instructs JavaScript to create a closure for you, making whatever this is outside the function, this inside the function. (If you wanted to make this inside the function, say, MySomething, you could just as easily call bind(MySomething), though bind(this) is the most mon use).

Thing the second:

You can store data in a Socket.IO socket. So, for example, if one socket is always associated with a player, you can do

socket.set('player', player, function() {
  // callback is called when variable is successfully stored
  console.log('player has been stored');
});

// and later

socket.get('player', function(err, player) {
  // callback is called either with an error set or the value you requested
  player.move();
});

The get and set methods take callbacks because the Socket.IO data store can be set to something other than an in-memory store; for example, Redis.

发布评论

评论列表(0)

  1. 暂无评论