0

I've been working on some type of game where I have to pass a map which is available as an Array to the players. I already looked at other posts who suggested to pass the array as an object but this produces the following problem: 1. The user doesn't receive the object or the array 2. I got a script that logs whenever a client (dis)connects and it's showing me that a client always connects and disconnects. (Probably the client times out and tries to reconnect??)

The server code looks like this:

io.sockets.on('connection', onConnect);

function onConnect(socket) {
  console.log("We have a new client: " + socket.id);
  socket.emit('field', {fields});

  socket.on('disconnect', function() {
    console.log("Client " + socket.id + " has disconnected!");
  });
}

And the client code:

socket = io.connect('http://localhost:3000');

socket.on("field",
  function(data) {
    console.log(data);
  }
);

I'm able to pass a single object of the array, but not the whole one...

Edit: The code itself works with a string but not with an array that I want to be passed...

m0g3ns
  • 44
  • 7
  • `socket.emit('field', {fields});` is not correct. Where is your `fields` defined? Try `socket.emit('field', fields);` instead – messerbill Feb 14 '18 at 17:11
  • @messerbill fields is defined below and is correctly formatted and accessable. With fields as a parameter it's the same issue... – m0g3ns Feb 14 '18 at 17:35
  • don't see `fields` defined in your code – Akash Dathan Feb 14 '18 at 18:50
  • what is being printed by `console.log(data)` when array is passed? – Akash Dathan Feb 14 '18 at 19:08
  • probably because the `fields` array contains abstract objects (`fields.push(new Field())`). Try writing the data into simple arrays and then building the `Field` classes on the clientside. – Manuel Otto Feb 14 '18 at 19:10

1 Answers1

0

I found out that socket.io is not able to emit large arrays (highest array length was a bit less than 8000 (the array was filled with objects that contain 2 properties)). Probably this is some type of storage error (could be user specific)...

m0g3ns
  • 44
  • 7