With NodeJS and websockets, I am trying to implement an online game server, for a multiplier game, I have people moving around with the ability to see each other, but when the server spawns a new enemy, I want to be able to broadcast the message to all connected clients.
But without the clients messaging the server first.
For example - The game spawns an enemy, send information to all clients.
My code so far:
/**************************************************
** NODE.JS REQUIREMENTS
**************************************************/
var util = require("util"),
// Utility resources (logging, object inspection, etc)
io = require("socket.io"),
// Socket.IO
Player = require("./Player").Player, // Player class
Zombie = require("./Zombie").Zombie;
// Socket controller
var socket,
// Array of connected players
players,
zombies;
var level = 1,
score = 0,
levelling = false;
function init() {
// Create an empty array to store players
players = [];
zombies = [];
// Set up Socket.IO to listen on port 8000
socket = io.listen(8000);
// Configure Socket.IO
socket.configure(function() {
// Only use WebSockets
socket.set("transports", ["websocket"]);
// Restrict log output
socket.set("log level", 2);
});
// Start listening for events
setEventHandlers();
};
blah blah blah -- tl;dr -- Game loop:
function mainLoop(){
//Main loop for the server. Called 60 times a second. May be modified for game
speed.
//setTimeout(mainLoop, 1000/5000);
this.broadcast.emit("update");
//THIS broadcast.emit line is where the error is being thrown. I need to know how
to broadcast here without the client initializing first.
};
I have tried a few things, socket.broadcast.emit("update");, but with no success.
I have tried for looping through currently known users and using "this.emit" but I always get "cannot call method emit of undefined" so damn annoying.
but when I have the clients send a message first, for example, when the client moves, it sends some information to the server, the server reacts with:
function onSocketConnection(client) {
util.log("New player has connected: " + client.id);
// Listen for client disconnected
client.on("disconnect", onClientDisconnect);
// Listen for new player message
client.on("new player", onNewPlayer);
// Listen for move player message
client.on("move player", onMovePlayer);
//listen for deaths
client.on("death", onPlayerDeath);
//listen for not moved message, to stop sprite animation
client.on("not moved", notMoved);
};
// Player has moved
function onMovePlayer(data) {
// Find player in array
var movePlayer = playerById(this.id);
// Player not found
if (!movePlayer) {
util.log("Player not found: "+this.id + " when asking if moved");
return;
};
// Update player position
movePlayer.setX(data.x);
movePlayer.setY(data.y);
movePlayer.setDir(data.dir);
// Broadcast updated position to connected socket clients
this.broadcast.emit("move player", {id: movePlayer.id, x:
movePlayer.getX(), y:movePlayer.getY(), dir: movePlayer.getDir()});
};
AND THEN the "this.broadcast.emit" has no issues. Without throwing the error.
OK, I solved this annoyance. I will post the answer for anyone else in the same boat. I had to remove the socket variable, and change the io require call from io = require("socket.io") to:
io = require("socket.io").listen(8000)
With this I then needed to change all corresponding variables which were dependent on the socket variable, for example:
socket.sockets.on("connection", onSocketConnection);
io.sockets.on("connection", onSocketConnection);
And then broadcasting is fine, for example: io.sockets.emit("updates")