8000 Pass a class member function to onEvent? · Issue #14 · Links2004/arduinoWebSockets · GitHub
[go: up one dir, main page]

Skip to content
Pass a class member function to onEvent? #14
Closed
@marvinroger

Description

@marvinroger

My skills are kind of limited in C++. A code being better than a thousand words:

MyClass.h

#ifndef MyClass_h
#define MyClass_h

#include <WebSocketsServer.h>
#include <Hash.h>

class MyClass {
  public:
    MyClass();
    ~MyClass();
  private:
    WebSocketsServer* wss;
    int needToAccessThisValue;
    void onWsEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length);
};

#endif

MyClass.cpp

#include "MyClass.h"

MyClass::MyClass()  {
  this->wss = new WebSocketsServer(81);
  this->wss->onEvent(/* this->onWsEvent */);
  this->needToAccessThisValue = 123;
}

MyClass::~MyClass() {
  delete this->wss;
}

void MyClass::onWsEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  Serial.println(this->needToAccessThisValue);
}

How can I pass onWsEvent to the onEvent method? I can't make onWsEvent static because I need to access properties from this function (static would not allow a this).

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0