Darbininkai Broliai
Darbininkai Broliai

Reputation: 159

node.js use local variables in imports

I'm working on a small project - online game in node.js, learning it at the same time. I started off with one big server file server.js where I did all http, https, socket handling, however, as it began growing in size I figured I should keep my classes and functions in other files. Now I have a file functions.js, which looks like this:

//functions.js
//broadcast message to all clients
exports.broadcast = function(msg, wss) {
wss.clients.forEach(function (client) {
 client.send(msg);    
  });
}

And the excerpt of interest from server.js is this:

var wss = new WebSocketServer({server: secureServer});
wss.on("connection", function connection(ws) {
//a single websocket starts here
  var thisSocket = new ClientSocket(ws);

  ws.on("close", function close() {
  //remove websocket from sockets, but wait 5 seconds to avoid refresh disconnects

  for (var i = 0; i < clientSockets.length; i++) {
  if (clientSockets[i].socket === ws) {
  clientSockets.splice(i, 1);
  break;
  }
  }
  //only inform if disconnected user had username
  if (typeof thisSocket.username != "undefined") {
  var message = {};
  message.type = "disconnected";
  message.username = thisSocket.username;
  functions.broadcast(JSON.stringify(message), wss);
  }
  });

Everything works fine, however, my question is - how do I tell the function "broadcast()" to use the local variable wss instead of having to specify in the function call

exports.broadcast = function(msg) {

Is this possible? Because it would help simplify the code a lot, as in other functions I will have to provide 3, 4 or even more variables to the function, whereas if the function was inside the same file it would only use 1 variable, for example.

Thank you in advance!

Upvotes: 2

Views: 675

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074266

You can make functions.js a builder for the broadcast function, like so:

//functions.js
//broadcast message to all clients
exports.getBroadcast = function(wss) {
  return function(msg) {
    wss.clients.forEach(function (client) {
      client.send(msg);    
   });
  };
};

Then in your app file that imports it:

var functions = require('./functions.js');
var broadcast = functions.getBroadcast(wss);

Then use broadcast, which has wss baked into it.

And of course, if functions.js has several functions in it that need wss, you can define them all in one get call.


Re your comment:

I am trying this: var functions = require("./functions.js").getFunctions(wss, clientSockets, gameRooms); var broadcast = functions.broadcast; var broadcastNewClient = functions.broadcastNewClient; var dataOnJoin = functions.dataOnJoin; I get an error "undefined".

That usage should be fine, provided getFunctions looks like this:

exports.getFunctions = function(wss, clientSockets, gameRooms) {
  return {
    broadcast: function(msg) {
      // ... implementation using `msg` and `wss`...
    },
    broadcastNewClient: function(/*...args...*/) {
      // ... implementation...
    },
    dataOnJoin: function(/*...args...*/) {
      // ... implementation...
    }
  };
};

Upvotes: 1

Related Questions