How to send a long string with WebSocket

Posted on
  • I had a problem with WebSocket when I try to send a long string(1688 Bytes in my case). I got error
    "Execution Interrupted during event processing.
    New interpreter error: LOW_MEMORY,MEMORY".

    Then I modified the ws module, made the send function can accept a json object, to prevent string copy. But in another for loop, it still make a copy of source, I made comment down below.

    g.prototype.send = function(a, b) {
        let c, f, d, e;
        try {
            if (
                ((c = a.value.length),
                    125 < a.value.length && (c = 126),
                    this.socket.write(
                        h(void 0 === b ? 129 : b, c + (this.masking ? 128 : 0))
                    ),
                    126 == c &&
                    (this.socket.write(h(a.value.length >> 8)),
                        this.socket.write(h(a.value.length))),
                    this.masking)
            ) {
                for (c = [], f = "", d = 0; 4 > d; d++) {
                    (e = Math.floor(255 * Math.random())), (c[d] = e), (f += h(e));
                }
                for (d = 0; d < a.value.length; d++) {
                    //here will make a copy of source
                    f += h(a.value.charCodeAt(d) ^ c[3 & d]);
                }
                print(process.memory());
                this.socket.write(f);
            } else this.socket.write(a);//2
        } catch (e) {
            (c = d = f = e = null), this.initializeConnection();
        }
    }
    

    I tried 5 methods(like using flash as buffer or turn source string in to array at first and exchange char in it in for loop) to fix it ,but none of them work.

    I wish to use the second method to send data, but server will emit error "MASK must be set".

    Anyone know how to deal with this? Thank you.

  • And I tried to send data piece by piece, on "drain" event of the socket, but still got LOW_MEMORY error.

  • Here is the test data (short version):

    [{"name":"6","nickname":"6","type":"dw","pin":"NodeMCU.D0","state":"auto","id":"8f00ea92fd13290a00b439c50fcf44b55adf4b5e"},{"name":"66","nickname":"66","type":"dw","pin":"NodeMCU.D1","state":"auto","id":"954d9746e2c362d436626ca9baadaf0b23f1344d"},{"name":"666","nickname":"666","type":"dw","pin":"NodeMCU.D2","state":"auto","id":"69a58c85aef959e6cfe3bf1f60304b9fd3e6a82b"},{"name":"66666","nickname":"66666","type":"dw","pin":"NodeMCU.D3","state":"auto","id":"ba057ce5419bf32c7cd8187bf59038f23e422fab"},{"name":"666666","nickname":"666666","type":"dw","pin":"NodeMCU.D5","state":"auto","id":"a207a27e844ad42e6bed12a33859e28b4fc1224c"},{"name":"66666666","nickname":"66666666","type":"dw","pin":"NodeMCU.D6","state":"auto","id":"7e1fc12b607bbef30c7120ab93a89e55d13b29e7"},{"name":"6666666666","nickname":"6666666666","type":"dw","pin":"NodeMCU.D8","state":"auto","id":"880eb9d52c07f310654c835410ff0c9bd746edbc"},{"name":"666666666666","nickname":"666666666666","type":"dw","pin":"NodeMCU.D9","state":"auto","id":"bd988671faeefd7f6504cb6967ab674933d30405"}]
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to send a long string with WebSocket

Posted by Avatar for user111618 @user111618

Actions