• ESP32 module, WIFI connected.

    In C native code , I send datas to a server by a socket connection.

    JsVar *req = jswrap_net_connect(options, callback, ST_NORMAL);
    JsVar *data = jsvNewFromString("Hello world" ); ///< Create a new string
    jswrap_net_socket_write(req, data);
    

    The server receives the data correctly, then it sends a response string "hello client" to ESP32.

    bool waitForInput(JsVar *req) {
      JsVarFloat now = jshGetMillisecondsFromTime(jshGetSystemTime());
    	//unsigned long now = millis();
    	while (!jswrap_stream_available(req) && ((jshGetMillisecondsFromTime(jshGetSystemTime()) - now) < 30000UL)) { jshDelayMicroseconds(200); }
      if(jswrap_stream_available(req)){
        return true;
      }
    	return false;
    }
    

    I use waitForInput to detect response data, but it always return FASLE.

    And, should I use the following function to receive data?

    JsVar *jswrap_stream_read(JsVar *parent, JsVarInt chars);
    
  • You can't write code that blocks waiting for input. It needs to use callbacks.

    I'm not quite sure what you're trying to do, but this looks like it'd be an awful lot easier in JavaScript!

  • @Gordon
    The JS module 'ws' is some slow in my code, so I want to write a C native 'ws' built-in module.

  • Personally I'd figure out which bit of the current module is slow (probably the masking) and implement just that part.

    But if you did want to implement a WS module you'd be better off adding the functionality to socketserver.c as a new type of socket.

  • @Gordon
    Good idea!
    Let me try. ;-)

  • Hi, @Gordon

    I try you suggestion, result is here:

    http://forum.espruino.com/conversations/302083/#comment13541177

    Thanks!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to get response data by socket connections in native C code?

Posted by Avatar for Aifer @Aifer

Actions