DDTCPClient is a high level socket util based on CocoaAsyncSocket.
- Thread safety, socket operations is on serial queue.
- Subcontracting and Sticky bag.
- Maintain read package one.
- Network state monitoring and reconnection mechanism.
- Heartbeat mechanism.
- Add
pod 'DDTCPClient'
to your Podfile. - Run
pod install
orpod update
. - Import <DDTCPClient/DDTCPClient.h>.
- Download all the files in the Source subdirectory.
- Add the source files to your Xcode project.
- Import
DDTCPClient.h
.
DDTCPClient *socket = [[DDTCPClient alloc] init];
socket.delegate = self;
// debug log open
socket.isDebug = YES;
// default is 10, set -1 when reconnect forever
socket.reconnectCount = -1;
// reconnect timeInterval
socket.reconnectTimeInterval = 5;
// Connect
[socket connectHost:host port:port];
// Disconnect
[socket disConnect];
// Send data
NSString *registStr = @"I'm register informations";
NSData *registData = [registStr dataUsingEncoding:NSUTF8StringEncoding];
[socket sendData:registData];
// Sent heart, just set once, heart data will be send one by one
NSString *heartStr = @"I'm heart informations";
NSData *heartData = [heartStr dataUsingEncoding:NSUTF8StringEncoding];
[socket setHeartData:heartData];
- (void)client:(DDTCPClient *)client didReadData:(NSData *)data {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"DDAsyncSocket -- %@", str);
}
- (void)client:(DDTCPClient *)client didConnect:(NSString *)host port:(uint16_t)port {
}
- (void)clientDidDisconnect:(DDTCPClient *)client {
}
This library requires iOS 8.0+
.
DDTCPClient is provided under the MIT license. See LICENSE file for details.