我正在尝试在websocket应用程序中实现基于IP地址的速率限制器,但我无法弄清楚如何将IP地址作为字符串。我一直在搜索文档,而且没有提到IP地址。
我有一个addr: Recipient<WSMessage>
和一个上下文。
到目前为止,我已经尝试过ctx.address()
- 无法转换为字符串msg.addr.tx.0
- 不能unbox bc私有。
弄清楚了:
fn chat_route(req: &HttpRequest<WsChatSessionState>) -> Result<HttpResponse, Error> {
ws::start(
req,
WSSession {
id: 0,
hb: Instant::now(),
ip: req.connection_info().remote().to_string(),
},
)
}