对于高性能的 websocket 服务器,理想情况下我想重新定位 Iron 以监听 websockets 而不是 http(s)。
是否可以使用 rust-websocket与 iron ,还是将两者一起使用没有意义?
如果可能,我该如何实现?
最佳答案
既然你的目标是创建一个高性能的 websocket 服务器,那么从一个 HTTP 服务器开始,比如 Iron,可能没有意义。 (Iron 基于 Hyper ,它宣称自己是“快速且正确的 HTTP 实现”)。我建议查看 tokio它被设计为“一个异步的、事件驱动的平台”,并被 Hyper 和 Iron 使用。
WebSockets 需要一种不同的协议(protocol)来创建双向交互式通信 session 。来自 Mozilla docs :
you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
因此,如果您不需要 HTTP,那么从一个专注于请求/响应的服务器开始可能会引入更多的复杂性而不是好处。同时iron websocket issue仍然开放,recent comment备注:
Personally I think it's pretty difficult to fit websocket into Iron's request-middleware-response model. I haven't seen elegant abstraction in other languages for this.
如果您真的想探索将 WebSockets 与 Iron 结合使用,您需要扩展 hyper 以支持 WebSockets (good discussion here),然后访问较低级别的 super 连接(在 iron issue #478 中进行了解释)。建立连接后,WebSocket 库将很有用(尽管 rust-websocket 似乎不再维护)。
关于websocket - 在 Iron 中使用 rust-websocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30907776/