GitHubphp
每次更新数据都要向对应的端口发送一次请求,以后返回数据以后关闭链接html
客户端和服务器一直连着,当有数据更新的时候,服务器会直接发给客户端,不须要客户端主动请求。(client 须要监听流的输入)前端
ps:在这过程当中,为了保证服务端和客户端一直是链接状态,客户端会定时不间断的发送心跳数据到服务器,代表还链接着,否则长时间没有数据更新,会断开链接,这样一直有心跳数据的时候,就会保证了链接没有中断,至于心跳数据的内容,就是前端后端共同商量的,和请求的数据是单独的。(一般采用nstimer)node
platform :ios, '8.0' inhibit_all_warnings! #use_frameworks! target 'wlentrust' do pod 'CocoaAsyncSocket' pod 'JSONModel', '1.1.0' pod 'AFNetworking', '3.0.4' pod 'XMLDictionary' end
因为tweak自己就是动态库,所以暂时只支持静态库#use_frameworks!ios
具体的请看MokeyDev 的logos tweak工程使用注意事项git
/Users/devzkn/code/tweak/wlentrust/wlentrust/src/socket/GCDAsyncSocketManager/Manager/GCDAsyncSocketCommunicationManager.m:41:1: Cannot synthesize weak property in file using manual reference counting
修改app LLVMXX -language-Object-C 支持ARC Y-Y-Y-NO
参考github
默认读消息为timeout 能够设置10chrome
- (void)socketWriteData:(NSString *)data { // 开始写数据 NSLog(@"socketWriteData:%@",data); NSData *requestData = [data dataUsingEncoding:NSUTF8StringEncoding]; [self.socket writeData:requestData withTimeout:-1 tag:0]; // [self socketBeginReadData];// 修改成链接创建以后 就立马监听 }
若是想要实时监听服务端的消息推送就能够修改,链接一旦创建 就开始读macos
- (void)socket:(GCDAsyncSocket *)socket didConnectToHost:(NSString *)host port:(UInt16)port { [self.socketManager socketBeginReadData];// 修改成链接创建以后 就立马监听 }
/** 开始读数据 */ #pragma mark - ******** 设置读数据的timeout 链接创建以后就开始监听读取数据 - (void)socketBeginReadData { NSLog(@"socketBeginReadData"); [self.socket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 maxLength:0 tag:0];//考虑使用-1 }
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { // 根据服务端返回的消息类型,解析参数,处理任务 }
一旦接受到数据 就开启下一次的监听输入流
receive data -》socketBeginReadDatajson
可让服务端新增响应类型 进行区分,或者app 端进行判断响应数据是否包含reqId,这个reqId 是只有app 主动发起的请求响应时才会存在
处理服务端的消息推送
GACRESPONSE_TYPE respType = [json[@"respType"] integerValue]; if(respType == RESPONSE_TYPE_NOTIFY){//消息通知 服务端的主动通知 //直接发送通知 // 一、 RESPONSE_TYPE_NOTIFY 处理服务端主动推送的任务 NSMutableDictionary * userInfo = [NSMutableDictionary dictionaryWithObject:json forKey:kRESPONSE_TYPENotificationjsonKey];//respType 传送json [[NSNotificationCenter defaultCenter] postNotificationName:kRESPONSE_TYPERESPONSE_TYPE_NOTIFYNotification object:self userInfo:userInfo]; }else{ //二、执行对应的block SocketDidReadBlock didReadBlock = self.requestsMap[requestID]; // if (errorCode != 0) { // // jsonError = [[NSError alloc]initWithDomain:NSURLErrorDomain code:errorCode userInfo:nil]; // } if (didReadBlock) { didReadBlock(jsonError, json); } } [self.socketManager socketBeginReadData];// 修改成链接创建以后 就立马监听
#pragma mark - ******** 失败从新链接 - (void)socketDidDisconnect:(GCDAsyncSocket *)socket withError:(NSError *)err {
- (void)socketWriteDataWithRequestType:(GACRequestType)type requestBody:(nonnull NSDictionary *)body completion:(nullable SocketDidReadBlock)callback { NSLog(@"socketWriteData:%@",body); if (self.socketManager.connectStatus == -1) { NSLog(@"socket 未连通");
- (void)socket:(GCDAsyncSocket *)socket didConnectToHost:(NSString *)host port:(UInt16)port { self.socketManager.connectStatus = 1;//此处的状态设置,提早到创建链接的地方 //此时将重连的时钟删除 [self.socketManager invalidatereconnectTimer];
工程师必备技能 - 90分钟本身开发一个chrome扩展