Compare commits

..

1 Commits

Author SHA1 Message Date
Benjamin Sergeant
edf0b0dce5 - WebSocket::send() sends message in TEXT mode by default
- WebSocketMessage sets a new binary field, which tells whether the received incoming message is binary or text
2019-06-09 11:55:34 -07:00
5 changed files with 77 additions and 58 deletions

View File

@@ -12,23 +12,23 @@ matrix:
- python test/run.py - python test/run.py
- make ws - make ws
# Linux # # Linux
- os: linux
dist: xenial
script:
- python test/run.py
- make ws
env:
- CC=gcc
- CXX=g++
# Clang + Linux disabled for now
# - os: linux # - os: linux
# dist: xenial # dist: xenial
# script: python test/run.py # script:
# - python test/run.py
# - make ws
# env: # env:
# - CC=clang # - CC=gcc
# - CXX=clang++ # - CXX=g++
# Clang + Linux disabled for now
- os: linux
dist: xenial
script: python test/run.py
env:
- CC=clang
- CXX=clang++
# Windows # Windows
- os: windows - os: windows

View File

@@ -1,17 +1,13 @@
# Changelog # Changelog
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## [unreleased] - 2019-06-09
### Changed
- travis CI uses g++ on Linux
## [4.0.0] - 2019-06-09 ## [4.0.0] - 2019-06-09
### Changed ### Changed
- WebSocket::send() sends message in TEXT mode by default - WebSocket::send() sends message in TEXT mode by default
- WebSocketMessage sets a new binary field, which tells whether the received incoming message is binary or text - WebSocketMessage sets a new binary field, which tells whether the received incoming message is binary or text
- WebSocket::send takes a third arg, binary which default to true (can be text too) - WebSocket::send takes a third arg, binary which default to true (can be text too)
- WebSocket callback only take one object, a const ix::WebSocketMessagePtr& msg - WebSocket callback only take one object, a const ix::WebSocketMessagePtr& msg
- Add explicit WebSocket::sendBinary method - Add explicite WebSocket::sendBinary
- New headers + WebSocketMessage class to hold message data, still not used across the board - New headers + WebSocketMessage class to hold message data, still not used across the board
- Add test/compatibility folder with small servers and clients written in different languages and different libraries to test compatibility. - Add test/compatibility folder with small servers and clients written in different languages and different libraries to test compatibility.
- ws echo_server has a -g option to print a greeting message on connect - ws echo_server has a -g option to print a greeting message on connect

View File

@@ -33,22 +33,27 @@ webSocket.disablePerMessageDeflate();
// Setup a callback to be fired when a message or an event (open, close, error) is received // Setup a callback to be fired when a message or an event (open, close, error) is received
webSocket.setOnMessageCallback( webSocket.setOnMessageCallback(
[](const ix::WebSocketMessagePtr& msg) [](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo)
{ {
if (msg->type == ix::WebSocketMessageType::Message) if (messageType == ix::WebSocketMessageType::Message)
{ {
std::cout << msg->str << std::endl; std::cout << str << std::endl;
} }
}); });
// 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 (default to TEXT mode) // Send a message to the server (default to BINARY mode)
webSocket.send("hello world"); webSocket.send("hello world");
// The message can be sent in BINARY mode (useful if you send MsgPack data for example) // The message can be sent in TEXT mode
webSocket.sendBinary("some serialized binary data"); webSocket.sendText("hello again");
// ... finally ... // ... finally ...
@@ -68,9 +73,14 @@ server.setOnConnectionCallback(
std::shared_ptr<ConnectionState> connectionState) std::shared_ptr<ConnectionState> connectionState)
{ {
webSocket->setOnMessageCallback( webSocket->setOnMessageCallback(
[webSocket, connectionState, &server](const ix::WebSocketMessagePtr msg) [webSocket, connectionState, &server](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo)
{ {
if (msg->type == ix::WebSocketMessageType::Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
std::cerr << "New connection" << std::endl; std::cerr << "New connection" << std::endl;
@@ -81,21 +91,19 @@ server.setOnConnectionCallback(
std::cerr << "id: " << connectionState->getId() << std::endl; std::cerr << "id: " << connectionState->getId() << std::endl;
// The uri the client did connect to. // The uri the client did connect to.
std::cerr << "Uri: " << msg->openInfo.uri << std::endl; std::cerr << "Uri: " << openInfo.uri << std::endl;
std::cerr << "Headers:" << std::endl; std::cerr << "Headers:" << std::endl;
for (auto it : msg->openInfo.headers) for (auto it : openInfo.headers)
{ {
std::cerr << it.first << ": " << it.second << std::endl; std::cerr << it.first << ": " << it.second << std::endl;
} }
} }
else if (msg->type == ix::WebSocketMessageType::Message) else if (messageType == ix::WebSocketMessageType::Message)
{ {
// For an echo server, we just send back to the client whatever was received by the server // For an echo server, we just send back to the client whatever was received by the server
// All connected clients are available in an std::set. See the broadcast cpp example. // All connected clients are available in an std::set. See the broadcast cpp example.
// Second parameter tells whether we are sending the message in binary or text mode. webSocket->send(str);
// Here we send it in the same mode as it was received.
webSocket->send(msg->str, msg->binary);
} }
} }
); );
@@ -326,27 +334,32 @@ The onMessage event will be fired when the connection is opened or closed. This
``` ```
webSocket.setOnMessageCallback( webSocket.setOnMessageCallback(
[](const ix::WebSocketMessagePtr& msg) [](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo)
{ {
if (msg->type == ix::WebSocketMessageType::Open) if (messageType == ix::WebSocketMessageType::Open)
{ {
std::cout << "send greetings" << std::endl; std::cout << "send greetings" << std::endl;
// Headers can be inspected (pairs of string/string) // Headers can be inspected (pairs of string/string)
std::cout << "Handshake Headers:" << std::endl; std::cout << "Handshake Headers:" << std::endl;
for (auto it : msg->headers) for (auto it : headers)
{ {
std::cout << it.first << ": " << it.second << std::endl; std::cout << it.first << ": " << it.second << std::endl;
} }
} }
else if (msg->type == ix::WebSocketMessageType::Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {
std::cout << "disconnected" << std::endl; std::cout << "disconnected" << std::endl;
// The server can send an explicit code and reason for closing. // The server can send an explicit code and reason for closing.
// This data can be accessed through the closeInfo object. // This data can be accessed through the closeInfo object.
std::cout << msg->closeInfo.code << std::endl; std::cout << closeInfo.code << std::endl;
std::cout << msg->closeInfo.reason << std::endl; std::cout << closeInfo.reason << std::endl;
} }
} }
); );
@@ -358,15 +371,20 @@ A message will be fired when there is an error with the connection. The message
``` ```
webSocket.setOnMessageCallback( webSocket.setOnMessageCallback(
[](const ix::WebSocketMessagePtr& msg) [](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo)
{ {
if (msg->type == ix::WebSocketMessageType::Error) if (messageType == ix::WebSocketMessageType::Error)
{ {
std::stringstream ss; std::stringstream ss;
ss << "Error: " << msg->errorInfo.reason << std::endl; ss << "Error: " << error.reason << std::endl;
ss << "#retries: " << msg->eventInfo.retries << std::endl; ss << "#retries: " << event.retries << std::endl;
ss << "Wait time(ms): " << msg->eventInfo.wait_time << std::endl; ss << "Wait time(ms): " << event.wait_time << std::endl;
ss << "HTTP Status: " << msg->eventInfo.http_status << std::endl; ss << "HTTP Status: " << event.http_status << std::endl;
std::cout << ss.str() << std::endl; std::cout << ss.str() << std::endl;
} }
} }
@@ -393,12 +411,17 @@ Ping/pong messages are used to implement keep-alive. 2 message types exists to i
``` ```
webSocket.setOnMessageCallback( webSocket.setOnMessageCallback(
[](const ix::WebSocketMessagePtr& msg) [](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo)
{ {
if (msg->type == ix::WebSocketMessageType::Ping || if (messageType == ix::WebSocketMessageType::Ping ||
msg->type == ix::WebSocketMessageType::Pong) messageType == ix::WebSocketMessageType::Pong)
{ {
std::cout << "pong data: " << msg->str << std::endl; std::cout << "pong data: " << str << std::endl;
} }
} }
); );

View File

@@ -200,7 +200,7 @@ namespace ix
{ {
if (client != webSocket) if (client != webSocket)
{ {
client->send(msg->str, msg->binary); client->send(msg->str);
} }
} }
} }

View File

@@ -30,7 +30,7 @@ namespace
Logger() << "New connection"; Logger() << "New connection";
connectionState->computeId(); connectionState->computeId();
Logger() << "id: " << connectionState->getId(); Logger() << "id: " << connectionState->getId();
Logger() << "Uri: " << msg->openInfo.uri; Logger() << "Uri: " << openInfo.uri;
Logger() << "Headers:"; Logger() << "Headers:";
for (auto&& it : msg->openInfo.headers) for (auto&& it : msg->openInfo.headers)
{ {
@@ -43,11 +43,11 @@ namespace
} }
else if (msg->type == ix::WebSocketMessageType::Message) else if (msg->type == ix::WebSocketMessageType::Message)
{ {
Logger() << "Message received: " << msg->str; Logger() << "Message received: " << str;
for (auto&& client : server.getClients()) for (auto&& client : server.getClients())
{ {
client->send(msg->str); client->send(str);
} }
} }
} }
@@ -89,25 +89,25 @@ namespace
} }
else if (msg->type == WebSocketMessageType::Error) else if (msg->type == WebSocketMessageType::Error)
{ {
ss << "Error ! " << msg->errorInfo.reason; ss << "Error ! " << error.reason;
log(ss.str()); log(ss.str());
testDone = true; testDone = true;
} }
else if (msg->type == WebSocketMessageType::Pong) else if (msg->type == WebSocketMessageType::Pong)
{ {
ss << "Received pong message " << msg->str; ss << "Received pong message " << str;
log(ss.str()); log(ss.str());
} }
else if (msg->type == WebSocketMessageType::Ping) else if (msg->type == WebSocketMessageType::Ping)
{ {
ss << "Received ping message " << msg->str; ss << "Received ping message " << str;
log(ss.str()); log(ss.str());
} }
else if (msg->type == WebSocketMessageType::Message) else if (msg->type == WebSocketMessageType::Message)
{ {
REQUIRE(msg->str.compare("Hey dude!") == 0); REQUIRE(str.compare("Hey dude!") == 0);
++receivedCount; ++receivedCount;
ss << "Received message " << msg->str; ss << "Received message " << str;
log(ss.str()); log(ss.str());
sendNextMessage(); sendNextMessage();
} }