项目地址:
RTCVPSocketIO
文章目录
Socket.IO client for iOS. Supports socket.io 2.0+
It’s based on a official Swift library from here:
SocketIO-Client-Swift
It uses Jetfire
Jetfire
引入项目
源码直接引入
使用简介
如果使用https 等连接注意检查工程的info.plist配置
Objective-C Example
#import "RTCVPSocketIO.h"
#import "RTCVPSocketAckEmitter.h"
NSURL* url = [[NSURL alloc] initWithString:@"https://localhost:8080"];
// 设置日志显示类
RTCVPSocketLogger *logger = [[RTCVPSocketLogger alloc]init];
// 自定义参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:@"参数" forKey: @"id"];
[params setObject:@"123456" forKey: @"roomId"];
RTCVPSocketIOClient* socket = [[RTCVPSocketIOClient alloc] init:url withConfig:@{@"log": @NO,
@"reconnects":@YES,
@"reconnectAttempts":@(20),
@"forcePolling": @NO,
@"secure": @YES,
@"forceNew":@YES,
@"forceWebsockets":@YES,
@"selfSigned":@YES,
@"reconnectWait":@2,
@"nsp":@"/",
@"connectParams":params,
@"logger":logger
}];;
[socket on:kSocketEventConnect callback:^(NSArray *array, RTCVPSocketAckEmitter *emitter) {
NSLog(@"====================kSocketEventConnect==========================");
}];
[socket on:kSocketEventDisconnect callback:^(NSArray *array, RTCVPSocketAckEmitter *emitter) {
NSLog(@"====================kSocketEventDisconnect==========================");
}];
[socket on:kSocketEventError callback:^(NSArray *array, RTCVPSocketAckEmitter *emitter) {
NSLog(@"====================kSocketEventError==========================");
}];
[socket on:kSocketEventReconnect callback:^(NSArray *array, RTCVPSocketAckEmitter *emitter) {
NSLog(@"====================kSocketEventReconnect==========================");
}];
[socket on:kSocketEventReconnectAttempt callback:^(NSArray *array, RTCVPSocketAckEmitter *emitter) {
NSLog(@"====================kSocketEventReconnectAttempt==========================");
}];
[socket on:kSocketEventStatusChange callback:^(NSArray *array, RTCVPSocketAckEmitter *emitter) {
NSLog(@"====================kSocketEventStatusChange==========================");
}];
[socket connect];
参数返回的array可能有多个,其中可以是字符串,也可能是字典;具体数据按照和服务协定;
上述在连接https的时候需要设置两个参数:secure和selfSigned的值要设为YES;http这两个参数需要设置为NO
连接服务
除了
[socket connect];
方法,还提供设置连接超时方法,如下:
[_socket connectWithTimeoutAfter:10 withHandler:^{
}];
输出库日志
RTCVPSocketLogger *logger = [[RTCVPSocketLogger alloc]init];
[logger onLogMsgWithCB:^(NSString *message, NSString *type) {
}];
Features
- Supports socket.io 2.0+
- Supports binary
- Supports Polling and WebSockets
- Supports TLS/SSL
Installation
Carthage
Add these line to your
Cartfile
:
github "vascome/vpsocketio" ~> 1.0.5 # Or latest version
Run
carthage update --platform ios,macosx
.
自定义消息监听:
[socket on:@"notification" callback:^(NSArray *array, RTCVPSocketAckEmitter *emitter) {
NSLog(@"====================notification==========================");
}];
request请求后响应服务:
[socket on:@"request" callback:^(NSArray *array, RTCVPSocketAckEmitter *emitter) {
emitResp resp = ^(NSInteger code,NSString *ID){
[emitter emitWith:@[[NSNull null],@{@"code":@(code),@"msg":ID}]];//格式根据需求处理
NSLog(@"====================request resp==========================");
};
self.notifyInfo(@"request",array,resp);
}];
发送消息
1.发送需要回掉的消息
RTCVPSocketOnAckCallback *callback = [blockSelf.socket emitWithAck:method items:@[message]];
[callback timingOutAfter:10 callback:^(NSArray *array) {
if ([array[0] isKindOfClass:[NSNull class]]) {
NSLog(@"");
}
}];
版权声明:本文为lym594887256原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。