目录
1、服务器
2、客户端
libwebsockets是一个C语言编写的轻量级WebSocket库,它提供了一个简单的API,可以用于创建WebSocket服务器和客户端。
以下是使用libwebsockets编写WebSocket服务器的简单步骤:
(1)安装libwebsockets库
可以通过以下命令安装libwebsockets库:
sudo apt-get install libwebsockets-dev
(2)创建WebSocket服务器
下面是一个简单的WebSocket服务器的代码:
#include
#include
#include
#include
static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
{
return 0;
}
static int callback_websocket(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
{
switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
printf("WebSocket connection established\n");
break;
case LWS_CALLBACK_RECEIVE:
printf("Received data: %s\n", (char *)in);
break;
case LWS_CALLBACK_CLOSED:
printf("WebSocket connection closed\n");
break;
default:
break;
}
return 0;
}
static struct lws_protocols protocols[] = {
{
"http",
callback_http,
0,
0,
},
{
"websocket",
callback_websocket,
0,
0,
},
{ NULL, NULL, 0, 0 } /* end of list */
};
int main(int argc, char **argv)
{
struct lws_context_creation_info info;
struct lws_context *context;
int port = 9000;
memset(&info, 0, sizeof(info));
info.port = port;
info.protocols = protocols;
context = lws_create_context(&info);
if (!context) {
printf("Error creating WebSocket context\n");
return 1;
}
printf("WebSocket server started on port %d\n", port);
while (1) {
lws_service(context, 50);
}
lws_context_destroy(context);
return 0;
}
(3)编译和运行服务器
可以使用以下命令编译服务器:
gcc -o server server.c -lwebsockets
然后运行服务器:
./server
现在WebSocket服务器已经启动了,可以使用浏览器或其他WebSocket客户端连接到它。
以上是使用libwebsockets库创建WebSocket服务器的简单教程,希望对你有所帮助。
以下是使用libwebsockets库创建一个基本的WebSocket客户端的示例代码:
#include
#include
#include
#include
static int callback_websocket(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
{
switch (reason) {
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
fprintf(stderr, "Error connecting to server\n");
break;
case LWS_CALLBACK_CLIENT_ESTABLISHED:
fprintf(stderr, "Connected to server\n");
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
fprintf(stderr, "Received data: %s\n", (char *)in);
break;
case LWS_CALLBACK_CLOSED:
fprintf(stderr, "Disconnected from server\n");
break;
default:
break;
}
return 0;
}
int main(int argc, char **argv)
{
struct lws_context *context;
struct lws *websocket;
struct lws_client_connect_info connect_info;
memset(&connect_info, 0, sizeof(connect_info));
connect_info.context = context;
connect_info.address = "localhost";
connect_info.port = 8000;
connect_info.path = "/";
connect_info.host = connect_info.address;
connect_info.origin = connect_info.address;
connect_info.protocol = NULL;
connect_info.ssl_connection = 0;
struct lws_protocols protocols[] = {
{ "default", callback_websocket, 0, 0 },
{ NULL, NULL, 0, 0 }
};
context = lws_create_context(NULL, NULL, protocols, NULL, NULL, NULL, NULL, -1, -1, 0);
if (context == NULL) {
fprintf(stderr, "Error creating context\n");
return 1;
}
websocket = lws_client_connect_via_info(&connect_info);
if (websocket == NULL) {
fprintf(stderr, "Error connecting to server\n");
return 1;
}
while (lws_service(context, 0) >= 0);
lws_context_destroy(context);
return 0;
}
这个示例代码创建了一个WebSocket客户端,连接到本地主机的8000端口,并打印收到的任何数据。您可以将connect_info.address和connect_info.port更改为连接到其他WebSocket服务器。
在main()函数中,我们首先设置连接信息结构体connect_info,然后创建一个lws_context对象。接下来,我们调用lws_client_connect_via_info()连接到WebSocket服务器,并在while循环中使用lws_service()处理任何传入的数据或事件。最后,我们销毁lws_context对象并返回0,表示程序成功结束。