Skip to content

Commit eb89bfc

Browse files
实现websocket插件
1 parent 86f8b6b commit eb89bfc

File tree

6 files changed

+36
-414
lines changed

6 files changed

+36
-414
lines changed

inc/plugin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Plugin {
4141
public:
4242
virtual ~Plugin() = default;
4343

44+
virtual void *GetInterface(const char *uuid) = 0;
45+
4446
/**
4547
* @brief 插件初始化
4648
*

plugins/websocket/websocket_client.cpp

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include "websocket_client.h"
88

9-
109
/* User defined client data structure */
1110
struct tclient_data {
1211

@@ -22,39 +21,29 @@ struct tmsg_list_elem {
2221
struct tmsg_list_elem *next;
2322
};
2423

25-
2624
/* Helper function to get a printable name for websocket opcodes */
2725
static const char *
28-
msgtypename(int flags)
29-
{
30-
unsigned f = (unsigned)flags & 0xFu;
26+
msgtypename(int flags) {
27+
unsigned f = (unsigned) flags & 0xFu;
3128
switch (f) {
32-
case MG_WEBSOCKET_OPCODE_CONTINUATION:
33-
return "continuation";
34-
case MG_WEBSOCKET_OPCODE_TEXT:
35-
return "text";
36-
case MG_WEBSOCKET_OPCODE_BINARY:
37-
return "binary";
38-
case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
39-
return "connection close";
40-
case MG_WEBSOCKET_OPCODE_PING:
41-
return "PING";
42-
case MG_WEBSOCKET_OPCODE_PONG:
43-
return "PONG";
29+
case MG_WEBSOCKET_OPCODE_CONTINUATION:return "continuation";
30+
case MG_WEBSOCKET_OPCODE_TEXT:return "text";
31+
case MG_WEBSOCKET_OPCODE_BINARY:return "binary";
32+
case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:return "connection close";
33+
case MG_WEBSOCKET_OPCODE_PING:return "PING";
34+
case MG_WEBSOCKET_OPCODE_PONG:return "PONG";
4435
}
4536
return "unknown";
4637
}
4738

48-
4939
/* Callback for handling data received from the server */
5040
static int
5141
websocket_client_data_handler(struct mg_connection *conn,
5242
int flags,
5343
char *data,
5444
size_t data_len,
55-
void *user_data)
56-
{
57-
struct tclient_data *pclient_data = (struct tclient_data *)user_data;
45+
void *user_data) {
46+
struct tclient_data *pclient_data = (struct tclient_data *) user_data;
5847
time_t now = time(NULL);
5948

6049
/* We may get some different message types (websocket opcodes).
@@ -68,7 +57,7 @@ websocket_client_data_handler(struct mg_connection *conn,
6857
/* Log output: We got some data */
6958
printf("%10.0f - Client received %lu bytes of %s data from server%s",
7059
difftime(now, pclient_data->started),
71-
(long unsigned)data_len,
60+
(long unsigned) data_len,
7261
msgtypename(flags),
7362
(is_text ? ": " : ".\n"));
7463

@@ -103,7 +92,7 @@ websocket_client_data_handler(struct mg_connection *conn,
10392
printf("\n");
10493

10594
/* ... and storing it (OOM ignored for simplicity). */
106-
p = (struct tmsg_list_elem *)malloc(sizeof(struct tmsg_list_elem));
95+
p = (struct tmsg_list_elem *) malloc(sizeof(struct tmsg_list_elem));
10796
p->timestamp = now;
10897
p->data = malloc(data_len);
10998
memcpy(p->data, data, data_len);
@@ -135,36 +124,31 @@ websocket_client_data_handler(struct mg_connection *conn,
135124
return 1;
136125
}
137126

138-
139127
/* Callback for handling a close message received from the server */
140128
static void
141129
websocket_client_close_handler(const struct mg_connection *conn,
142-
void *user_data)
143-
{
144-
struct tclient_data *pclient_data = (struct tclient_data *)user_data;
130+
void *user_data) {
131+
struct tclient_data *pclient_data = (struct tclient_data *) user_data;
145132

146133
pclient_data->closed = time(NULL);
147134
printf("%10.0f - Client: Close handler\n",
148135
difftime(pclient_data->closed, pclient_data->started));
149136
}
150137

151-
152138
/* Websocket client test function */
153-
void
154-
run_websocket_client(const char *host,
155-
int port,
156-
int secure,
157-
const char *path,
158-
const char *greetings)
159-
{
139+
void run_websocket_client(const char *host,
140+
int port,
141+
int secure,
142+
const char *path,
143+
const char *greetings) {
160144
char err_buf[100] = {0};
161145
struct mg_connection *client_conn;
162146
struct tclient_data *pclient_data;
163147
int i;
164148

165149
/* Allocate some memory for callback specific data.
166150
* For simplicity, we ignore OOM handling in this example. */
167-
pclient_data = (struct tclient_data *)malloc(sizeof(struct tclient_data));
151+
pclient_data = (struct tclient_data *) malloc(sizeof(struct tclient_data));
168152

169153
/* Store start time in the private structure */
170154
pclient_data->started = time(NULL);
@@ -216,7 +200,7 @@ run_websocket_client(const char *host,
216200
difftime(time(NULL), pclient_data->started));
217201
mg_websocket_client_write(client_conn,
218202
MG_WEBSOCKET_OPCODE_PING,
219-
(const char *)&i,
203+
(const char *) &i,
220204
sizeof(int));
221205
sleep(1);
222206
}
@@ -346,8 +330,8 @@ run_websocket_client(const char *host,
346330
while (*where != NULL) {
347331
printf("%10.0f - [%5lu] ",
348332
difftime((*where)->timestamp, pclient_data->started),
349-
(unsigned long)(*where)->len);
350-
fwrite((const char *)(*where)->data, 1, (*where)->len, stdout);
333+
(unsigned long) (*where)->len);
334+
fwrite((const char *) (*where)->data, 1, (*where)->len, stdout);
351335
printf("\n");
352336

353337
where = &((*where)->next);
@@ -373,19 +357,16 @@ run_websocket_client(const char *host,
373357
free(pclient_data);
374358
}
375359

376-
377360
Status WebsocketClient::Init(IContext *lpContext, std::string pluginName) {
378361
Websocket::Init(lpContext, pluginName);
379362
return Status::Ok();
380363
}
381364

382-
383365
Status WebsocketClient::Start() {
384366

367+
mg_init_library(0);
385368

386-
mg_init_library(0);
387-
388-
auto run = [this]()-> int32_t {
369+
auto run = [this]() -> int32_t {
389370
printf("=========================================\n");
390371
run_websocket_client("127.0.0.1", 8081, 0, "/websocket", "Hello World!");
391372
return 0;

plugins/websocket/websocket_plugin.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ Status Websocket::SendAsync(PluginServiceType in, void *lpInData) {
5252
bool Websocket::Support(PluginServiceType serviceType) {
5353
return false;
5454
}
55+
5556
Status Websocket::PostMessage(PluginServiceType in, void *lpInData) {
5657
return Status();
5758
}
5859

60+
void *Websocket::GetInterface(const char *uuid) {
61+
62+
63+
return nullptr;
64+
}
65+
5966
#ifdef __cplusplus
6067
extern "C" {
6168
const char *GetVersion() {

plugins/websocket/websocket_plugin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Websocket : public Plugin {
1414
public:
1515
~Websocket() override;
1616

17+
void *GetInterface(const char *uuid) override;
18+
1719
Status Init(IContext *lpContext, std::string pluginName) override;
1820

1921
Status Start() override;

0 commit comments

Comments
 (0)