-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathwebsocket_client.c
149 lines (130 loc) · 6.24 KB
/
websocket_client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
*
* Ulfius Framework example program
*
* This example program implements a websocket
*
* Copyright 2017-2022 Nicolas Mora <[email protected]>
*
* License MIT
*
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <inttypes.h>
#include <string.h>
#include <ulfius.h>
#include <unistd.h>
#include "u_example.h"
#define PORT "9275"
#define PREFIX_WEBSOCKET "/websocket"
#if defined(U_DISABLE_WEBSOCKET)
#error You must build ulfius with websocket support enabled to compile this example, check the install documentation
#else
void websocket_manager_callback(const struct _u_request * request,
struct _websocket_manager * websocket_manager,
void * websocket_manager_user_data) {
(void)(request);
#ifndef U_DISABLE_WS_MESSAGE_LIST
websocket_manager->keep_messages = U_WEBSOCKET_KEEP_INCOMING;
#endif
if (websocket_manager_user_data != NULL) {
y_log_message(Y_LOG_LEVEL_DEBUG, "websocket_manager_user_data is %s", websocket_manager_user_data);
}
// Send text message without fragmentation
if (ulfius_websocket_wait_close(websocket_manager, 2000) == U_WEBSOCKET_STATUS_OPEN) {
if (ulfius_websocket_send_message(websocket_manager, U_WEBSOCKET_OPCODE_TEXT, o_strlen("Message without fragmentation from client"), "Message without fragmentation from client") != U_OK) {
y_log_message(Y_LOG_LEVEL_ERROR, "Error send message without fragmentation");
}
}
// Send text message with fragmentation
if (ulfius_websocket_wait_close(websocket_manager, 2000) == U_WEBSOCKET_STATUS_OPEN) {
if (ulfius_websocket_send_fragmented_message(websocket_manager, U_WEBSOCKET_OPCODE_TEXT, o_strlen("Message with fragmentation from client"), "Message with fragmentation from client", 5) != U_OK) {
y_log_message(Y_LOG_LEVEL_ERROR, "Error send message with fragmentation");
}
}
// Send ping message
if (ulfius_websocket_wait_close(websocket_manager, 2000) == U_WEBSOCKET_STATUS_OPEN) {
if (ulfius_websocket_send_message(websocket_manager, U_WEBSOCKET_OPCODE_PING, 0, NULL) != U_OK) {
y_log_message(Y_LOG_LEVEL_ERROR, "Error send ping message");
}
}
// Send binary message without fragmentation
if (ulfius_websocket_wait_close(websocket_manager, 2000) == U_WEBSOCKET_STATUS_OPEN) {
if (ulfius_websocket_send_message(websocket_manager, U_WEBSOCKET_OPCODE_BINARY, o_strlen("Message without fragmentation from client"), "Message without fragmentation from client") != U_OK) {
y_log_message(Y_LOG_LEVEL_ERROR, "Error send binary message without fragmentation");
}
}
// Send JSON message without fragmentation
#ifndef U_DISABLE_JANSSON
if (ulfius_websocket_wait_close(websocket_manager, 2000) == U_WEBSOCKET_STATUS_OPEN) {
json_t * message = json_pack("{ss}", "send", "JSON message without fragmentation");
if (ulfius_websocket_send_json_message(websocket_manager, message) != U_OK) {
y_log_message(Y_LOG_LEVEL_ERROR, "Error send JSON message without fragmentation");
}
json_decref(message);
}
#endif
y_log_message(Y_LOG_LEVEL_DEBUG, "Closing websocket_manager_callback");
}
/**
* websocket_incoming_message_callback
* Read incoming message and prints it on the console
*/
void websocket_incoming_message_callback (const struct _u_request * request,
struct _websocket_manager * websocket_manager,
const struct _websocket_message * last_message,
void * websocket_incoming_message_user_data) {
(void)(request);
(void)(websocket_manager);
if (websocket_incoming_message_user_data != NULL) {
y_log_message(Y_LOG_LEVEL_DEBUG, "websocket_incoming_message_user_data is %s", websocket_incoming_message_user_data);
}
y_log_message(Y_LOG_LEVEL_DEBUG, "Incoming message, opcode: 0x%02x, mask: %d, len: %zu", last_message->opcode, last_message->has_mask, last_message->data_len);
if (last_message->opcode == U_WEBSOCKET_OPCODE_TEXT) {
y_log_message(Y_LOG_LEVEL_DEBUG, "text payload '%.*s'", (int)last_message->data_len, last_message->data);
} else if (last_message->opcode == U_WEBSOCKET_OPCODE_BINARY) {
y_log_message(Y_LOG_LEVEL_DEBUG, "binary payload");
}
}
void websocket_onclose_callback (const struct _u_request * request,
struct _websocket_manager * websocket_manager,
void * websocket_onclose_user_data) {
(void)(request);
(void)(websocket_manager);
if (websocket_onclose_user_data != NULL) {
y_log_message(Y_LOG_LEVEL_DEBUG, "websocket_onclose_user_data is %s", websocket_onclose_user_data);
o_free(websocket_onclose_user_data);
}
}
int main(int argc, char ** argv) {
struct _u_request request;
struct _u_response response;
struct _websocket_client_handler websocket_client_handler = {NULL, NULL};
char * websocket_user_data = o_strdup("my user data");
char * url = (argc>1&&0==o_strcmp("-https", argv[1]))?"wss://localhost:" PORT PREFIX_WEBSOCKET:"ws://localhost:" PORT PREFIX_WEBSOCKET;
y_init_logs("websocket_client", Y_LOG_MODE_CONSOLE, Y_LOG_LEVEL_DEBUG, NULL, "Starting websocket_client");
ulfius_init_request(&request);
ulfius_init_response(&response);
if (ulfius_set_websocket_request(&request, url, "protocol", "permessage-deflate") == U_OK) {
ulfius_add_websocket_client_deflate_extension(&websocket_client_handler);
request.check_server_certificate = 0;
if (ulfius_open_websocket_client_connection(&request, &websocket_manager_callback, websocket_user_data, &websocket_incoming_message_callback, websocket_user_data, &websocket_onclose_callback, websocket_user_data, &websocket_client_handler, &response) == U_OK) {
y_log_message(Y_LOG_LEVEL_DEBUG, "Wait for user to press <enter> to close the program");
getchar();
ulfius_websocket_client_connection_close(&websocket_client_handler);
y_log_message(Y_LOG_LEVEL_DEBUG, "Websocket closed");
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "Error ulfius_open_websocket_client_connection");
o_free(websocket_user_data);
}
} else {
y_log_message(Y_LOG_LEVEL_ERROR, "Error ulfius_set_websocket_request");
o_free(websocket_user_data);
}
ulfius_clean_request(&request);
ulfius_clean_response(&response);
y_close_logs();
return 0;
}
#endif