FayeCpp (C++) 是为桌面和移动平台(如 Mac、Windows、Linux、iOS、Android)提供的轻量级、跨平台客户端库。
该库使用 "纯 C++"(由标准 C++ 提供的功能)创建,不使用重型的 STL 和 C++11 功能。对于 Mac 和 iOS,还有一个 Objective-C 客户端包装器。
git clone https://github.com/OlehKulykov/FayeCpp.git
cd FayeCpp
git submodule init
git submodule update
或通过一些命令
git clone https://github.com/OlehKulykov/FayeCpp.git
cd FayeCpp
git submodule update --init --recursive
pod 'FayeCpp+OpenSSL', :inhibit_warnings => true
pod 'FayeCpp', :inhibit_warnings => true
对于使用 Libwebsockets 构建的 Android,您需要应用修复方案
cd libwebsockets
git apply < ../libwebsockets_h.patch
使用(安装或更新)最新的 CMake 构建系统,需要版本 2.8 或更高。自版本 0.1.11 起增加了库的静态链接版本(阅读下文部分:静态链接)。
cd FayeCpp
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make
在配置阶段,Libwebsockets 和 Jansson 也将被配置,因此,您可以为此库添加 cmake 标志。例如
cmake -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_PREFIX:PATH=/usr \ -DJANSSON_BUILD_DOCS:BOOL=OFF \ -DLWS_WITHOUT_DEBUG:BOOL=ON ..有关更多选项,请阅读 Libwebsockets、Jansson 和 CMake 的文档。
使用 nmake 构建
cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ..
nmake
如何生成 Microsoft Visual Studio 解决方案(如果需要,请替换为您的版本)
cmake -G"Visual Studio 11" -DCMAKE_BUILD_TYPE=Release ..
示例配置 不 支持 OpenSSL 的 Microsoft Visual Studio
cmake -DCMAKE_INSTALL_PREFIX:PATH=c:\dev\FayeCpp\win-install \ -DLWS_WITH_SSL:BOOL=OFF \ -DLWS_SSL_CLIENT_USE_OS_CA_CERTS:BOOL=OFF \ -DLWS_USE_CYASSL:BOOL=OFF \ -DLWS_WITHOUT_SERVER:BOOL=ON \ -DLWS_WITHOUT_DAEMONIZE:BOOL=ON \ -DCMAKE_BUILD_TYPE=Release \ -G"Visual Studio 11" ..
如果您使用FayeCpp与 Libwebsockets 支持,您应该生成 MinGW Makefile,并在配置过程中忽略 Libwebsockets 共享库。因为,从Windows Vista开始,您可能会遇到链接器错误:"未定义的引用 'inet_ntop'",构建将中断。所以,为了一切链接在一起并解决此问题,还有一个额外的C源文件:builds/windows-mingw/inet_ntop.c
,该文件仅在使用 MinGW 编译器时编译。
已成功测试了最新的稳定版 mingw-w64.sourceforge.net/download.php Mingw-builds 项目 - 使用trunk的本地工具链
MinGW64 version: 3.4
MinGW32 version: 3.11
The C compiler identification is GNU 4.9.2
The CXX compiler identification is GNU 4.9.2
cd <path_to_Android_NDK>
./ndk-build NDK_PROJECT_PATH=<path_to_FayeCpp>/builds/android
将
<path_to_Android_NDK>
和<path_to_FayeCpp>
替换为实际路径。
为了创建iOS框架,在 builds/ios/
文件夹中创建了脚本 build_ios_framework.sh
。因此,当您在根目录时,只需执行以下命令
cd builds/ios/
./build_ios_framework.sh
因此,完成之后,您将在 builds/ios/
文件夹中得到 FayeCpp.framework
框架,其中包含 i386
、x86_64
、armv7
、armv7s
和 arm64
架构。
对于使用 OpenSSL 构建 iOS 框架,导航到 builds/ios-with-openssl/
文件夹,并按上述描述进行操作。
如果您想要使用使用 CMake 生成的库的静态版本,您应该将标志 FAYECPP_STATIC 添加到编译器标志中,并与其他依赖项进行链接。
有关旧版本,请参阅 类Unix平台的构建 部分
从Qt SDK 5.3版本开始使用QtWebSockets模块QtWebSockets和QWebSocket类QWebSocket。因此,FayeCpp将使用它们,忽略Libwebsockets。
与往常一样,你有一个扩展名为*i*.pro的Qt项目文件,所以将*fayecpp.pri*包含到项目中。
include(<path_to_FayeCpp>/fayecpp.pri)
当然,将
<path_to_FayeCpp>
替换为相对于*i*.pro文件位置的实际路径。
为了方便集成,有一个位于builds/ios/
文件夹的Xcode静态iOS库项目 fayecpp.xcodeproj,完整路径是:builds/ios/fayecpp.xcodeproj
。所以集成与任何其他第三方静态库相同。
将文件添加到"<main project>"...
,定位fayecpp.xcodeproj
并添加。或者将fayecpp.xcodeproj
拖动到你的主项目中。构建阶段
,展开与二进制文件链接的库
组。libfayecpp.a
然后点击添加。构建设置
中找到查找路径
组。始终搜索用户路径
更改为**是**。头文件搜索路径
文件夹添加到主要头文件fayecpp.h
所在的目录(仓库根目录)。现在你可以以
#include <fayecpp.h>
的方式包含FayeCpp头文件。当你在Objective-C代码中包含FayeCpp头文件时 - 不要忘记将文件扩展名从*.m更改为*.mm。
对于Mac和iOS,在contrib/objc/
文件夹中有一个Objective-C客户端包装器。只需将FayeCppClient.h
、FayeCppClient.mm
文件添加到你的项目并使用它们。此包装器应与自动引用计数(ARC)一起使用。或者使用CocoaPods。
// Add FayeCpp library to search headers folders.
// Include library header.
#include <fayecpp.h>
using namespace FayeCpp;
class FayeDelegate : public FayeCpp::Delegate
{
public:
// override all FayeCpp::Delegate methods.
virtual void onFayeTransportConnected(FayeCpp::Client * client)
{
// ....
}
virtual void onFayeClientReceivedMessageFromChannel(FayeCpp::Client * client,
const FayeCpp::REVariantMap & message,
const FayeCpp::REString & channel)
{
// Print channel which received message
RELog::log("Received message from channel: \"%s\"", channel.UTF8String());
// Iterate all message (VariantMap) pairs
REVariantMap::Iterator iterator = message.iterator();
while (iterator.next())
{
iterator.key(); // get key
iterator.value(); // get value for key
}
// .....
}
// overide other methods
FayeDelegate()
{
// .....
}
virtual ~FayeDelegate()
{
// ......
}
};
// define somewhere variable or field for client
FayeCpp::Client * _client = NULL;
// create & initialize client
_client = new FayeCpp::Client();
_client->setUrl("ws://your_faye_host:80/faye");
_client->setDelegate(_delegatePointerHere);
_client->connect();
// subscribing or adding channels to pending subscriptions
// From "The Bayeux Protocol Specification v1.0" section "Channels"
// http://docs.cometd.org/reference/bayeux_protocol_elements.html.
// The channel name consists of an initial "/" followed by an optional sequence of path segments separated by a single slash "/" character. Within a path segment, the character "/" is reserved.
_client->subscribeToChannel("/faye_channel_1");
_client->subscribeToChannel("/something/faye_channel_2");
// Variant object can hold basic types + list and maps
REVariant value; // create null value, value.type() is REVariant::TypeNone;
value = REVariant(); // set null value, value.type() is REVariant::TypeNone;
value = "C string value"; // set C (const char *) string value, value.type() is REVariant::TypeString;
value = L"Строка"; // set wide (const wchar_t *) string value, value.type() is REVariant::TypeString;
value = 5; // set integer value, value.type() is REVariant::TypeInteger;
value = 3.14f; // set float value, value.type() is REVariant::TypeReal;
value = 3.14159265359; // set double value, value.type() is REVariant::TypeReal;
value = true; // set boolean value with true, value.type() is REVariant::TypeBool;
value = false; // set boolean value with false, value.type() is REVariant::TypeBool;
// Map object (hash or dictionary), stores values by string keys
REVariantMap message; // or FayeCpp::REVariantMap message; if namespace not used.
message["text"] = "Hello world"; // set C (const char *) string value
message[L"wide characters key"] = L"Hello world !!!"; // set wide (const wchar_t *) string value with wide string key.
message[L"Сообщение"] = L"Привет мир !!!"; // set wide (const wchar_t *) string value with wide string key.
message["integer key"] = 1; // set integer value
message["float key"] = 3.14f; // set float value
message["double key"] = 3.14159265359; // set double value
message["is_use_something_1"] = true; // set boolean value with true
message["is_use_something_2"] = false; // set boolean value with false
message["null key"] = REVariant(); // set null value
value = message; // set map value, value.type() is REVariant::TypeMap;
// List object
REVariantList parameters; // or FayeCpp::REVariantList if namespace not used.
parameters += "Text value"; // add C (const char *) string value, or use 'parameters.add()' method.
parameters += L"Текстовое значение"; // add wide (const wchar_t *) string value with wide string key, or use 'parameters.add()' method.
parameters += 2; // add integer value, or use 'parameters.add()' method.
parameters += 3.14f; // add float value, or use 'parameters.add()' method.
parameters += 3.14159265359; // add double value, or use 'parameters.add()' method.
parameters += true; // add boolean value with true, or use 'parameters.add()' method.
parameters += false; // add boolean value with false, or use 'parameters.add()' method.
parameters += REVariant(); // add null value, or use 'parameters.add()' method.
value = parameters; // set list value, value.type() is REVariant::TypeList;
message["parameters"] = parameters; // set list value for key
// Create dummy message
REVariantMap message;
message["text"] = "Hello world";
message[L"wide charectes key"] = L"Hello world !!!";
message["integer key"] = 1;
message["float key"] = 3.14f;
message["double key"] = 3.14159265359;
// Send message object to server with channel name
_client->sendMessageToChannel(message, "/faye_channel_1");
// include Objective-C client wrapper
#import "FayeCppClient.h"
// define strong property for the client
@property (nonatomic, strong) FayeCppClient * client;
// create client and strongly hold with property
self.client = [[FayeCppClient alloc] init];
#pragma mark - FayeCpp client delegate
- (void) onFayeClientConnected:(FayeCppClient *) client
{
NSLog(@"On faye client connected");
}
- (void) onFayeClient:(FayeCppClient *) client
receivedMessage:(NSDictionary *) message
fromChannel:(NSString *) channel
{
NSLog(@"On faye client received message: \n%@ \nfrom channel: %@", message, channel);
}
// implement other delegate messages if needed ...
// set client delegate, all delegate methods are optional and called from main thread
[_client setDelegate:self];
// set faye url string
[_client setUrlString:@"ws://your_faye_host:80/faye"];
// connect and add channels to channels to pending subscriptions
[_client connect];
[_client subscribeToChannel:@"/faye_channel_1"];
[_client subscribeToChannel:@"/something/faye_channel_2"];
[_client sendMessage:@{@"text" : @"Hello world", @"other_params" : @[]}
toChannel:@"/faye_channel_1"];
MIT许可证(MIT)
版权所有(c)2014 - 2016 Kulykov Oleh [email protected]
特此免费授权,任何人获取本软件及其相关文档文件(以下简称“软件”)的副本,可在未经限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、许可和/或销售软件的副本,并允许将软件提供给其他人进行上述操作,但须遵守以下条件:
所有副本或软件的实质性部分均应包含上述版权声明和本许可声明。
软件按“原样”提供,不包括任何形式的保证,无论是明示的、隐含的,还是关于适销性、特定目的适用性或不侵犯他人权利的保证。在任何情况下,无论作者是否被提起诉讼或版权所有者因任何原因造成的索赔、损害或其他责任,包括但不限于合同、侵权或其他责任,均不承担任何责任,不论其起因于、源于或与软件或软件的使用或任何其他方式有关。