Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何获取request的socket fd? #183

Open
jarven-zhang opened this issue Mar 10, 2021 · 6 comments
Open

如何获取request的socket fd? #183

jarven-zhang opened this issue Mar 10, 2021 · 6 comments

Comments

@jarven-zhang
Copy link

jarven-zhang commented Mar 10, 2021

这是web服务端接收客户端连接的处理接口,我们需要获取客户端的唯一标识,从而管理客户端。
`

void WsServer::HandleWs(request& req, response& res)
{
if (req.get_content_type() != content_type::websocket)
{
//gLog->Log("HandleWs error");
//LogFile::GetInstance()->Log("HandleWs error");
return;
}

req.on(ws_open, [&](request& req) 
{//用户连接
});
   
req.on(ws_message, [&](request& req) 
{//接收消息
    req.get_conn<cinatra::NonSSL>()->shared_from_this()->send_ws_string("Hello, we received your request!");
  
    //echo
    auto part_data = req.get_part_data();
    std::string str = std::string(part_data.data(), part_data.length());
    req.get_conn<cinatra::NonSSL>()->shared_from_this()->send_ws_string(str);
    //std::cout << str << std::endl;
});
   
req.on(ws_close, [&](request& req) {//连接断开
    }); 
   
req.on(ws_error, [&](request& req) {
    }); 

} `
查看了一下代码,没有找到其fd的成员。

@qicosmos
Copy link
Owner

通过fd来确定唯一client标识是有问题的,因为连接可能会断开重连,重连后同一个client的fd也是不同的。你需要通过客户端发送一个唯一的key告诉服务端,服务端保存起来,重连后需要重新发送唯一key,这样才能不会因为client断线而找不到原来的client。

@jarven-zhang
Copy link
Author

通过fd来确定唯一client标识是有问题的,因为连接可能会断开重连,重连后同一个client的fd也是不同的。你需要通过客户端发送一个唯一的key告诉服务端,服务端保存起来,重连后需要重新发送唯一key,这样才能不会因为client断线而找不到原来的client。

是的。但是,我们这边的业务,不关心具体的客户端。我们只是想确认连接,同一个客户端重连,他的连接变化我们也接受。
现在我是通过req.get_conncinatra::NonSSL()->shared_from_this() 来区分每个连接。但是,现在我们碰到了一个很棘手的问题。

@jarven-zhang
Copy link
Author

jarven-zhang commented Mar 23, 2021

我仿照github上cinatra的用例6写的服务端程序,在debian系统中运行,下面是大致的代码
//客户端连接上
req.on(ws_open, [&](request& req)
{
std::cout << "ws_open ! The req:" << req.get_conncinatra::NonSSL()->shared_from_this() << std::endl;
});
//接收消息
req.on(ws_message, [&](request& req)
{
std::cout << "ws_message! The req:" << req.get_conncinatra::NonSSL()->shared_from_this() << std::endl;
});
//连接断开
req.on(ws_close, [&](request& req)
{
std::cout << "ws_close ! The req:" << req.get_conncinatra::NonSSL()->shared_from_this() << std::endl;
// dosomething
});

我创建了4个客户端连接到服务器,我期望的打印,每个连接应该是相同的,但事与愿违.....

@jarven-zhang
Copy link
Author

jarven-zhang commented Mar 23, 2021

这个是在debian上运行的打印结果:

image

@jarven-zhang
Copy link
Author

测试了很多次,都是这样,服务器程序启动之后,第一个客户端(上面的连接1),它的连接fd与发送消息的fd是不一样的。而且只有第一个这样,后续的客户端,他们的fd都是相同的。(我这里把req.get_conncinatra::NonSSL()->shared_from_this()当成是连接)

@jarven-zhang
Copy link
Author

@qicosmos 希望您能帮助我们确认一下这个问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants