ESP32 Websocket Server
Re: ESP32 Websocket Server
No one to help ???!!!!
Re: ESP32 Websocket Server
Hi.
Can you do this asynchronously?
I want to send a message to the ws-client from the main loop (int main ()), not from the ws-handler; and one ws-connection just fine for me.
My problem is that I can't even get the ws-connection parameters (fs, hd) correctly and got the error:
Could you please share the code to save the parameters of the current ws-connection?W (64488) httpd_ws: httpd_ws_get_frame_type: Failed to read header byte (socket FD invalid), closing socket now
Re: ESP32 Websocket Server
Hi,
I would like to know if it's possible set two uri's of type websocket inside "esp32 websocket server", but with a client using only a single socket to access both uri's.
I tried to write a javascript test code inside the web browser, but opened 2 sockets(1 socket for each uri).
I think because i create two new WebSocket(xxx uri) objects.
I dont know if i can send requests with different uri's in only a new WebSocket(xxx uri) object.
Javascript partial code:
I want to do 2 websockets requests(with different uri) using only a single socket if this is possible.
"Esp32 websocket server" partial code:
Thank's.
I would like to know if it's possible set two uri's of type websocket inside "esp32 websocket server", but with a client using only a single socket to access both uri's.
I tried to write a javascript test code inside the web browser, but opened 2 sockets(1 socket for each uri).
I think because i create two new WebSocket(xxx uri) objects.
I dont know if i can send requests with different uri's in only a new WebSocket(xxx uri) object.
Javascript partial code:
Code: Select all
var wsUri = "ws://192.168.0.9/";
var wsStateUri = "ws://192.168.0.9/serverState";
websocket = new WebSocket(wsUri);
websocketState = new WebSocket(wsStateUri);
"Esp32 websocket server" partial code:
Code: Select all
// URI handler for websocket client(s) message reception.
httpd_uri_t get_clients_message = {
.uri = "/",
.method = HTTP_GET,
.handler = get_clients_message_handler,
.user_ctx = NULL,
.is_websocket = true
};
httpd_register_uri_handler(server, &get_clients_message);
// URI handler for websocket client(s) get server state.
httpd_uri_t get_server_state = {
.uri = "/serverState",
.method = HTTP_GET,
.handler = get_server_state_handler,
.user_ctx = NULL,
.is_websocket = true
};
httpd_register_uri_handler(server, &get_server_state);
Thank's.
Re: ESP32 Websocket Server
Hi,stdenits wrote: ↑Thu Aug 06, 2020 3:56 amHi.
Can you do this asynchronously?
I want to send a message to the ws-client from the main loop (int main ()), not from the ws-handler; and one ws-connection just fine for me.
My problem is that I can't even get the ws-connection parameters (fs, hd) correctly and got the error:Could you please share the code to save the parameters of the current ws-connection?W (64488) httpd_ws: httpd_ws_get_frame_type: Failed to read header byte (socket FD invalid), closing socket now
"Can you do this asynchronously?"
Yes.
Declaring global variable to facilitate.
static httpd_handle_t server = NULL;
httpd_start(&server, &config)
I am using this function:
esp_err_t httpd_queue_work(httpd_handle_t handle, httpd_work_fn_t work, void *arg)
esp_err_t httpd_queue_work( server, ws_async_message_send, &sockfd)
static void ws_async_message_send(void *arg)
{
int* fd = (int*)arg;
printf("*fd = %d\n\n", *fd );
httpd_ws_send_frame_async(server, *fd, &ws_pkt);
// configure packet(ws_pkt) before. all four struct parameters correctly.
// declare global variable: "static httpd_ws_frame_t ws_pkt;"
// declare global variable: buffer "buf" to "ws_pkt.payload = buf" too to facilitate.
// work with global variables facilitate because local variable are destroyed and this may be your problem.
// the socket list are global too.
}
I registered two functions in httpd_config_t before start the server.
config.open_fn = opened_socket_handler;
config.close_fn = closed_socket_handler;
When the socket open the callback function "opened_socket_handler" are called.
Callback function prototype: typedef esp_err_t (*httpd_open_func_t)(httpd_handle_t hd, int sockfd);
When the socket close the callback function "closed_socket_handler" are called.
Callback function prototype: typedef void (*httpd_close_func_t)(httpd_handle_t hd, int sockfd);
These 2 callback function pass a parameter " int sockfd".
I created a global variable "list" to insert in the list the socket when the open function are called.
and delete the socket from the list when the close function are called.
Be careful with your websocket client side. See if the client are not opening more than one single socket per client. This can get you in trouble.
Websocket rfc is helpful too to understand the packet format and other things:
https://tools.ietf.org/html/rfc6455
Re: ESP32 Websocket Server
Hello, i am late, but can you attach a representation of the code you mentioned above ?
Re: ESP32 Websocket Server
Thanks baldhead. I appreciate your good work. It help a lot!Baldhead wrote: ↑Thu Aug 06, 2020 5:02 amHi,stdenits wrote: ↑Thu Aug 06, 2020 3:56 amHi.
Can you do this asynchronously?
I want to send a message to the ws-client from the main loop (int main ()), not from the ws-handler; and one ws-connection just fine for me.
My problem is that I can't even get the ws-connection parameters (fs, hd) correctly and got the error:Could you please share the code to save the parameters of the current ws-connection?W (64488) httpd_ws: httpd_ws_get_frame_type: Failed to read header byte (socket FD invalid), closing socket now
"Can you do this asynchronously?"
Yes.
Declaring global variable to facilitate.
static httpd_handle_t server = NULL;
httpd_start(&server, &config)
I am using this function:
esp_err_t httpd_queue_work(httpd_handle_t handle, httpd_work_fn_t work, void *arg)
esp_err_t httpd_queue_work( server, ws_async_message_send, &sockfd)
static void ws_async_message_send(void *arg)
{
int* fd = (int*)arg;
printf("*fd = %d\n\n", *fd );
httpd_ws_send_frame_async(server, *fd, &ws_pkt);
// configure packet(ws_pkt) before. all four struct parameters correctly.
// declare global variable: "static httpd_ws_frame_t ws_pkt;"
// declare global variable: buffer "buf" to "ws_pkt.payload = buf" too to facilitate.
// work with global variables facilitate because local variable are destroyed and this may be your problem.
// the socket list are global too.
}
I registered two functions in httpd_config_t before start the server.
config.open_fn = opened_socket_handler;
config.close_fn = closed_socket_handler;
When the socket open the callback function "opened_socket_handler" are called.
Callback function prototype: typedef esp_err_t (*httpd_open_func_t)(httpd_handle_t hd, int sockfd);
When the socket close the callback function "closed_socket_handler" are called.
Callback function prototype: typedef void (*httpd_close_func_t)(httpd_handle_t hd, int sockfd);
These 2 callback function pass a parameter " int sockfd".
I created a global variable "list" to insert in the list the socket when the open function are called.
and delete the socket from the list when the close function are called.
Be careful with your websocket client side. See if the client are not opening more than one single socket per client. This can get you in trouble.
Websocket rfc is helpful too to understand the packet format and other things:
https://tools.ietf.org/html/rfc6455
Re: ESP32 Websocket Server
Thank's for the compliment @abansal22
Who is online
Users browsing this forum: jesper and 81 guests