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