适用于 iOS 的 Faye 客户端。该项目是 pcrawfor 在以下位置重写的库的版本:https://github.com/pcrawfor/FayeObjC 改进了捕获错误、增加了订阅块、可以为频道设置扩展。增加了自动连接重连,等等...
self.client = [[MZFayeClient alloc] initWithURL:[NSURL URLWithString:@"ws://localhost:9292/faye"]];
[self.client subscribeToChannel:@"/server" success:^{
NSLog(@"Subscribed successfully to 'server' channel!");
} failure:^(NSError *) {
NSLog(@"Error subscribing to 'server' channel: %@", error.userInfo);
} receivedMessage:^(NSDictionary *message) {
NSLog(@"Message on 'server' channel: %@", message);
}];
[self.client subscribeToChannel:@"/browser" success:nil failure:nil receivedMessage:^(NSDictionary *message) {
NSLog(@"Message on 'browser' channel: %@", message);
}];
[self.client connect:^{
[self.client sendMessage:@{@"text": @"hello!"} toChannel:@"/server" success:^{
NSLog(@"Message sent successfully.");
} failure:^(NSError *)error {
NSLog(@"Error sending message: %@", error.userInfo);
}];
} failure:^(NSError *) {
NSLog(@"Error connecting: %@", error.userInfo);
}];
- (instancetype)initWithURL:(NSURL *)url;
+ (instancetype)clientWithURL:(NSURL *)url;
- (void)sendMessage:(NSDictionary *)message toChannel:(NSString *)channel success:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler;
- (void)sendMessage:(NSDictionary *)message toChannel:(NSString *)channel usingExtension:(NSDictionary *)extension success:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler;
- (void)subscribeToChannel:(NSString *)channel success:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler receivedMessage:(MZFayeClientSubscriptionHandler)subscriptionHandler;
- (void)unsubscribeFromChannel:(NSString *)channel success:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler;
- (void)connect:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler;
- (void)disconnect:(MZFayeClientSuccessHandler)successHandler failure:(MZFayeClientFailureHandler)failureHandler;
有关扩展的更多信息,请参阅 Faye 网站,
- (void)setExtension:(NSDictionary *)extension forChannel:(NSString *)channel;
- (void)removeExtensionForChannel:(NSString *)channel;
许多委托方法可以用块完成,但仍然存在,以供那些喜欢这种方式的人使用。
注意:在 -connect:failure:
中,successHandler
和 failureHandlers
只会在连接成功或失败第一次时被调用。由于自动重连逻辑,此类事件可能会发生多次。如果您需要每次连接成功或失败时都处理连接成功或失败,您应使用委托方法(或作为补充)。
@protocol MZFayeClientDelegate <NSObject>
@optional
- (void)fayeClient:(MZFayeClient *)client didConnectToURL:(NSURL *)url;
- (void)fayeClient:(MZFayeClient *)client didDisconnectWithError:(NSError *)error;
- (void)fayeClient:(MZFayeClient *)client didUnsubscribeFromChannel:(NSString *)channel;
- (void)fayeClient:(MZFayeClient *)client didSubscribeToChannel:(NSString *)channel;
- (void)fayeClient:(MZFayeClient *)client didFailWithError:(NSError *)error;
- (void)fayeClient:(MZFayeClient *)client didFailDeserializeMessage:(NSDictionary *)message
withError:(NSError *)error;
- (void)fayeClient:(MZFayeClient *)client didReceiveMessage:(NSDictionary *)messageData fromChannel:(NSString *)channel;
@end
如果您想运行 Faye 服务器以测试客户端,您可以在这里找到所需的所有信息:http://faye.jcoglan.com/ 您也可以在 Server
目录下运行 faye.rb
文件
gem install faye thin eventmachine
rackup faye.ru -s thin -E production
curl http://localhost:9292/faye -d 'message={"channel":"/server", "data":"hello"}'
由 Square 维护的符合 WebSocket (RFC 6455) 的客户端库,https://github.com/square/SocketRocket
Objective-C 的 Base64 扩展,为 NSData 和 NSString 提供支持 https://github.com/ekscrypto/Base64
MZFayeClient 需要 iOS 6.x 或更高版本。
MZFayeClient使用ARC。