Socket.io: How to limit the size of emitted data from client to the websocket server

后端 未结 5 954

I have a node.js server with socket.io. My clients use socket.io to connect to the node.js server.

Data is transmitted from clients to server in the following way:

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-11 14:18

    Well, I'll go with the Javascript side of the thing... let's say you don't want to allow users to go over a certain limit of data, you can just:

    var allowedSize = 10;
    
    Object.keys(Data).map(function( key, idx ) {
        if( idx > allowedSize ) return;
        // Do some work with Data[key]
    });
    

    this not only allows you to properly cycle through the elements of your object, it lets you limit easily. ( obviously this can also ruin your own pre-set requests )

提交回复
热议问题