1

I need to connect my esp32 with websocket server and it works fine with postman as I can set header that is

**KEY        |      Value**
serialNo     |     123456

in this header, Block key has token named 'serialNo' and it's value is 123456.

I don't know how to send this header when you begin the web socket for handshaking.

I tried using

setExtraHeader()

But I don't know how to specifically use it. I'd appreciate your help and if you don't understand anything feel free to ask. And I'm using VS code with IDE extension to program it.

I want to connect with

e.g. ws://api.sdasdaf.com/abc/def   // It could be any websocket server with header token required to be connected.

I'm looking for response.

1 Answers1

0

I've added and extra header with command

websockets ws; 
websocket.begin(host,80,url);
websocket.setExtraHeader("serialNo: 123456");

It does make connection but it failed frequently. It gives output on serial monitor like:

[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
Code 101[WSc] Connected to url: /api/WebSocket/fridge
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
Code 101[WSc] Connected to url: /api/WebSocket/fridge
[WSc] Disconnected!

Other method I've tried is tweaking WebSocketClient.cpp file. at line 630:

if(ws_header) {
        handshake += WEBSOCKETS_STRING(
            "Connection: Upgrade\r\n"
            "Upgrade: websocket\r\n"
            "Sec-WebSocket-Version: 13\r\n"
            "serialNo: 123456\r\n"    // Added this line
            "Sec-WebSocket-Key: ");
        handshake += client->cKey + NEW_LINE;

Added that serialNo . . line. It gives me output on serial monitor like :

[WSc] Disconnected!
Code 101[WSc] Connected to url: /api/WebSocket/fridge
[WSc] Disconnected!
Code 101[WSc] Connected to url: /api/WebSocket/fridge
[WSc] Disconnected!
Code 101[WSc] Connected to url: /api/WebSocket/fridge
[WSc] Disconnected!
Code 101[WSc] Connected to url: /api/WebSocket/fridge
[WSc] Disconnected!
Code 101[WSc] Connected to url: /api/WebSocket/fridge
[WSc] Disconnected!
Code 101[WSc] Connected to url: /api/WebSocket/fridge

It shows this alternative behavior.

I'm still trying to figure it out but I don't know how to make stable handshake.