FayeCpp 1.2.0

FayeCpp 1.2.0

测试已测试
语言语言 CC
许可 MIT
发布最后发布2016年9月

Oleh Kulykov 维护。



FayeCpp 1.2.0

  • Oleh Kulykov

FayeCpp - Faye C++ 轻量级、跨平台客户端。

Build status Coverity Scan Build Status

FayeCpp (C++) 是为桌面和移动平台(如 MacWindowsLinuxiOSAndroid)提供的轻量级、跨平台客户端库。

该库使用 "纯 C++"(由标准 C++ 提供的功能)创建,不使用重型的 STLC++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

依赖项

  • Libwebsockets - “轻量级纯 C 库,用于最小化 CPU 和内存资源的使用”,或者使用 FayeCppQt SDK(见下文)一起使用。
  • Jansson - “用于编码、解码和处理 JSON 数据的 C 库”。

支持 OpenSSL 的 Podfile

pod 'FayeCpp+OpenSSL', :inhibit_warnings => true

Podfile

pod 'FayeCpp', :inhibit_warnings => true

修复

对于使用 Libwebsockets 构建的 Android,您需要应用修复方案

cd libwebsockets
git apply < ../libwebsockets_h.patch

构建


使用(安装或更新)最新的 CMake 构建系统,需要版本 2.8 或更高。自版本 0.1.11 起增加了库的静态链接版本(阅读下文部分:静态链接)。

在类 Unix 平台上构建

cd FayeCpp
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make

在配置阶段,LibwebsocketsJansson 也将被配置,因此,您可以为此库添加 cmake 标志。例如

cmake -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX:PATH=/usr \
-DJANSSON_BUILD_DOCS:BOOL=OFF \
-DLWS_WITHOUT_DEBUG:BOOL=ON ..

有关更多选项,请阅读 LibwebsocketsJanssonCMake 的文档。

在Windows上使用Microsoft Visual Studio进行构建

  • 执行 开始 -> Microsoft Visual Studio .... -> Visual Studio 工具 -> ... 工具命令提示符,使用管理员权限(右键菜单:以管理员身份运行)。
  • 与在 类Unix平台的构建 中相同,有一些小的变化,告诉 CMake 生成makefile 并使用 nmake 或生成 Microsoft Visual Studio 解决方案和项目并进行构建。

使用 nmake 构建

cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ..
nmake

如何生成 Microsoft Visual Studio 解决方案(如果需要,请替换为您的版本)

cmake -G"Visual Studio 11" -DCMAKE_BUILD_TYPE=Release ..

示例配置 支持 OpenSSLMicrosoft 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" ..
  • 或者另一种选择:特别为这种情况下添加了通过 AppVeyor 服务对 Microsoft Windows 的持续集成,因此您可以查看位于存储库根目录的 appveyor.yml 文件,并找出如何进行配置和构建的步骤(最少行动)。

在Windows上使用MinGW进行构建

如果您使用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

使用Android NDK构建Android应用

  • 应用上述描述的补丁。
  • 下载并安装 Android NDK
  • 导航到已安装的 Android NDK 文件夹。
  • 使用项目路径参数执行 ndk-build 脚本
cd <path_to_Android_NDK>
./ndk-build NDK_PROJECT_PATH=<path_to_FayeCpp>/builds/android

<path_to_Android_NDK><path_to_FayeCpp> 替换为实际路径。

构建iOS框架

为了创建iOS框架,在 builds/ios/ 文件夹中创建了脚本 build_ios_framework.sh。因此,当您在根目录时,只需执行以下命令

cd builds/ios/
./build_ios_framework.sh

因此,完成之后,您将在 builds/ios/ 文件夹中得到 FayeCpp.framework 框架,其中包含 i386x86_64armv7armv7sarm64 架构。

通过OpenSSL构建iOS框架

对于使用 OpenSSL 构建 iOS 框架,导航到 builds/ios-with-openssl/ 文件夹,并按上述描述进行操作。

静态链接

如果您想要使用使用 CMake 生成的库的静态版本,您应该将标志 FAYECPP_STATIC 添加到编译器标志中,并与其他依赖项进行链接。

集成


与Qt SDK(5.3及以上版本)集成

有关旧版本,请参阅 类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文件位置的实际路径。

与Xcode iOS项目集成

为了方便集成,有一个位于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。

与Objective-C代码一起使用

对于MaciOS,在contrib/objc/文件夹中有一个Objective-C客户端包装器。只需将FayeCppClient.hFayeCppClient.mm文件添加到你的项目并使用它们。此包装器应与自动引用计数(ARC)一起使用。或者使用CocoaPods。

在C++中使用库


包含库

// 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() 
    {
        // ......
    }
};

Faye客户端

// 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");

在Objective-C中使用库


包含库并创建客户端实例

// 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]

特此免费授权,任何人获取本软件及其相关文档文件(以下简称“软件”)的副本,可在未经限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、许可和/或销售软件的副本,并允许将软件提供给其他人进行上述操作,但须遵守以下条件:

所有副本或软件的实质性部分均应包含上述版权声明和本许可声明。

软件按“原样”提供,不包括任何形式的保证,无论是明示的、隐含的,还是关于适销性、特定目的适用性或不侵犯他人权利的保证。在任何情况下,无论作者是否被提起诉讼或版权所有者因任何原因造成的索赔、损害或其他责任,包括但不限于合同、侵权或其他责任,均不承担任何责任,不论其起因于、源于或与软件或软件的使用或任何其他方式有关。