这是一个基于 WebSockets 通信的 Bayeux 协议的客户端实现,用于 OSX 和 iOS。使用本库,可以轻松连接到 Faye 服务器,或者理论上任何实现了 Bayeux 协议 的服务器。
BayeuxClient 支持 OSX 版本 10.7 及以上,iOS 版本 5.0 及以上。它需要 ARC 支持,以及一些仅在 Apple 的 LLVM 编译器 v4.0+(xcode 4.4+)或开源 LLVM 编译器 v3.1+ 中可用的其他编译器技巧。
BayeuxClient 使用 SocketRocket 来处理 WebSocket 连接。
推荐使用 CocoaPods 进行安装。请将以下内容添加到您的 Podfile 中
pod 'BayeuxClient', '~> 0.1.1'
然后运行 pod install
。这将为您的项目下载并安装 BayeuxClient 和 SocketRocket 依赖项。
将 BayeuxClient.h
头文件导入到您的项目中,并实现 BayeuxClientDelegate
协议
// BayeuxTest.h
#import <Foundation/Foundation.h>
#import "BayeuxClient.h"
@interface BayeuxTest : NSObject <BayeuxClientDelegate>
@property BayeuxClient *client;
@end
// BayeuxTest.m
#import "BayeuxTest.h"
@implementation BayeuxTest
- (void)connectToServer
{
// initialize the client with the URL to the server... may also use http or https protocols.
self.client = [[BayeuxClient alloc] initWithURLString:@"ws://domain.ext/path"];
// subscribe to a channel - you can call subscribeToChannel: before or after you connect
[self.client subscribeToChannel:@"/example/channel"];
// assign myself to be the delegate (see BayeuxClientDelegate methods implemented below)
self.client.delegate = self;
// connect
[self.client connect];
}
#pragma mark - Required BayeuxClientDelegate methods
- (void)bayeuxClient:(BayeuxClient *)client receivedMessage:(NSDictionary *)message fromChannel:(NSString *)channel
{
NSLog(@"A message was received - this is the only method you MUST implement from the BayeuxClientDelegate protocol.");
}
#pragma mark - Optional BayeuxClientDelegate methods
- (void)bayeuxClientDidConnect:(BayeuxClient *)client
{
NSLog(@"The client connected.");
}
- (void)bayeuxClient:(BayeuxClient *)client subscribedToChannel:(NSString *)channel
{
NSLog(@"The client successfully connected to a channel.");
}
- (void)bayeuxClient:(BayeuxClient *)client unsubscribedFromChannel:(NSString *)channel
{
NSLog(@"The client successfully unsubscribed from a channel.");
}
- (void)bayeuxClient:(BayeuxClient *)client publishedMessageId:(NSString *)messageId toChannel:(NSString *)channel error:(NSError *)error
{
NSLog(@"The server has responded to a published message.");
}
- (void)bayeuxClient:(BayeuxClient *)client failedToSubscribeToChannel:(NSString *)channel withError:(NSError *)error
{
NSLog(@"The client encountered an error while subscribing to a channel.");
}
- (void)bayeuxClient:(BayeuxClient *)client failedToSerializeMessage:(id)message withError:(NSError *)error
{
NSLog(@"The client encountered an error while serializing a message.");
}
- (void)bayeuxClient:(BayeuxClient *)client failedToDeserializeMessage:(id)message withError:(NSError *)error
{
NSLog(@"The client encountered an error while deserializing a message.");
}
- (void)bayeuxClient:(BayeuxClient *)client failedWithError:(NSError *)error
{
NSLog(@"The client encountered some error which likely caused it to disconnect.");
}
- (void)bayeuxClientDidDisconnect:(BayeuxClient *)client
{
NSLog(@"The client successfully disconnected gracefully, because you asked it to.");
}
@end
完成之后,您可以使用 [self.client disconnect]
方法优雅地断开连接。该方法将向服务器发送一个断开连接消息,服务器将回应并在代理上触发 bayeuxClientDidDisconnect:
方法。
或者,您可以执行硬断开连接(即,将客户端释放,例如 self.client = nil
这将使 ARC 断开连接)。几分钟后,服务器将知道您已断开连接。显然,宜使用优雅断开连接。
一旦连接到客户端,您可以使用以下方法向不同通道发布消息
messageId = [self.client publishMessage:@"This is a test." toChannel:@"/test/channel"];
根据 Bayeux 协议文档,服务器不需要响应。但如果它确实如此,它将在您的代理上调用 bayeuxClient:publishedMessageId:toChannel:error:
。参数 messageId
将与从 publishMessage:toChannel:
返回的值匹配,参数 error
将是 nil
(表示发布成功),或描述为什么消息无法发布的 NSError
。
还可以插入 Bayeux 扩展。这些扩展可以在消息发送到服务器之前或接收后对其进行修改,通常用于实现某种自定义认证方案。扩展类实现了 BayeuxClientExtension
协议
// BayeuxTestExtension.h
#import <Foundation/Foundation.h>
#import "BayeuxClient.h"
@interface BayeuxTestExtension : NSObject <BayeuxClientExtension>
@end
// BayeuxTestExtension.m
#import "BayeuxTestExtension.h"
@implement BayeuxTestExtension
// implement one or both of these optional methods
- (BOOL)bayeuxClient:(BayeuxClient *)client willSendMessage:(NSMutableDictionary *)message
{
NSLog(@"The client is just about to send a message to the server.");
return YES;
}
- (BOOL)bayeuxClient:(BayeuxClient *)client willReceiveMessage:(NSMutableDictionary *)message
{
NSLog(@"The client just received a message from the server.");
return YES;
}
@end
这两种方法都返回一个 BOOL
。如果你返回 NO
,消息将被丢弃(即,消息不会发送到服务器,或收到的消息不会进行处理)。可以通过以下方式添加扩展:
[self.client addExtension:[[BayeuxTestExtension alloc] init]];
客户端会维护对扩展的强引用。