测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2016年6月 |
由 Anatoliy Popov 维护。
SocketCluster 的原生 iOS 客户端 http://socketcluster.io/
注意:客户端支持 SocketCluster v.3。还测试了 SocketCluster v.4
使用 CocoaPods
pod "SocketCluster-ios-client";
连接
[[SCSocket client] initWithHost:@«host» onPort:portNo securely:YES];
//set delegate
[SCSocket client].delegate=self;
//use this, if you want restore subscriptions on reconnect
[[SCSocket client] setRestoreChannels:YES];
[[SCSocket client] connect];
频道
使用 SCChannel
类
SCChannel* channel = [[SCChannel alloc] initWithChannelName:@"test" andDelegate:nil];
[channel subscribeWithSuccess:^(id response) {
} withFail:^(NSError *error, id response) {
}];
SCChannel
方法
//create channel with name and delegate
-(nonnull instancetype) initWithChannelName:(nonnull NSString*)channelName andDelegate:(nullable id /*<SCChannelDelegate>*/)delegate;
//return channel name
-(nonnull NSString*)getName;
// subscribe to channel with success and fail block
-(void)subscribeWithSuccess:(nullable void (^)(id response))success withFail:(nullable void (^)(NSError* error,id response))fail;
// unsubscribe from channel with success block
-(void)unsubscribeWithSuccess:(nullable void (^)(void))success;
SCChannelDelegate
方法
//fires when some publish data to channel
-(void)SCChannel:(nonnull id/*<SCChannel>*/) channel receiveData:(nullable id)data;
//fires when server send kickOut event
-(void)SCChannel:(nonnull id/*<SCChannel>*/) channel kickOutWithMessage:(nullable id)message;
消息
使用 SCMessage
类
[[[SCMessage alloc] initWithEventName:@«eventName» andData:@{@"data":@"test" }] send]
SCMessage
方法
// init message with event name and data
-(nonnull instancetype) initWithEventName:(nonnull NSString*)eventName andData:(nullable id)data
//send message without success and fail block
//return message cid
-(NSInteger)send;
//send message with success and fail block
//return message cid
-(NSInteger)sendWithSuccess:(nullable void (^)(SCMessage* message,id response))success withFail:(nullable void (^)(SCMessage* message,id response))fail;
//send message without success and fail block to channel
//return message cid
-(NSInteger)sendToChannel:(SCChannel*)channel;
//send message with success and fail block to channel
//return message cid
-(NSInteger)sendToChannel:(SCChannel*)channel withSuccess:(nullable void (^)(SCMessage* message,id response))success withFail:(nullable void (^)(SCMessage* message,id response))fail;
SCSocket
类的附加方法
// emit ‘login’ event with data and success/fail blocks
-(void)loginWithData:(nullable NSDictionary*)data withSuccess:(nullable void (^)(id response))success withFail:(nullable void (^)(id response))fail;
//subscribe to channel without success/fail blocks
-(void)subscribeToChannel:(SCChannel*)channel
//send SCMessage to SCChannel
-(NSInteger)sendMessage:(SCMessage*)message toChannel:(nullable SCChannel*)channel;
//emit event
//this method don’t add message to queue, so if connection lost it will not resend
-(NSInteger) emitEvent:(NSString*)event withData:(id)data
SocketClusterDelegate
方法
//call after client connect to server and #handshake event
-(void)socketClusterConnectEvent;
//call after #setAuthToken event
-(void)socketClusterAuthenticateEvent:(NSString*)token
//call every time, when client receive event from server
-(void)socketClusterReceivedEvent:(NSString *)eventName WithData:(NSDictionary *)data isStandartEvent:(BOOL)isStandartEvent;