mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-12 22:10:01 -04:00
add basic websocket support
This commit is contained in:
parent
8ad11cd515
commit
17d8c9fa8c
10 changed files with 292 additions and 29 deletions
57
client/websocket.c
Normal file
57
client/websocket.c
Normal file
|
@ -0,0 +1,57 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "curl/curl.h"
|
||||
#include "curl/websockets.h"
|
||||
|
||||
#include "types.h"
|
||||
|
||||
extern CURLM* multi_handle;
|
||||
|
||||
struct WSResult* recv_from_websocket(CURL* http_handle, int buffer_size) {
|
||||
const struct curl_ws_frame* ws_meta;
|
||||
char* buffer = malloc(buffer_size);
|
||||
size_t result_len;
|
||||
CURLcode res = curl_ws_recv(http_handle, buffer, buffer_size, &result_len, &ws_meta);
|
||||
|
||||
struct WSResult* result = malloc(sizeof(struct WSResult));
|
||||
result->buffer_size = result_len;
|
||||
result->buffer = buffer;
|
||||
result->res = (int) res;
|
||||
result->closed = (ws_meta->flags & CURLWS_CLOSE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int send_to_websocket(CURL* http_handle, const char* data, int data_len) {
|
||||
size_t sent;
|
||||
CURLcode res = curl_ws_send(http_handle, data, data_len, &sent, 0, CURLWS_BINARY);
|
||||
return (int) res;
|
||||
}
|
||||
|
||||
void close_websocket(CURL* http_handle) {
|
||||
size_t sent;
|
||||
curl_ws_send(http_handle, "", 0, &sent, 0, CURLWS_CLOSE);
|
||||
}
|
||||
|
||||
//allow the main code to automatically clean up this websocket
|
||||
void cleanup_websocket(CURL* http_handle) {
|
||||
struct RequestInfo *request_info;
|
||||
curl_easy_getinfo(http_handle, CURLINFO_PRIVATE, &request_info);
|
||||
|
||||
curl_multi_remove_handle(multi_handle, http_handle);
|
||||
curl_easy_cleanup(http_handle);
|
||||
free(request_info);
|
||||
}
|
||||
|
||||
int get_result_size (const struct WSResult* result) {
|
||||
return result->buffer_size;
|
||||
}
|
||||
char* get_result_buffer (const struct WSResult* result) {
|
||||
return result->buffer;
|
||||
}
|
||||
int get_result_code (const struct WSResult* result) {
|
||||
return result->res;
|
||||
}
|
||||
int get_result_closed (const struct WSResult* result) {
|
||||
return result->closed;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue