Add client support for websocket subprotocol. Look for the new addSubProtocol method for details
This commit is contained in:
@ -20,9 +20,9 @@ typedef unsigned long int nfds_t;
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <poll.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
|
@ -185,19 +185,32 @@ namespace ix
|
||||
_pingTimeoutSecs);
|
||||
}
|
||||
|
||||
WebSocketInitResult status = _ws.connectToUrl(_url, _extraHeaders, timeoutSecs);
|
||||
WebSocketHttpHeaders headers(_extraHeaders);
|
||||
std::string subProtocolsHeader;
|
||||
auto subProtocols = getSubProtocols();
|
||||
if (!subProtocols.empty())
|
||||
{
|
||||
for (auto subProtocol : subProtocols)
|
||||
{
|
||||
subProtocolsHeader += ",";
|
||||
subProtocolsHeader += subProtocol;
|
||||
}
|
||||
headers["Sec-WebSocket-Protocol"] = subProtocolsHeader;
|
||||
}
|
||||
|
||||
WebSocketInitResult status = _ws.connectToUrl(_url, headers, timeoutSecs);
|
||||
if (!status.success)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
|
||||
_onMessageCallback(
|
||||
std::make_shared<WebSocketMessage>(WebSocketMessageType::Open,
|
||||
"",
|
||||
0,
|
||||
WebSocketErrorInfo(),
|
||||
WebSocketOpenInfo(status.uri, status.headers),
|
||||
WebSocketCloseInfo()));
|
||||
_onMessageCallback(std::make_shared<WebSocketMessage>(
|
||||
WebSocketMessageType::Open,
|
||||
"",
|
||||
0,
|
||||
WebSocketErrorInfo(),
|
||||
WebSocketOpenInfo(status.uri, status.headers, status.protocol),
|
||||
WebSocketCloseInfo()));
|
||||
return status;
|
||||
}
|
||||
|
||||
@ -525,4 +538,16 @@ namespace ix
|
||||
{
|
||||
return _ws.bufferedAmount();
|
||||
}
|
||||
|
||||
void WebSocket::addSubProtocol(const std::string& subProtocol)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_configMutex);
|
||||
_subProtocols.push_back(subProtocol);
|
||||
}
|
||||
|
||||
const std::vector<std::string>& WebSocket::getSubProtocols()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_configMutex);
|
||||
return _subProtocols;
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -57,6 +57,7 @@ namespace ix
|
||||
void enablePong();
|
||||
void disablePong();
|
||||
void disablePerMessageDeflate();
|
||||
void addSubProtocol(const std::string& subProtocol);
|
||||
|
||||
// Run asynchronously, by calling start and stop.
|
||||
void start();
|
||||
@ -101,6 +102,7 @@ namespace ix
|
||||
bool isAutomaticReconnectionEnabled() const;
|
||||
void setMaxWaitBetweenReconnectionRetries(uint32_t maxWaitBetweenReconnectionRetries);
|
||||
uint32_t getMaxWaitBetweenReconnectionRetries() const;
|
||||
const std::vector<std::string>& getSubProtocols();
|
||||
|
||||
private:
|
||||
WebSocketSendInfo sendMessage(const std::string& text,
|
||||
@ -151,6 +153,9 @@ namespace ix
|
||||
static const int kDefaultPingIntervalSecs;
|
||||
static const int kDefaultPingTimeoutSecs;
|
||||
|
||||
// Subprotocols
|
||||
std::vector<std::string> _subProtocols;
|
||||
|
||||
friend class WebSocketServer;
|
||||
};
|
||||
} // namespace ix
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include "IXCancellationRequest.h"
|
||||
#include "IXSocket.h"
|
||||
#include "IXWebSocketHttpHeaders.h"
|
||||
#include "IXWebSocketInitResult.h"
|
||||
#include "IXWebSocketPerMessageDeflate.h"
|
||||
#include "IXWebSocketPerMessageDeflateOptions.h"
|
||||
#include <atomic>
|
||||
@ -18,28 +19,6 @@
|
||||
|
||||
namespace ix
|
||||
{
|
||||
struct WebSocketInitResult
|
||||
{
|
||||
bool success;
|
||||
int http_status;
|
||||
std::string errorStr;
|
||||
WebSocketHttpHeaders headers;
|
||||
std::string uri;
|
||||
|
||||
WebSocketInitResult(bool s = false,
|
||||
int status = 0,
|
||||
const std::string& e = std::string(),
|
||||
WebSocketHttpHeaders h = WebSocketHttpHeaders(),
|
||||
const std::string& u = std::string())
|
||||
{
|
||||
success = s;
|
||||
http_status = status;
|
||||
errorStr = e;
|
||||
headers = h;
|
||||
uri = u;
|
||||
}
|
||||
};
|
||||
|
||||
class WebSocketHandshake
|
||||
{
|
||||
public:
|
||||
|
36
ixwebsocket/IXWebSocketInitResult.h
Normal file
36
ixwebsocket/IXWebSocketInitResult.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* IXWebSocketInitResult.h
|
||||
* Author: Benjamin Sergeant
|
||||
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "IXWebSocketHttpHeaders.h"
|
||||
|
||||
namespace ix
|
||||
{
|
||||
struct WebSocketInitResult
|
||||
{
|
||||
bool success;
|
||||
int http_status;
|
||||
std::string errorStr;
|
||||
WebSocketHttpHeaders headers;
|
||||
std::string uri;
|
||||
std::string protocol;
|
||||
|
||||
WebSocketInitResult(bool s = false,
|
||||
int status = 0,
|
||||
const std::string& e = std::string(),
|
||||
WebSocketHttpHeaders h = WebSocketHttpHeaders(),
|
||||
const std::string& u = std::string())
|
||||
{
|
||||
success = s;
|
||||
http_status = status;
|
||||
errorStr = e;
|
||||
headers = h;
|
||||
uri = u;
|
||||
protocol = h["Sec-WebSocket-Protocol"];
|
||||
}
|
||||
};
|
||||
} // namespace ix
|
@ -12,11 +12,14 @@ namespace ix
|
||||
{
|
||||
std::string uri;
|
||||
WebSocketHttpHeaders headers;
|
||||
std::string protocol;
|
||||
|
||||
WebSocketOpenInfo(const std::string& u = std::string(),
|
||||
const WebSocketHttpHeaders& h = WebSocketHttpHeaders())
|
||||
const WebSocketHttpHeaders& h = WebSocketHttpHeaders(),
|
||||
const std::string& p = std::string())
|
||||
: uri(u)
|
||||
, headers(h)
|
||||
, protocol(p)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
@ -6,4 +6,4 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#define IX_WEBSOCKET_VERSION "7.0.0"
|
||||
#define IX_WEBSOCKET_VERSION "7.1.0"
|
||||
|
Reference in New Issue
Block a user