|
@@ -38,7 +38,9 @@ int WebSocketClient::open(const char* _url, const http_headers& headers) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
hlogi("%s", url.c_str());
|
|
hlogi("%s", url.c_str());
|
|
|
- http_req_.reset(new HttpRequest);
|
|
|
|
|
|
|
+ if (!http_req_) {
|
|
|
|
|
+ http_req_.reset(new HttpRequest);
|
|
|
|
|
+ }
|
|
|
// ws => http
|
|
// ws => http
|
|
|
http_req_->url = "http" + url.substr(2, -1);
|
|
http_req_->url = "http" + url.substr(2, -1);
|
|
|
http_req_->ParseUrl();
|
|
http_req_->ParseUrl();
|
|
@@ -107,7 +109,22 @@ int WebSocketClient::open(const char* _url, const http_headers& headers) {
|
|
|
size -= nparse;
|
|
size -= nparse;
|
|
|
if (http_parser_->IsComplete()) {
|
|
if (http_parser_->IsComplete()) {
|
|
|
if (http_resp_->status_code != HTTP_STATUS_SWITCHING_PROTOCOLS) {
|
|
if (http_resp_->status_code != HTTP_STATUS_SWITCHING_PROTOCOLS) {
|
|
|
- hloge("server side not support websocket!");
|
|
|
|
|
|
|
+ // printf("websocket response:\n%s\n", http_resp_->Dump(true, true).c_str());
|
|
|
|
|
+ if (http_req_->redirect && HTTP_STATUS_IS_REDIRECT(http_resp_->status_code)) {
|
|
|
|
|
+ std::string location = http_resp_->headers["Location"];
|
|
|
|
|
+ if (!location.empty()) {
|
|
|
|
|
+ hlogi("redirect %s => %s", http_req_->url.c_str(), location.c_str());
|
|
|
|
|
+ std::string ws_url = location;
|
|
|
|
|
+ if (hv::startswith(location, "http")) {
|
|
|
|
|
+ ws_url = hv::replace(location, "http", "ws");
|
|
|
|
|
+ }
|
|
|
|
|
+ // NOTE: not triggle onclose when redirecting.
|
|
|
|
|
+ channel->onclose = NULL;
|
|
|
|
|
+ open(ws_url.c_str());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ hloge("server side could not upgrade to websocket: status_code=%d", http_resp_->status_code);
|
|
|
channel->close();
|
|
channel->close();
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|