can send TEXT message (we only support BINARY messages now)
This commit is contained in:
parent
d41b7f64e4
commit
f029321664
@ -46,9 +46,12 @@ webSocket.setOnMessageCallback(
|
|||||||
// Now that our callback is setup, we can start our background thread and receive messages
|
// Now that our callback is setup, we can start our background thread and receive messages
|
||||||
webSocket.start();
|
webSocket.start();
|
||||||
|
|
||||||
// Send a message to the server
|
// Send a message to the server (default to BINARY mode)
|
||||||
webSocket.send("hello world");
|
webSocket.send("hello world");
|
||||||
|
|
||||||
|
// The message can be sent in TEXT mode
|
||||||
|
webSocket.sendText("hello again");
|
||||||
|
|
||||||
// ... finally ...
|
// ... finally ...
|
||||||
|
|
||||||
// Stop the connection
|
// Stop the connection
|
||||||
|
@ -302,7 +302,13 @@ namespace ix
|
|||||||
WebSocketSendInfo WebSocket::send(const std::string& text,
|
WebSocketSendInfo WebSocket::send(const std::string& text,
|
||||||
const OnProgressCallback& onProgressCallback)
|
const OnProgressCallback& onProgressCallback)
|
||||||
{
|
{
|
||||||
return sendMessage(text, false, onProgressCallback);
|
return sendMessage(text, SendMessageKind::Binary, onProgressCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
WebSocketSendInfo WebSocket::sendText(const std::string& text,
|
||||||
|
const OnProgressCallback& onProgressCallback)
|
||||||
|
{
|
||||||
|
return sendMessage(text, SendMessageKind::Text, onProgressCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocketSendInfo WebSocket::ping(const std::string& text)
|
WebSocketSendInfo WebSocket::ping(const std::string& text)
|
||||||
@ -311,11 +317,11 @@ namespace ix
|
|||||||
constexpr size_t pingMaxPayloadSize = 125;
|
constexpr size_t pingMaxPayloadSize = 125;
|
||||||
if (text.size() > pingMaxPayloadSize) return WebSocketSendInfo(false);
|
if (text.size() > pingMaxPayloadSize) return WebSocketSendInfo(false);
|
||||||
|
|
||||||
return sendMessage(text, true);
|
return sendMessage(text, SendMessageKind::Ping);
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocketSendInfo WebSocket::sendMessage(const std::string& text,
|
WebSocketSendInfo WebSocket::sendMessage(const std::string& text,
|
||||||
bool ping,
|
SendMessageKind sendMessageKind,
|
||||||
const OnProgressCallback& onProgressCallback)
|
const OnProgressCallback& onProgressCallback)
|
||||||
{
|
{
|
||||||
if (!isConnected()) return WebSocketSendInfo(false);
|
if (!isConnected()) return WebSocketSendInfo(false);
|
||||||
@ -332,13 +338,22 @@ namespace ix
|
|||||||
std::lock_guard<std::mutex> lock(_writeMutex);
|
std::lock_guard<std::mutex> lock(_writeMutex);
|
||||||
WebSocketSendInfo webSocketSendInfo;
|
WebSocketSendInfo webSocketSendInfo;
|
||||||
|
|
||||||
if (ping)
|
switch (sendMessageKind)
|
||||||
{
|
{
|
||||||
webSocketSendInfo = _ws.sendPing(text);
|
case SendMessageKind::Text:
|
||||||
}
|
{
|
||||||
else
|
webSocketSendInfo = _ws.sendText(text, onProgressCallback);
|
||||||
{
|
} break;
|
||||||
webSocketSendInfo = _ws.sendBinary(text, onProgressCallback);
|
|
||||||
|
case SendMessageKind::Binary:
|
||||||
|
{
|
||||||
|
webSocketSendInfo = _ws.sendBinary(text, onProgressCallback);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case SendMessageKind::Ping:
|
||||||
|
{
|
||||||
|
webSocketSendInfo = _ws.sendPing(text);
|
||||||
|
} break;
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocket::invokeTrafficTrackerCallback(webSocketSendInfo.wireSize, false);
|
WebSocket::invokeTrafficTrackerCallback(webSocketSendInfo.wireSize, false);
|
||||||
|
@ -101,6 +101,8 @@ namespace ix
|
|||||||
|
|
||||||
WebSocketSendInfo send(const std::string& text,
|
WebSocketSendInfo send(const std::string& text,
|
||||||
const OnProgressCallback& onProgressCallback = nullptr);
|
const OnProgressCallback& onProgressCallback = nullptr);
|
||||||
|
WebSocketSendInfo sendText(const std::string& text,
|
||||||
|
const OnProgressCallback& onProgressCallback = nullptr);
|
||||||
WebSocketSendInfo ping(const std::string& text);
|
WebSocketSendInfo ping(const std::string& text);
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
@ -120,7 +122,7 @@ namespace ix
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
WebSocketSendInfo sendMessage(const std::string& text,
|
WebSocketSendInfo sendMessage(const std::string& text,
|
||||||
bool ping,
|
SendMessageKind sendMessageKind,
|
||||||
const OnProgressCallback& callback = nullptr);
|
const OnProgressCallback& callback = nullptr);
|
||||||
|
|
||||||
bool isConnected() const;
|
bool isConnected() const;
|
||||||
|
@ -742,6 +742,15 @@ namespace ix
|
|||||||
_enablePerMessageDeflate, onProgressCallback);
|
_enablePerMessageDeflate, onProgressCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WebSocketSendInfo WebSocketTransport::sendText(
|
||||||
|
const std::string& message,
|
||||||
|
const OnProgressCallback& onProgressCallback)
|
||||||
|
|
||||||
|
{
|
||||||
|
return sendData(wsheader_type::TEXT_FRAME, message,
|
||||||
|
_enablePerMessageDeflate, onProgressCallback);
|
||||||
|
}
|
||||||
|
|
||||||
void WebSocketTransport::sendOnSocket()
|
void WebSocketTransport::sendOnSocket()
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_txbufMutex);
|
std::lock_guard<std::mutex> lock(_txbufMutex);
|
||||||
|
@ -30,6 +30,13 @@ namespace ix
|
|||||||
{
|
{
|
||||||
class Socket;
|
class Socket;
|
||||||
|
|
||||||
|
enum class SendMessageKind
|
||||||
|
{
|
||||||
|
Text,
|
||||||
|
Binary,
|
||||||
|
Ping
|
||||||
|
};
|
||||||
|
|
||||||
class WebSocketTransport
|
class WebSocketTransport
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -71,6 +78,8 @@ namespace ix
|
|||||||
void poll();
|
void poll();
|
||||||
WebSocketSendInfo sendBinary(const std::string& message,
|
WebSocketSendInfo sendBinary(const std::string& message,
|
||||||
const OnProgressCallback& onProgressCallback);
|
const OnProgressCallback& onProgressCallback);
|
||||||
|
WebSocketSendInfo sendText(const std::string& message,
|
||||||
|
const OnProgressCallback& onProgressCallback);
|
||||||
WebSocketSendInfo sendPing(const std::string& message);
|
WebSocketSendInfo sendPing(const std::string& message);
|
||||||
void close();
|
void close();
|
||||||
ReadyStateValues getReadyState() const;
|
ReadyStateValues getReadyState() const;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user