These are chat archives for Links2004/arduinoWebSockets

19th
Dec 2016
battuashwik
@battuashwik
Dec 19 2016 16:49
Anyone here?
battuashwik
@battuashwik
Dec 19 2016 16:59
https://github.com/Links2004/arduinoWebSockets/blob/master/examples/WebSocketClient/WebSocketClient.ino
In this function is passed as callback to the onEvent method
Can anyhelp on how i can use this to attach a class member function
@Links2004 Can u please help with this
Markus
@Links2004
Dec 19 2016 17:01
hi, you want to register a public function of a class with onEvent ?
somethink like this will do the jop
webSocket.onEvent(std::bind(&Foo::doSomethingArgs, this, _1, _2, _3));
where this is the instance of the Foo class
_1, _2, _3 are the parameters WStype_t type, uint8_t * payload, size_t lenght
battuashwik
@battuashwik
Dec 19 2016 17:12
@Links2004
_webSocket->onEvent(
    std::bind(
      &DeviceHubConnectionManager::onWebSocketEvent,
      this,
      WStype_t type,
      uint8_t * payload,
      size_t length
    ));
Is this correct
Markus
@Links2004
Dec 19 2016 17:13
_webSocket->onEvent(
    std::bind(
      &DeviceHubConnectionManager::onWebSocketEvent,
      <instance variable here>,
     _1, _2, _3
    ));
battuashwik
@battuashwik
Dec 19 2016 17:15
its not working
i mean _1, _2, _3 is exactly what
#include "device-hubconnection-manager.h"

DeviceHubConnectionManager::DeviceHubConnectionManager(DeepStreamClient *dsClient) {
  _dsClient = dsClient;
  _webSocket = new WebSocketsClient();
  _webSocket->onEvent(
    std::bind(
      &DeviceHubConnectionManager::onWebSocketEvent,
      this,
     _1, _2, _3
  ));
}

void DeviceHubConnectionManager::_onWebSocketDisconnected() {

}

void DeviceHubConnectionManager::_onWebSocketConnected() {

}

void DeviceHubConnectionManager::_onWebSocketTxtData(String data) {

}

void DeviceHubConnectionManager::_onWebSocketBinData() {

}

void DeviceHubConnectionManager::onWebSocketEvent(
    WStype_t type, uint8_t * payload, size_t length) {
  String payloadMsg = "";

  for (int i = 0; i < length - 1; i++) {
    payloadMsg += char(payload[i]);
  }

  switch(type) {
    case WStype_DISCONNECTED:
        _onWebSocketDisconnected();
        break;

    case WStype_CONNECTED:
        _onWebSocketConnected();
        break;

    case WStype_TEXT:
        _onWebSocketTxtData(payloadMsg);
        // USE_SERIAL.printf("[WSc] get text: %s\n", payload);
        // char recordSeparator = char(31);
        // char groupSeparator = char(30);
        // int topicNameIndex = payloadMsg.indexOf(recordSeparator);
        // int actionNameIndex = payloadMsg.indexOf(recordSeparator, topicNameIndex + 1);
        // String topicName = payloadMsg.substring(0, topicNameIndex);
        // String actionName = payloadMsg.substring(topicNameIndex + 1, actionNameIndex);
        //
        // USE_SERIAL.print("topicNameIndex ::: ");
        // USE_SERIAL.println(topicNameIndex);
        // USE_SERIAL.printf("actionNameIndex ::: ");
        // USE_SERIAL.println(actionNameIndex);
        // USE_SERIAL.print("topicName ::: ");
        // USE_SERIAL.println(topicName);
        // USE_SERIAL.print("actionName ::: ");
        // USE_SERIAL.println(actionName);
        //
        // if (topicName == "C" && actionName == "CH") {
        //   webSocket.sendTXT("C" + String(char(31)) +  "CHR" + String(char(31)) +  "ws://192.168.1.6:6020/deepstream" + String(char(30)));
        // } else if (topicName == "C" && actionName == "A") {
        //   webSocket.sendTXT("A" + String(char(31)) + "REQ" + String(char(31)) +  "{\"username\":\"ashwik\",\"password\":\"test1234\"}" + String(char(30)));
        // } else if (topicName == "A" && actionName == "A") {
        //   USE_SERIAL.println("AUTHENTICATED SUCCESSFULLY");
        // }

        break;

    case WStype_BIN:
        _onWebSocketBinData();
        break;
  }
}
@Links2004 The above is my code, can u help on what exactly should be the syntax
Markus
@Links2004
Dec 19 2016 17:17
what kind of error do you get?
battuashwik
@battuashwik
Dec 19 2016 17:18

error: '_1' was not declared in this scope

_1, _2, _3

Markus
@Links2004
Dec 19 2016 17:20

ok two ways to fix this:

using namespace std::placeholders;

or

std::placeholders::_1, std::placeholders::_2, std::placeholders::_3

may you need

#include <functional>

too.

battuashwik
@battuashwik
Dec 19 2016 17:21
Ty a lot
works like a charm
Markus
@Links2004
Dec 19 2016 17:21
you are welcome