Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7027193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:12:56+00:00 2026-05-28T00:12:56+00:00

When I start a new instance of GameServer, it sets up the socket and

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:12:57+00:00Added an answer on May 28, 2026 at 12:12 am

    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 common 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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a code like this <script>$(document).ready(function(){ var myPlayList = []; //document.write(whatTo); //New instance
Given the code: new Thread(new BackgroundWorker()).start(); Intuitively it feels like the BackgroundWorker instance should
I want to start a new thread using a C function, not an objective-C
I would like to start a new instance of a wcf service host from
I want a new instance that is a copy. I could instantiate from integers
I am working on a .NET program that starts a new instance of Excel,
Often times, I start a new instance of Visual Studio, just to create a
I will start new PDA project on the windows mobile and compact framework 2.0
The following code causes a NullReferenceException tStartParameter = String.Format(tStartParameter, tTo, tSubject) tProcess = Process.Start(New
Start a new Silverlight application... and in the code behind (in the Loaded event),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.