我有一个应该响应 GET 请求的 websocket 服务器(监听环回接口)。这是我的做法
ws_.async_accept_ex(
[self = shared_from_this()](websocket::response_type& res) //
{
if (res.result_int() == 400) { // bad request, assume HTTP GET
osstream response;
/* invoke request handlers */
for (auto& do_handle : self->handlers) {
do_handle({ "GET" }, response);
/* if responded, assing response to the body*/
if (response.tellp()) {
res.content_length(response.str().size());
res.body() = std::move(response.str());
break;
}
}
}
},
net::bind_executor(strand_,
[self = shared_from_this()](beast::error_code ec) {
self->on_accept(ec);
}));
但我还需要能够处理位置(如localhost:2019/some_location
)。
有办法吗?
解决方法:手动读取表头,检查是否升级
beast::flat_buffer buffer_;
websocket::request_type req_;
/*
...
...
*/
http::async_read(ws_.next_layer(), buffer_, req_,
[self=shared_from_this()](beast::error_code ec, size_t)
{
if(ec){
fail(ec, "[http] read");
return;
}
if(websocket::is_upgrade(self->req_)){
self->ws_.accept(self->req_,ec);
self->on_accept(ec);
}
else{
osstream response;
websocket::response_type res;
vector<string_view> req_args
{ "GET", self->req_.base().target().begin() };
/* invoke request handlers */
for(auto &do_handle : self->handlers){
do_handle(req_args, response);
/* if responded, assing response to the body*/
if(response.tellp()){
res.content_length(response.str().size());
res.body() = std::move(response.str());
break;
}}
http::write(self->ws_.next_layer(), res);
}
});