如何在Linux Shell中读取WebSocket响应

curl “wss://ws-feed.gdax.com”
curl: (1) Protocol “wss” not supported or disabled in libcurl

推荐使用

# install
npm install -g wscat

# use
wscat -c "wss://ws-feed.gdax.com"

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux C++实现WebSocket客户端和服务端,需要使用第三方库来处理WebSocket协议的数据帧的解析和封装。以下是一些常用的WebSocket库: - libwebsockets:这是一个轻量级、高性能的C库,支持WebSocket客户端和服务端,以及HTTP客户端和服务端。它还支持SSL和TLS加密协议。 - WebSocket++:这是一个C++的WebSocket库,支持WebSocket客户端和服务端,以及TLS加密协议。它还提供了一些高级功能,如消息压缩和自定义协议扩展。 - Boost.Beast:这是Boost库一个HTTP和WebSocket库,支持WebSocket客户端和服务端,以及SSL加密协议。它提供了一个简单的API来处理WebSocket协议的数据帧。 下面是一个简单的示例代码,演示如何使用libwebsockets库实现一个WebSocket客户端和服务端: ```c++ // WebSocket客户端 #include <libwebsockets.h> int main() { struct lws_context_creation_info info; struct lws_client_connect_info connect_info; struct lws *wsi = NULL; const char *address = "ws://localhost:8080"; memset(&info, 0, sizeof(info)); info.port = CONTEXT_PORT_NO_LISTEN; info.protocols = NULL; info.gid = -1; info.uid = -1; struct lws_context *context = lws_create_context(&info); if (context == NULL) { return -1; } memset(&connect_info, 0, sizeof(connect_info)); connect_info.context = context; connect_info.address = address; connect_info.port = 0; connect_info.path = "/"; connect_info.host = lws_canonical_hostname(context); connect_info.origin = connect_info.host; connect_info.protocol = "chat"; connect_info.ssl_connection = 0; wsi = lws_client_connect_via_info(&connect_info); if (wsi == NULL) { lws_context_destroy(context); return -1; } while (true) { lws_service(context, 50); } lws_context_destroy(context); return 0; } // WebSocket服务端 #include <libwebsockets.h> static int callback_echo(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { switch (reason) { case LWS_CALLBACK_ESTABLISHED: { printf("Connection established\n"); break; } case LWS_CALLBACK_RECEIVE: { printf("Received data: %s\n", (char *)in); lws_write(wsi, (unsigned char *)in, len, LWS_WRITE_TEXT); break; } default: break; } return 0; } int main() { struct lws_context_creation_info info; struct lws_protocols protocol; struct lws_context *context = NULL; memset(&info, 0, sizeof(info)); info.port = 8080; info.protocols = &protocol; info.gid = -1; info.uid = -1; memset(&protocol, 0, sizeof(protocol)); protocol.name = "chat"; protocol.callback = callback_echo; protocol.per_session_data_size = 0; protocol.rx_buffer_size = 0; context = lws_create_context(&info); if (context == NULL) { return -1; } while (true) { lws_service(context, 50); } lws_context_destroy(context); return 0; } ``` 这个示例代码使用libwebsockets库分别实现了一个WebSocket客户端和服务端。在客户端,它创建了一个WebSocket连接,并发送和接收数据。在服务端,它监听端口8080,并接收客户端的连接请求,收到数据后将其原样发送回去。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

死磕音视频

你的打赏是我分享的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值