草庐IT

C/C++好用的websocket库

ztenv 2025-07-18 原文

Intrduction

Libwebsockets is a simple-to-use, MIT-license, pure C library providing client and server for http/1, http/2, websockets, MQTT and other protocols in a security-minded, lightweight, configurable, scalable and flexible way. It’s easy to build and cross-build via cmake and is suitable for tasks from embedded RTOS through mass cloud serving.

It supports a lot of lightweight ancilliary implementations for things like JSON, CBOR, JOSE, COSE, and supports OpenSSL and MbedTLS v2 and v3 out of the box for everything. It’s very gregarious when it comes to event loop sharing, supporting libuv, libevent, libev, sdevent, glib and uloop, as well as custom event libs.

100+ independent minimal examples for various scenarios, CC0-licensed (public domain) for cut-and-paste, allow you to get started quickly.

There are a lot of READMEs on a variety of topics.

We do a huge amount of CI testing per push, currently 582 builds on 30 platforms. You can see the lws CI rack and read about how lws-based Sai is used to coordinate all the testing.

Architecture

Serialized Secure Streams

Secure Streams APIs are also serializable, the exact same client code can fulfil the connection directly in the same process as you would expect, or forward the actions, metadata and payloads to an SS Proxy that owns the policy over a Unix Domain or TCP socket connection to be fulfilled centrally. This allows, eg, h2 streams from different processes sharing a single connection.

The serialized SS can also travel over generic transports like UART, an example is provided implementing the Binance example on an RPi Pico with a UART transport to a UART transport SS proxy, where the pico itself has no network stack, tls, compression or wss stack, but can send and receive to and from the endpoint as if it did.

The optional lws_trasport_mux is used to interpose between the UART transport and the SSPC layer, allowing a single pipe to carry many separate SS connections.

The user SS code is identical however it is transported, muxed and fulfilled.

v4.3 is released

See the changelog

Lws work retrospective

The initial commit for lws will have been 11 years ago come Oct 28 2021, it’s been a lot of work. There are a total of 4.3K patches, touching 800KLOC cumulatively (this is not the size in the repo, but over the years, how many source lines were changed by patches).

Gratifyingly, it turns out over the years, ~15% of that was contributed by 404 contributors: that’s not so bad. Thanks a lot to everyone who has provided patches.

Today at least tens of millions of devices and product features rely on lws to handle their communications including several from FAANG; Google now include lws as part of Android sources.

Support

This is the libwebsockets C library for lightweight websocket clients and servers. For support, visit

https://libwebsockets.org

and consider joining the project mailing list at

https://libwebsockets.org/mailman/listinfo/libwebsockets

You can get the latest version of the library from git:

https://libwebsockets.org/git
Doxygen API docs for development: https://libwebsockets.org/lws-api-doc-main/html/index.html

有关C/C++好用的websocket库的更多相关文章

  1. ruby - Faye WebSocket,关闭处理程序被触发后重新连接到套接字 - 2

    我有一个super简单的脚本,它几乎包含了FayeWebSocketGitHub页面上用于处理关闭连接的内容:ws=Faye::WebSocket::Client.new(url,nil,:headers=>headers)ws.on:opendo|event|p[:open]#sendpingcommand#sendtestcommand#ws.send({command:'test'}.to_json)endws.on:messagedo|event|#hereistheentrypointfordatacomingfromtheserver.pJSON.parse(event.d

  2. C/C++好用的websocket库 - 2

    IntrductionLibwebsocketsisasimple-to-use,MIT-license,pureClibraryprovidingclientandserverforhttp/1,http/2,websockets,MQTTandotherprotocolsinasecurity-minded,lightweight,configurable,scalableandflexibleway.It’seasytobuildandcross-buildviacmakeandissuitablefortasksfromembeddedRTOSthroughmasscloudservi

  3. ruby-on-rails - Websocket-rails 不适用于 Nginx 和 Unicorn 的生产环境 - 2

    我有带有gemwebsocket-rails0.7的Rails3.2应用程序。在开发机上,一切正常在生产环境中,我使用Nginx/1.6作为代理服务器,Unicorn作为http服务器。Thin用于独立模式(在https://github.com/websocket-rails/websocket-rails/wiki/Standalone-Server-Mode之后)。nginx配置:location/websocket{proxy_passhttp://localhost:3001/websocket;proxy_http_version1.1;proxy_set_headerUp

  4. WebSocket的那些事(1-概念篇) - 2

    目录一、什么是Websocket二、WebSocket部分header介绍三、HTTPVSWebSocket四、什么时候使用WebSockets五、关于SockJS和STOMP一、什么是Websocket根据RFC6455标准,Websocket协议提供了一种标准化的方式在客户端和服务端之间通过TCP连接建立全双工、双向通信渠道。它是一种不同于HTTP的TCP协议,但是被设计为在HTTP基础上运行。Websocket交互始于HTTP请求,该请求会通过HTTPUpgrade请求头去升级请求,进而切换到Websocket协议。请求报文如下:GET/spring-websocket-portfoli

  5. ruby-on-rails - 如何使用 Rails/websocket-rails(独立)调试 'Invalid frame header' Websocket 问题 - 2

    我开始使用websocket-rails,试图将旧的通知轮询系统(在Ruby2.1/Rails4.0上)转换为更现代的WS系统。我在独立模式下使用WebsocketRails,这是我的配置,基本上是默认配置:WebsocketRails.setupdo|config|config.standalone=trueend我还设置了一个在默认端口上运行的新Redis-这里似乎没有通信问题。在客户端,我添加了websocket-rails的JS,并在尝试打开连接和订阅channel时使用:@dispatcher=newWebSocketRails"localhost:3001/websocke

  6. ruby-on-rails - ActionCable - 无法在生产环境中升级到 WebSocket - 2

    ActionCable在生产中不起作用。在开发中运行良好,但在生产中运行不佳。在Ubuntu14.04上使用Puma运行Nginx。我已经检查过redis-server已启动并正在运行。Rails-v5.0.0.1production.log:INFO--:StartedGET"/cable/"[non-WebSocket]for178.213.184.193at2016-11-2514:55:39+0100ERROR--:FailedtoupgradetoWebSocket(REQUEST_METHOD:GET,HTTP_CONNECTION:close,HTTP_UPGRADE:)

  7. ruby - Sinatra 与 EventMachine WebSockets 一起使用是否成功? - 2

    我已经使用Sinatra一段时间了,我想通过websockets推送数据来为我的网络应用程序添加一些实时功能。我已经成功地单独使用了gem“em-websocket”,但无法编写一个具有sinatra网络服务器和网络套接字服务器的ruby​​文件。我试过旋转运行!或开始!方法在单独的线程中关闭但没有成功。有人用过这个吗?我想将它们放在同一个文件中,这样我就可以在两个服务器之间共享变量。 最佳答案 没试过,但应该不会太难:require'em-websocket'require'sinatra/base'require'thin'EM

  8. javascript - Reactjs:如何在组件之间共享一个 websocket - 2

    我是React的新手,我在组件结构和它们之间共享websocket方面遇到了一些问题。该应用程序由类别和产品组成。初始数据加载将通过Ajax请求完成,并且将使用websocket保持数据更新。我的组件层次结构如下所示:类别列表类别产品列表产品CategoriesList保存类别的状态,ProductsList保存类别中产品的状态。所以我想在CategoriesList和ProductsList中使用相同的websocket,但监听不同的websocket事件:category:updated和product:updated。如何在组件之间共享websocket以及初始化它的正确位置?由

  9. javascript - 聊天使用什么协议(protocol)? webRTC 或 Websockets - 2

    我正在创建一个可以进行视频、音频和文本聊天的聊天网站。我目前正在使用Websockets进行文本聊天。我需要知道视频、音频和文本聊天的最佳方式是什么。哪个更有效。我应该为所有这些使用Websockets,还是为视频和音频使用webRTC,为文本聊天使用Websockets。我可以两者都做,并且不想为webRTC重新编写我的文本聊天,但如果这是一个更好的选择,那么我会的。编辑:我刚刚发现了binaryJS,它是webRTC更好更简单的替代品吗?谢谢,@_C1D 最佳答案 通过将您的视频和音频流嵌入到websockets中,您将拥有一个

  10. javascript - 用于数据传输的 WebSockets 与 XHR - 2

    我正在使用Node.js为Web应用程序设计架构,我们需要能够将中等大小的文件从图库发送到客户端。当用户浏览图库时,他们将尽快(针对每个图库项目)发送这些二进制文件。这些文件可能高达6Mb,但可能平均约为2Mb。我的客户坚持认为我们应该使用websockets而不是XHR进行数据传输。需要明确的是,我们不需要双向通信。我缺乏这方面的经验,需要推理方面的帮助。到目前为止,我的观点如下:使用WebSockets会破坏HTTP提供的任何客户端缓存。如果用户两次访问图库中的同一项目,他们将被迫重新下载内容。WebSocket消息不能由代理缓存处理/路由到代理缓存。它们必须始终由显式服务器处理。

随机推荐