使用mongoose来创建嵌入式websocket客户端和http客户端
1 嵌入式非常合适
2 在简单需要的场合下,不需要其他文件,只需要两个文件包含
包含mongoose.h 和 mongoose.c 就可以使用了,比较方便,尤其在嵌入式里面,这两个文件就很有用了,不用其他依赖
#include "httpclient.h"
#include "mongoose.h"
/* RESTful server host and request URI */
//static const char *s_url =
//"http://www.abc/data/1";
static int s_exit_flag = 0;
static PageCallback gcallback = NULL;
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
struct http_message *hm = (struct http_message *) ev_data;
int connect_status;
switch (ev) {
case MG_EV_CONNECT:
connect_status = *(int *)ev_data;
if (connect_status != 0) {
printf("Error connecting to server: %s\n", strerror(connect_status));
s_exit_flag = 1;
}
break;
case MG_EV_HTTP_REPLY:
//printf("Got reply:\n%.*s\n", (int)hm->body.len, hm->body.p);
nc->flags |= MG_F_SEND_AND_CLOSE;
s_exit_flag = 1;
if (gcallback != NULL)
{
gcallback(hm->body.p, (int)hm->body.len);
}
break;
case MG_EV_CLOSE:
if (s_exit_flag == 0) {
printf("Server closed connection\n");
s_exit_flag = 1;
};
break;
default:
break;
}
}
int httpclient(const char* url, PageCallback callback)
{
struct mg_mgr mgr;
struct mg_connection *nc;
gcallback = callback;
mg_mgr_init(&mgr, NULL);
nc = mg_connect_http(&mgr, ev_handler, url, NULL, NULL);
mg_set_protocol_http_websocket(nc);
printf("Starting RESTful client against %s\n", url);
while (s_exit_flag == 0) {
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}
包含mongoose.h 和 mongoose.c 就可以使用了,比较方便,尤其在嵌入式里面,这两个文件就很有用了,不用
#include "mongoose.h"
static int s_done = 0;
static int s_is_connected = 0;
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
(void)nc;
switch (ev) {
case MG_EV_CONNECT: {
int status = *((int *)ev_data);
if (status != 0) {
printf("-- Connection error: %d\n", status);
}
break;
}
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
struct http_message *hm = (struct http_message *) ev_data;
if (hm->resp_code == 101) {
printf("-- Connected\n");
s_is_connected = 1;
}
else {
printf("-- Connection failed! HTTP code %d\n", hm->resp_code);
/* Connection will be closed after this. */
}
break;
}
case MG_EV_POLL: {
char msg[500];
int n = 0;
#ifdef _WIN32 /* Windows console input is special. */
INPUT_RECORD inp[100];
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD i, num;
if (!PeekConsoleInput(h, inp, sizeof(inp) / sizeof(*inp), &num)) break;
for (i = 0; i < num; i++) {
if (inp[i].EventType == KEY_EVENT &&
inp[i].Event.KeyEvent.wVirtualKeyCode == VK_RETURN) {
break;
}
}
if (i == num) break;
if (!ReadConsole(h, msg, sizeof(msg), &num, NULL)) break;
/* Un-unicode. This is totally not the right way to do it. */
for (i = 0; i < num * 2; i += 2) msg[i / 2] = msg[i];
n = (int)num;
#else /* For everybody else, we just read() stdin. */
fd_set read_set, write_set, err_set;
struct timeval timeout = { 0, 0 };
FD_ZERO(&read_set);
FD_ZERO(&write_set);
FD_ZERO(&err_set);
FD_SET(0 /* stdin */, &read_set);
if (select(1, &read_set, &write_set, &err_set, &timeout) == 1) {
n = read(0, msg, sizeof(msg));
}
#endif
if (n <= 0) break;
while (msg[n - 1] == '\r' || msg[n - 1] == '\n') n--;
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, msg, n);
break;
}
case MG_EV_WEBSOCKET_FRAME: {
struct websocket_message *wm = (struct websocket_message *) ev_data;
printf("%.*s\n", (int)wm->size, wm->data);
break;
}
case MG_EV_CLOSE: {
if (s_is_connected) printf("-- Disconnected\n");
s_done = 1;
break;
}
}
}
int main(int argc, char **argv) {
struct mg_mgr mgr;
struct mg_connection *nc;
const char *chat_server_url = argc > 1 ? argv[1] : "ws://127.0.0.1:8000";
mg_mgr_init(&mgr, NULL);
nc = mg_connect_ws(&mgr, ev_handler, chat_server_url, "ws_chat", NULL);
if (nc == NULL) {
fprintf(stderr, "Invalid address\n");
return 1;
}
while (!s_done) {
mg_mgr_poll(&mgr, 100);
}
mg_mgr_free(&mgr);
return 0;
}