-->
Page 1 of 2

Websockets help

PostPosted: Fri Sep 04, 2015 11:19 am
by Stevenelson
I've gotten the example working for Websockets in the latest build. It is successfully displaying a timestamp, but I can't seem to figure out how to send text commands via websockets.

This is probably a datatype question, the million different datatypes in C go over my head most of the time. I'm missing something in my understanding of the "payload". I would have thought I could do something like this:

Code: Select allvoid webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
        case WStype_TEXT:
           Serial.printf("[%u] get Text: %s\n", num, payload);

            if (payload=="hello"){
             // echo data back to browser
             webSocket.sendTXT(num, "yes", 3);
            } else{
             // echo data back to browser
              webSocket.sendTXT(num, "no", 2);
            }
            break;
       
    }

}


It certainly doesn't like that since payload is not a string. I get that it's not a string, I get that I have to cast it. But everything I've tried to correctly cast it keeps failing.

Can someone point me in the right direction?

Re: Websockets help

PostPosted: Fri Sep 04, 2015 11:36 am
by martinayotte
You can't compare strings like this :

Code: Select allif (payload=="hello"){


You need to use strcmp() function :

Code: Select allif (strcmp(payload , "hello") == 0){

Re: Websockets help

PostPosted: Fri Sep 04, 2015 11:49 am
by Stevenelson
Here's the error that gives me:

Error: initializing argument 1 of 'int strcmp(const char*, const char*)' [-fpermissive]
int _EXFUN(strcmp,(const char *, const char *));
^
invalid conversion from 'uint8_t* {aka unsigned char*}' to 'const char*' [-fpermissive]

Re: Websockets help

PostPosted: Fri Sep 04, 2015 12:02 pm
by martinayotte
Yes, I've forgot, use that instead :

Code: Select allif (strcmp((char *)payload , "hello") == 0){