Error loading Scripts

I am having a problem with the networking of the project here: https://playcanvas.com/editor/scene/1171515. I have successfully gotten networking with building blocks to work, but now when I try to destroy blocks using the same networking method I get this error in the browser console:

Uncaught RangeError: Maximum call stack size exceeded
at t (socket.io.min.js:6)
at t (socket.io.min.js:6)
at t (socket.io.min.js:6)
at t (socket.io.min.js:6)
at t (socket.io.min.js:6)
at t (socket.io.min.js:6)
at t (socket.io.min.js:6)
at t (socket.io.min.js:6)
at t (socket.io.min.js:6)
at t (socket.io.min.js:6)
The socket.io.min.js:6 all shows the link to here: https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.3/socket.io.min.js

Here is my code for networking client side:

    var Network = pc.createScript('network');
    
    // static variables
    Network.id = null;
    Network.socket = null;
    
    // initialize code called once per entity
    Network.prototype.initialize = function() {
    
    var socket = io.connect('https://chicken64.glitch.me'); // Glitch hosted server
    Network.socket = socket;
    
    var onPlayerBuildN = function(name, x, y, z, tagero){
        //always directly after socket declairation
        socket.emit('otherBuild', {Name:name, X:x, Y:y, Z:z, Tagero:tagero});
    };
    var onPlayerDestroy = function(destName){
        socket.emit('otherDestroy', destName);
    };
    
    this.app.on("player:build", onPlayerBuildN);
    this.app.on("blockDestroyed", onPlayerDestroy);
    
    socket.on('buildFromOther', function(data){
        pc.app.fire("player:build2", data.Oname, data.Ox, data.Oy, data.Oz, data.Otagero);
    });
    socket.on('destroyFromOther', function(car){
        pc.app.fire("player:destroy", car.destName);
    });
    
};

Here is my code for networking server side:

    var server = require('http').createServer();
    var options = {
        cors: true
    }
    
    var io = require('socket.io')(server, options);

    
    io.sockets.on('connection', function(socket) {
            socket.on('otherBuild', function(data){
              socket.broadcast.emit('buildFromOther',        {Oname:data.Name,Ox:data.X,Oy:data.Y,Oz:data.Z,Otagero:data.Tagero});
              //console.log(data.X + ", " + data.Y + " ," + data.Z);
            });
            socket.on('otherDestroy', function(car){
              socket.broadcast.emit('destroyFromOther', car.destName);
            });
    });

    console.log ('Server started');
    server.listen(3000);

Here is the link for my server: https://glitch.com/edit/#!/chicken64?path=server.js%3A20%3A20

Any ideas on how to fix this?

Hi @Nathan_Crouch,

I think the way your networking works when destroying a cube creates a circular reference.

  1. You send otherDestroy to the server.
  2. Then you broadcast to everybody destroyFromOther.
  3. The player that sends the initial otherDestroy receives that message and fires player:destroy again.

If that’s the case, the only thing you need to do is to make sure that on step 2. you broadcast the message to all players except the original player that initiated the action.

1 Like

Hi @Leonidas ! Thanks for answering, but I am 90% sure that socket.broadcast.emit sends to everyone but the sender in step 2, and socket.emit sends to everyone. Your point about step 2 is why I put the .broadcast part in the server code, but this may not be working or the problem may be somewhere else.

1 Like

@yaustar Do you have any ideas?

I’m afraid I don’t have the time to debug the project. I agree with Leonidas that it looks like an infinite loop somewhere.