• Hi, @Gordon

    You are right!

    Follow your suggestion, I write the "C" native Mask library for websocket module on Espruino.
    On Esp32 module, I test the library for some times.
    Without Mask-lib, It takes about 64 seconds to transfer 23KB data to a websocket server.
    By using Mask-lib, It takes about 1.68 second to do the same work.
    Mask-lib can improve the performance of 'ws' about 40 times.

    All codes are here:

    jswrap_mask.h

    [#include](https://forum.espruino.com/search/?q=%23include) "jsvar.h"
    
    JsVar *jswrap_mask_4(JsVar *str);
    

    jswrap_mask.c

    [#include](https://forum.espruino.com/search/?q=%23include) "jswrap_mask.h"
    [#include](https://forum.espruino.com/search/?q=%23include) "jsinteractive.h"
    [#include](https://forum.espruino.com/search/?q=%23include) "jsvar.h"
    [#include](https://forum.espruino.com/search/?q=%23include) "jshardware.h"
    
    // Define the JavaScript class
    /*JSON{
      "type" : "class",
      "class" : "M"
    }*/
    
    // Define the `jswrap_mask_4` to be a `staticmethod` on the `M` class
    /*JSON{
      "type" : "staticmethod",
      "class" : "M",
      "name" : "mask4",
      "generate" : "jswrap_mask_4",
      "params" : [
        ["str","JsVar","An object containing a string"]
      ],
      "return" : ["JsVar","Returns a masked string object"]
    }
    Create a masked string, mask is 4 random chars.
    */
    JsVar *jswrap_mask_4(JsVar *str) {
      if (!jsvIsString(str)) {
        return jsvNewFromEmptyString();
      }
      size_t msglength = jsvGetStringLength(str);
      char *masked = (char *)jsvMalloc(msglength+1);
      jsvGetString(str, masked, msglength+1);
      //jsvUnLock(str); // crash?
      char mask[4] = {0,0,0,0};
    	//String masked = message; //jsvgetstring
    	for (int i = 0; i < 4; i++)
    	{
        char a = (char)jshGetRandomNumber();
        mask[i] = a;
    	}
    	for (int i = 0; i < msglength; i++){
        masked[i] ^= mask[i % 4];
      }
      JsVar *maskedVar = jsvNewFromEmptyString();
      jsvAppendStringBuf(maskedVar, mask, 4);
      jsvAppendStringBuf(maskedVar, masked, msglength);
      jsvFree(masked);
      return maskedVar;
    }
    

    Change in ws.js

    /** Send message based on opcode type */
    WebSocket.prototype.send = function (msg, opcode) {
      opcode = opcode === undefined ? 0x81 : opcode;
      var size = msg.length;
      if (msg.length>125) {
        size = 126;
      }
      this.socket.write(strChr(opcode, size + ( this.masking ? 128 : 0 )));
    
      if (size == 126) {
        // Need to write extra bytes for longer messages
        this.socket.write(strChr(msg.length >> 8));
        this.socket.write(strChr(msg.length));
      }
    
      if (this.masking) { // change is here
        this.socket.write(M.mask4(msg)); // using built-in lib instead of JS masking codes 
      } else {
        this.socket.write(msg);
      }
    };
    

    Espruino is flexible, Cool!

About

Avatar for Aifer @Aifer started