MFWebSocket 0.1.3

MFWebSocket 0.1.3

XieBangyao 维护。



  • 作者
  • XieBangyao

MFWebSocket

基于 Facebook 的 SocketRocket 库封装的 WebSocket 库。

封装使用 Facebook 的 SocketRocket,实现每个发送消息成功失败回调、连接超时、发送超时。


须知

1. 说明

为实现 Send 超时功能,每个 send 的参数都默认添加了一个格式为 @"identificationID": @"2018-11-30 15:33:06.34337576" 的 key-value,每次 send 都需要服务端原封不动返回这个 key-value

2. 示例

2.1 用户发送

用户命令参数:

@{@"cmd": @"test"}

实际 Send 命令参数:

@{@"cmd": @"test",@"identificationID": @"2018-11-30 15:33:06.34337576"}。

identificationID 字段无需自行添加,但需要服务端返回

2.2 服务端返回

@{@"identificationID":  @"2018-11-30 15:33:06.34337576",  //identificationID标识
@"xxx": @"xxx"//服务端返回的其他数据}。

Requirements 要求

iOS 8+
Xcode 8+


Installation 安装

1.手动安装

下载DEMO后, 将子文件夹MFWebSocket拖入到项目中,导入头文件MFWebSocketManager.h开始使用,注意:项目中需要有'SocketRocket', '~> 0.5.1'第三方库!

2.CocoaPods安装

首先 викликати команду pod 'MFWebSocket',:git => 'https://github.com/MeetFutureOrg/MFWebSocket.git' потім викликати команду pod install або pod install --no-repo-update

如果发现pod search MFWebSocket 不是最新版本,在终端执行pod setup命令更新本地spec镜像缓存(时间可能有点长),重新搜索就OK了


Usage 使用方法

1. Open Socket 开启Socket

1.1 使用NSString初始化

[MFWebSocketShareManager mf_openWithUrlString:@"http://192.168.0.1" connect:^{
        
} failure:^(NSError *error) {
        
}];

1.2 使用NSURL初始化

[MFWebSocketShareManager mf_openWithUrl:[NSURL URLWithString:@"http://192.168.0.1"] connect:^{
        
} failure:^(NSError *error) {
        
}];

1.3 使用NSURLRequest初始化

[MFWebSocketShareManager mf_openWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.1"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5] connect:^{
        
 } failure:^(NSError *error) {
        
 }];

2. Send Message 发送消息

[MFWebSocketShareManager mf_send:@{@"cmd":@"read"} receive:^(NSDictionary *message, MFSocketReceiveType type) {
    
} failure:^(NSError *error) {
        
}];

3. Close Socket 关闭Socket

//主动关闭Socket
[MFWebSocketShareManager mf_closeSocketWithBlock:^(NSInteger code, NSString *reason, BOOL wasClean) {
        
}];

4. Start HeartBeat 开启HeartBeat

4.1 自动开启

默认自动开启

4.2 手动开启

//先设置需要手动开启
MFWebSocketShareManager.manualStartHeartBeat = YES;

//手动开启
[MFWebSocketShareManager mf_manualStartHeartBeatAfterDelay:5.0];

5. 其他设置

这些设置需要在打开套接字之前设置才能生效

5.1 ping信息

pingInfo类型必须是NSSting或NSData类型,内容由服务端定义,默认为@"ping"

@property (nonatomic, strong) id pingInfo;

5.2 延迟多少秒自动启动HeartBeat

需要将manualStartHeartBeat属性设置为NO才能生效。默认为0秒,立即启动

@property (nonatomic, assign) NSTimeInterval autoStartHeartBeatDelay;

5.3 最大重连次数

最大重连次数,默认为5次

@property (nonatomic, assign) NSUInteger maxReconnectTimes;

5.4 open与send超时时间

open与send超时时间,默认为5秒

@property (nonatomic, assign) NSTimeInterval timeoutInterval;

5.5 最大ping次数

最大ping次数,多少次ping不通则认为掉线,默认为3次

@property (nonatomic, assign) NSUInteger maxPingTimes;

5.6 ping时间间隔

ping时间间隔,表示多少秒ping一次,默认为2秒

@property (nonatomic, assign) NSTimeInterval pingInterval;

5.7 错误域

错误域,用于区分SocketRocket错误还是MFWebSocket定义的错误。默认为@"com.xby.XBYSocket"

@property (nonatomic, strong) NSString *errorDomain;

联系方式

Email : [email protected]
Blog : https://adrenine.github.io/

许可证

MFWebSocket 使用 MIT 许可证,详情见 LICENSE 文件。