Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
197cf8ed36 | |||
dd0d7c268f | |||
b2bfccac0a | |||
8b8b352e61 | |||
0403dd354b | |||
b78b453504 | |||
f8fef833b8 |
@ -1,8 +1,15 @@
|
||||
# Changelog
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## [5.0.3] - 2019-08-14
|
||||
- CobraMetricThreadedPublisher _enable flag is an atomic, and CobraMetricsPublisher is enabled by default
|
||||
|
||||
## [5.0.2] - 2019-08-01
|
||||
- ws cobra_subscribe has a new -q (quiet) option
|
||||
- ws cobra_subscribe knows to and display msg stats (count and # of messages received per second)
|
||||
- ws cobra_subscribe, cobra_to_statsd and cobra_to_sentry commands have a new option, --filter to restrict the events they want to receive
|
||||
|
||||
## [5.0.1] - 2019-07-25
|
||||
### Unreleased
|
||||
- ws connect command has a new option to send in binary mode (still default to text)
|
||||
- ws connect command has readline history thanks to libnoise-cpp. Now ws connect one can use using arrows to lookup previous sent messages and edit them
|
||||
|
||||
|
@ -1 +1 @@
|
||||
5.0.1
|
||||
5.0.3
|
||||
|
18
README.md
18
README.md
@ -16,7 +16,7 @@
|
||||
|
||||
The [*ws*](https://github.com/machinezone/IXWebSocket/tree/master/ws) folder countains many interactive programs for chat, [file transfers](https://github.com/machinezone/IXWebSocket/blob/master/ws/ws_send.cpp), [curl like](https://github.com/machinezone/IXWebSocket/blob/master/ws/ws_http_client.cpp) http clients, demonstrating client and server usage.
|
||||
|
||||
Here is what the client API looks like.
|
||||
### WebSocket client API
|
||||
|
||||
```
|
||||
ix::WebSocket webSocket;
|
||||
@ -56,7 +56,7 @@ webSocket.sendBinary("some serialized binary data");
|
||||
webSocket.stop()
|
||||
```
|
||||
|
||||
Here is what the server API looks like. Note that server support is very recent and subject to changes.
|
||||
### WebSocket server API
|
||||
|
||||
```
|
||||
// Run a server on localhost at a given port.
|
||||
@ -117,7 +117,7 @@ server.wait();
|
||||
|
||||
```
|
||||
|
||||
Here is what the HTTP client API looks like.
|
||||
### HTTP client API
|
||||
|
||||
```
|
||||
//
|
||||
@ -170,7 +170,7 @@ out = httpClient.post(url, std::string("foo=bar"), args);
|
||||
//
|
||||
// Result
|
||||
//
|
||||
auto errorCode = response->errorCode; // Can be HttpErrorCode::Ok, HttpErrorCode::UrlMalformed, etc...
|
||||
auto statusCode = response->statusCode; // Can be HttpErrorCode::Ok, HttpErrorCode::UrlMalformed, etc...
|
||||
auto errorCode = response->errorCode; // 200, 404, etc...
|
||||
auto responseHeaders = response->headers; // All the headers in a special case-insensitive unordered_map of (string, string)
|
||||
auto payload = response->payload; // All the bytes from the response as an std::string
|
||||
@ -196,7 +196,7 @@ bool ok = httpClient.performRequest(args, [](const HttpResponsePtr& response)
|
||||
// ok will be false if your httpClient is not async
|
||||
```
|
||||
|
||||
Here is what the HTTP server API looks like. Note that HTTP server support is very, very recent and subject to changes.
|
||||
### HTTP server API
|
||||
|
||||
```
|
||||
ix::HttpServer server(port, hostname);
|
||||
@ -243,7 +243,7 @@ CMakefiles for the library and the examples are available. This library has few
|
||||
```
|
||||
mkdir build # make a build dir so that you can build out of tree.
|
||||
cd build
|
||||
cmake ..
|
||||
cmake -DUSE_TLS=1 ..
|
||||
make -j
|
||||
make install # will install to /usr/local on Unix, on macOS it is a good idea to sudo chown -R `whoami`:staff /usr/local
|
||||
```
|
||||
@ -251,6 +251,12 @@ make install # will install to /usr/local on Unix, on macOS it is a good idea to
|
||||
Headers and a static library will be installed to the target dir.
|
||||
There is a unittest which can be executed by typing `make test`.
|
||||
|
||||
Options for building:
|
||||
|
||||
* `-DUSE_TLS=1` will enable TLS support
|
||||
* `-DUSE_MBED_TLS=1` will use [mbedlts](https://tls.mbed.org/) for the TLS support (default on Windows)
|
||||
* `-DUSE_WS=1` will build the ws interactive command line tool
|
||||
|
||||
### vcpkg
|
||||
|
||||
It is possible to get IXWebSocket through Microsoft [vcpkg](https://github.com/microsoft/vcpkg).
|
||||
|
@ -12,10 +12,10 @@
|
||||
|
||||
#include "IXCancellationRequest.h"
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
struct addrinfo;
|
||||
|
||||
|
@ -6,8 +6,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "IXWebSocketHttpHeaders.h"
|
||||
#include "IXProgressCallback.h"
|
||||
#include "IXWebSocketHttpHeaders.h"
|
||||
#include <tuple>
|
||||
|
||||
namespace ix
|
||||
@ -111,10 +111,12 @@ namespace ix
|
||||
class Http
|
||||
{
|
||||
public:
|
||||
static std::tuple<bool, std::string, HttpRequestPtr> parseRequest(std::shared_ptr<Socket> socket);
|
||||
static std::tuple<bool, std::string, HttpRequestPtr> parseRequest(
|
||||
std::shared_ptr<Socket> socket);
|
||||
static bool sendResponse(HttpResponsePtr response, std::shared_ptr<Socket> socket);
|
||||
|
||||
static std::tuple<std::string, std::string, std::string> parseRequestLine(const std::string& line);
|
||||
static std::tuple<std::string, std::string, std::string> parseRequestLine(
|
||||
const std::string& line);
|
||||
static std::string trim(const std::string& str);
|
||||
};
|
||||
}
|
||||
} // namespace ix
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
#include <assert.h>
|
||||
#include <zlib.h>
|
||||
|
||||
namespace ix
|
||||
@ -52,6 +53,8 @@ namespace ix
|
||||
bool HttpClient::performRequest(HttpRequestArgsPtr args,
|
||||
const OnResponseCallback& onResponseCallback)
|
||||
{
|
||||
assert(_async && "HttpClient needs its async parameter set to true "
|
||||
"in order to call performRequest");
|
||||
if (!_async) return false;
|
||||
|
||||
// Enqueue the task
|
||||
|
@ -6,9 +6,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "IXHttp.h"
|
||||
#include "IXSocket.h"
|
||||
#include "IXWebSocketHttpHeaders.h"
|
||||
#include "IXHttp.h"
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
|
@ -6,9 +6,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "IXHttp.h"
|
||||
#include "IXSocketServer.h"
|
||||
#include "IXWebSocket.h"
|
||||
#include "IXHttp.h"
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
@ -47,4 +47,3 @@ namespace ix
|
||||
void setDefaultConnectionCallback();
|
||||
};
|
||||
} // namespace ix
|
||||
|
||||
|
@ -128,6 +128,10 @@ namespace ix
|
||||
optval != 0)
|
||||
{
|
||||
pollResult = PollResultType::Error;
|
||||
|
||||
// set errno to optval so that external callers can have an
|
||||
// appropriate error description when calling strerror
|
||||
errno = optval;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -6,9 +6,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "IXGetFreePort.h"
|
||||
#include <iostream>
|
||||
#include <ixwebsocket/IXWebSocketServer.h>
|
||||
#include "IXGetFreePort.h"
|
||||
#include <mutex>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <sstream>
|
||||
|
@ -429,12 +429,18 @@ namespace ix
|
||||
}
|
||||
|
||||
void CobraConnection::subscribe(const std::string& channel,
|
||||
SubscriptionCallback cb)
|
||||
const std::string& filter,
|
||||
SubscriptionCallback cb)
|
||||
{
|
||||
// Create and send a subscribe pdu
|
||||
Json::Value body;
|
||||
body["channel"] = channel;
|
||||
|
||||
if (!filter.empty())
|
||||
{
|
||||
body["filter"] = filter;
|
||||
}
|
||||
|
||||
Json::Value pdu;
|
||||
pdu["action"] = "rtm/subscribe";
|
||||
pdu["body"] = body;
|
||||
|
@ -75,7 +75,9 @@ namespace ix
|
||||
|
||||
// Subscribe to a channel, and execute a callback when an incoming
|
||||
// message arrives.
|
||||
void subscribe(const std::string& channel, SubscriptionCallback cb);
|
||||
void subscribe(const std::string& channel,
|
||||
const std::string& filter = std::string(),
|
||||
SubscriptionCallback cb = nullptr);
|
||||
|
||||
/// Unsubscribe from a channel
|
||||
void unsubscribe(const std::string& channel);
|
||||
|
@ -17,7 +17,7 @@ namespace ix
|
||||
const std::string CobraMetricsPublisher::kSetBlacklistId = "sms_set_blacklist_id";
|
||||
|
||||
CobraMetricsPublisher::CobraMetricsPublisher() :
|
||||
_enabled(false)
|
||||
_enabled(true)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include <chrono>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace ix
|
||||
@ -132,8 +133,8 @@ namespace ix
|
||||
CobraMetricsThreadedPublisher _cobra_metrics_theaded_publisher;
|
||||
|
||||
/// A boolean to enable or disable this system
|
||||
/// push becomes a no-op when _enabled is true
|
||||
bool _enabled;
|
||||
/// push becomes a no-op when _enabled is false
|
||||
std::atomic<bool> _enabled;
|
||||
|
||||
/// A uuid used to uniquely identify a session
|
||||
std::string _session;
|
||||
|
17
ws/ws.cpp
17
ws/ws.cpp
@ -9,15 +9,10 @@
|
||||
//
|
||||
#include "ws.h"
|
||||
|
||||
//
|
||||
// Main drive for websocket utilities
|
||||
//
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
// #include <unistd.h>
|
||||
|
||||
#include <cli11/CLI11.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
@ -60,6 +55,7 @@ int main(int argc, char** argv)
|
||||
std::string hostname("127.0.0.1");
|
||||
std::string pidfile;
|
||||
std::string channel;
|
||||
std::string filter;
|
||||
std::string message;
|
||||
std::string password;
|
||||
std::string appkey;
|
||||
@ -76,6 +72,7 @@ int main(int argc, char** argv)
|
||||
bool followRedirects = false;
|
||||
bool verbose = false;
|
||||
bool save = false;
|
||||
bool quiet = false;
|
||||
bool compress = false;
|
||||
bool strict = false;
|
||||
bool stress = false;
|
||||
@ -170,6 +167,8 @@ int main(int argc, char** argv)
|
||||
cobraSubscribeApp->add_option("--rolesecret", rolesecret, "Role secret");
|
||||
cobraSubscribeApp->add_option("channel", channel, "Channel")->required();
|
||||
cobraSubscribeApp->add_option("--pidfile", pidfile, "Pid file");
|
||||
cobraSubscribeApp->add_option("--filter", filter, "Stream SQL Filter");
|
||||
cobraSubscribeApp->add_flag("-q", quiet, "Quiet / only display stats");
|
||||
|
||||
CLI::App* cobraPublish = app.add_subcommand("cobra_publish", "Cobra publisher");
|
||||
cobraPublish->add_option("--appkey", appkey, "Appkey");
|
||||
@ -194,6 +193,7 @@ int main(int argc, char** argv)
|
||||
cobra2statsd->add_option("channel", channel, "Channel")->required();
|
||||
cobra2statsd->add_flag("-v", verbose, "Verbose");
|
||||
cobra2statsd->add_option("--pidfile", pidfile, "Pid file");
|
||||
cobra2statsd->add_option("--filter", filter, "Stream SQL Filter");
|
||||
|
||||
CLI::App* cobra2sentry = app.add_subcommand("cobra_to_sentry", "Cobra to sentry");
|
||||
cobra2sentry->add_option("--appkey", appkey, "Appkey");
|
||||
@ -206,6 +206,7 @@ int main(int argc, char** argv)
|
||||
cobra2sentry->add_flag("-v", verbose, "Verbose");
|
||||
cobra2sentry->add_flag("-s", strict, "Strict mode. Error out when sending to sentry fails");
|
||||
cobra2sentry->add_option("--pidfile", pidfile, "Pid file");
|
||||
cobra2sentry->add_option("--filter", filter, "Stream SQL Filter");
|
||||
|
||||
CLI::App* runApp = app.add_subcommand("snake", "Snake server");
|
||||
runApp->add_option("--port", port, "Connection url");
|
||||
@ -290,7 +291,7 @@ int main(int argc, char** argv)
|
||||
{
|
||||
ret = ix::ws_cobra_subscribe_main(appkey, endpoint,
|
||||
rolename, rolesecret,
|
||||
channel);
|
||||
channel, filter, quiet);
|
||||
}
|
||||
else if (app.got_subcommand("cobra_publish"))
|
||||
{
|
||||
@ -302,14 +303,14 @@ int main(int argc, char** argv)
|
||||
{
|
||||
ret = ix::ws_cobra_to_statsd_main(appkey, endpoint,
|
||||
rolename, rolesecret,
|
||||
channel, hostname, statsdPort,
|
||||
channel, filter, hostname, statsdPort,
|
||||
prefix, fields, verbose);
|
||||
}
|
||||
else if (app.got_subcommand("cobra_to_sentry"))
|
||||
{
|
||||
ret = ix::ws_cobra_to_sentry_main(appkey, endpoint,
|
||||
rolename, rolesecret,
|
||||
channel, dsn,
|
||||
channel, filter, dsn,
|
||||
verbose, strict, jobs);
|
||||
}
|
||||
else if (app.got_subcommand("snake"))
|
||||
|
6
ws/ws.h
6
ws/ws.h
@ -56,7 +56,9 @@ namespace ix
|
||||
const std::string& endpoint,
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
const std::string& channel);
|
||||
const std::string& channel,
|
||||
const std::string& filter,
|
||||
bool quiet);
|
||||
|
||||
int ws_cobra_publish_main(const std::string& appkey,
|
||||
const std::string& endpoint,
|
||||
@ -71,6 +73,7 @@ namespace ix
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
const std::string& channel,
|
||||
const std::string& filter,
|
||||
const std::string& host,
|
||||
int port,
|
||||
const std::string& prefix,
|
||||
@ -82,6 +85,7 @@ namespace ix
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
const std::string& channel,
|
||||
const std::string& filter,
|
||||
const std::string& dsn,
|
||||
bool verbose,
|
||||
bool strict,
|
||||
|
@ -11,13 +11,17 @@
|
||||
#include <atomic>
|
||||
#include <ixcobra/IXCobraConnection.h>
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
namespace ix
|
||||
{
|
||||
int ws_cobra_subscribe_main(const std::string& appkey,
|
||||
const std::string& endpoint,
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
const std::string& channel)
|
||||
const std::string& channel,
|
||||
const std::string& filter,
|
||||
bool quiet)
|
||||
{
|
||||
|
||||
ix::CobraConnection conn;
|
||||
@ -28,8 +32,28 @@ namespace ix
|
||||
|
||||
Json::FastWriter jsonWriter;
|
||||
|
||||
// Display incoming messages
|
||||
std::atomic<int> msgPerSeconds(0);
|
||||
std::atomic<int> msgCount(0);
|
||||
|
||||
auto timer = [&msgPerSeconds, &msgCount]
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
std::cout << "#messages " << msgCount << " "
|
||||
<< "msg/s " << msgPerSeconds
|
||||
<< std::endl;
|
||||
|
||||
msgPerSeconds = 0;
|
||||
auto duration = std::chrono::seconds(1);
|
||||
std::this_thread::sleep_for(duration);
|
||||
}
|
||||
};
|
||||
|
||||
std::thread t(timer);
|
||||
|
||||
conn.setEventCallback(
|
||||
[&conn, &channel, &jsonWriter]
|
||||
[&conn, &channel, &jsonWriter, &filter, &msgCount, &msgPerSeconds, &quiet]
|
||||
(ix::CobraConnectionEventType eventType,
|
||||
const std::string& errMsg,
|
||||
const ix::WebSocketHttpHeaders& headers,
|
||||
@ -37,33 +61,40 @@ namespace ix
|
||||
{
|
||||
if (eventType == ix::CobraConnection_EventType_Open)
|
||||
{
|
||||
std::cout << "Subscriber: connected" << std::endl;
|
||||
spdlog::info("Subscriber connected");
|
||||
|
||||
for (auto it : headers)
|
||||
{
|
||||
std::cerr << it.first << ": " << it.second << std::endl;
|
||||
spdlog::info("{}: {}", it.first, it.second);
|
||||
}
|
||||
}
|
||||
else if (eventType == ix::CobraConnection_EventType_Authenticated)
|
||||
{
|
||||
std::cout << "Subscriber authenticated" << std::endl;
|
||||
conn.subscribe(channel,
|
||||
[&jsonWriter](const Json::Value& msg)
|
||||
spdlog::info("Subscriber authenticated");
|
||||
conn.subscribe(channel, filter,
|
||||
[&jsonWriter, &quiet,
|
||||
&msgPerSeconds, &msgCount](const Json::Value& msg)
|
||||
{
|
||||
std::cout << jsonWriter.write(msg) << std::endl;
|
||||
if (!quiet)
|
||||
{
|
||||
std::cout << jsonWriter.write(msg) << std::endl;
|
||||
}
|
||||
|
||||
msgPerSeconds++;
|
||||
msgCount++;
|
||||
});
|
||||
}
|
||||
else if (eventType == ix::CobraConnection_EventType_Subscribed)
|
||||
{
|
||||
std::cout << "Subscriber: subscribed to channel " << subscriptionId << std::endl;
|
||||
spdlog::info("Subscriber: subscribed to channel {}", subscriptionId);
|
||||
}
|
||||
else if (eventType == ix::CobraConnection_EventType_UnSubscribed)
|
||||
{
|
||||
std::cout << "Subscriber: unsubscribed from channel " << subscriptionId << std::endl;
|
||||
spdlog::info("Subscriber: unsubscribed from channel {}", subscriptionId);
|
||||
}
|
||||
else if (eventType == ix::CobraConnection_EventType_Error)
|
||||
{
|
||||
std::cout << "Subscriber: error" << errMsg << std::endl;
|
||||
spdlog::error("Subscriber: error {}", errMsg);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
@ -25,6 +25,7 @@ namespace ix
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
const std::string& channel,
|
||||
const std::string& filter,
|
||||
const std::string& dsn,
|
||||
bool verbose,
|
||||
bool strict,
|
||||
@ -94,7 +95,7 @@ namespace ix
|
||||
}
|
||||
|
||||
conn.setEventCallback(
|
||||
[&conn, &channel, &jsonWriter,
|
||||
[&conn, &channel, &filter, &jsonWriter,
|
||||
verbose, &receivedCount, &sentCount,
|
||||
&condition, &conditionVariableMutex,
|
||||
&progressCondition, &queue]
|
||||
@ -119,7 +120,7 @@ namespace ix
|
||||
else if (eventType == ix::CobraConnection_EventType_Authenticated)
|
||||
{
|
||||
std::cerr << "Subscriber authenticated" << std::endl;
|
||||
conn.subscribe(channel,
|
||||
conn.subscribe(channel, filter,
|
||||
[&jsonWriter, verbose,
|
||||
&sentCount, &receivedCount,
|
||||
&condition, &conditionVariableMutex,
|
||||
|
@ -63,6 +63,7 @@ namespace ix
|
||||
const std::string& rolename,
|
||||
const std::string& rolesecret,
|
||||
const std::string& channel,
|
||||
const std::string& filter,
|
||||
const std::string& host,
|
||||
int port,
|
||||
const std::string& prefix,
|
||||
@ -90,7 +91,7 @@ namespace ix
|
||||
uint64_t msgCount = 0;
|
||||
|
||||
conn.setEventCallback(
|
||||
[&conn, &channel, &jsonWriter, &statsdClient, verbose, &tokens, &prefix, &msgCount]
|
||||
[&conn, &channel, &filter, &jsonWriter, &statsdClient, verbose, &tokens, &prefix, &msgCount]
|
||||
(ix::CobraConnectionEventType eventType,
|
||||
const std::string& errMsg,
|
||||
const ix::WebSocketHttpHeaders& headers,
|
||||
@ -112,7 +113,7 @@ namespace ix
|
||||
else if (eventType == ix::CobraConnection_EventType_Authenticated)
|
||||
{
|
||||
spdlog::info("Subscriber authenticated");
|
||||
conn.subscribe(channel,
|
||||
conn.subscribe(channel, filter,
|
||||
[&jsonWriter, &statsdClient,
|
||||
verbose, &tokens, &prefix, &msgCount]
|
||||
(const Json::Value& msg)
|
||||
|
Reference in New Issue
Block a user