@@ -138,7 +138,7 @@ - (void)sendMessage:(ChatModel *)chatModel timeOut:(NSUInteger)timeOut tag:(long
138138- (void )executeDisconnectServer
139139{
140140 // 更新sokect连接状态
141- _connectStatus = SocketConnectStatus_DisconnectByUser ;
141+ _connectStatus = SocketConnectStatus_UnConnected ;
142142 [self disconnect ];
143143}
144144
@@ -288,8 +288,8 @@ - (void)socketDidSecure:(GCDAsyncSocket *)sock
288288#pragma mark - TCP已经断开连接
289289- (void )socketDidDisconnect : (GCDAsyncSocket *)sock withError : (NSError *)err
290290{
291- // 如果是主动断开连接
292- if (_connectStatus == SocketConnectStatus_DisconnectByUser) return ;
291+ // //如果是主动断开连接
292+ // if (_connectStatus == SocketConnectStatus_DisconnectByUser) return;
293293 // 置为未连接状态
294294 _connectStatus = SocketConnectStatus_UnConnected;
295295 // 自动重连
@@ -320,7 +320,7 @@ - (NSTimeInterval)socket:(GCDAsyncSocket *)sock shouldTimeoutWriteWithTag:(long)
320320#pragma mark - 网络监听
321321- (void )networkChanged : (NSNotification *)notification {
322322
323- if (_connectStatus == SocketConnectStatus_DisconnectByUser) return ; // 主动断开连接
323+ // if (_connectStatus == SocketConnectStatus_DisconnectByUser) return; //主动断开连接
324324
325325 if (networkStatus == RealStatusNotReachable||_connectStatus == SocketConnectStatus_UnConnected) {
326326 [self serverInterruption ];// 断开连接,默认还会重连3次 ,还未连接自动断开
@@ -333,4 +333,125 @@ - (void)networkChanged:(NSNotification *)notification {
333333
334334
335335
336+ #pragma mark - 消息发送
337+ // 发送文本消息
338+ - (void )sendTextMessage : (ChatModel *)textModel
339+ {
340+ dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(1 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
341+
342+ // 模拟回执
343+ ChatModel *receipet = [[ChatModel alloc ]init];
344+ receipet.messageType = Message_NormalReceipt;
345+ receipet.sendTime = textModel.sendTime ;
346+ ChatMessageType type = ChatMessageType_NormalReceipt;
347+ for (id <ChatHandlerDelegate>delegate in self.delegates ) {
348+ if ([delegate respondsToSelector: @selector (didReceiveMessage:type: )]) {
349+ [delegate didReceiveMessage: receipet type: type];
350+ }
351+ }
352+ });
353+
354+ dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(2 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
355+
356+ // 模拟回复
357+ ChatModel *reply = [[ChatModel alloc ]init];
358+ ChatContentModel *content = [[ChatContentModel alloc ]init];
359+ reply.content = content;
360+ reply.content .text = @" 收到文本内容" ;
361+ reply.messageType = Message_Normal;
362+ reply.contenType = Content_Text;
363+ reply.toUserID = textModel.fromUserID ;
364+ reply.chatType = @" userChat" ;
365+ ChatMessageType type = ChatMessageType_Normal;
366+ for (id <ChatHandlerDelegate>delegate in self.delegates ) {
367+ if ([delegate respondsToSelector: @selector (didReceiveMessage:type: )]) {
368+ [delegate didReceiveMessage: reply type: type];
369+ }
370+ }
371+ });
372+ }
373+
374+ // 发送语音消息
375+ - (void )sendAudioMessage : (ChatModel *)audioModel
376+ {
377+ dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(1 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
378+
379+ // 模拟回执
380+ ChatModel *receipet = [[ChatModel alloc ]init];
381+ receipet.messageType = Message_NormalReceipt;
382+ receipet.sendTime = audioModel.sendTime ;
383+ ChatMessageType type = ChatMessageType_NormalReceipt;
384+ for (id <ChatHandlerDelegate>delegate in self.delegates ) {
385+ if ([delegate respondsToSelector: @selector (didReceiveMessage:type: )]) {
386+ [delegate didReceiveMessage: receipet type: type];
387+ }
388+ }
389+ });
390+
391+ dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(2 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
392+
393+ // 模拟回复
394+ ChatModel *reply = [[ChatModel alloc ]init];
395+ ChatContentModel *content = [[ChatContentModel alloc ]init];
396+ reply.content = content;
397+ reply.content .text = @" 收到语音" ;
398+ reply.messageType = Message_Normal;
399+ reply.contenType = Content_Text;
400+ reply.toUserID = audioModel.fromUserID ;
401+ reply.chatType = @" userChat" ;
402+ ChatMessageType type = ChatMessageType_Normal;
403+ for (id <ChatHandlerDelegate>delegate in self.delegates ) {
404+ if ([delegate respondsToSelector: @selector (didReceiveMessage:type: )]) {
405+ [delegate didReceiveMessage: reply type: type];
406+ }
407+ }
408+ });
409+ }
410+
411+ // 发送图片消息
412+ - (void )sendPicMessage : (NSArray <ChatModel *>*)picModels
413+ {
414+
415+ [picModels enumerateObjectsUsingBlock: ^(ChatModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
416+ dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(1 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
417+
418+ // 模拟回执
419+ ChatModel *receipet = [[ChatModel alloc ]init];
420+ receipet.messageType = Message_NormalReceipt;
421+ receipet.sendTime = obj.sendTime ;
422+ ChatMessageType type = ChatMessageType_NormalReceipt;
423+ for (id <ChatHandlerDelegate>delegate in self.delegates ) {
424+ if ([delegate respondsToSelector: @selector (didReceiveMessage:type: )]) {
425+ [delegate didReceiveMessage: receipet type: type];
426+ }
427+ }
428+ });
429+
430+ dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(2 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
431+
432+ // 模拟回复
433+ ChatModel *reply = [[ChatModel alloc ]init];
434+ ChatContentModel *content = [[ChatContentModel alloc ]init];
435+ reply.content = content;
436+ reply.content .text = @" 收到图片" ;
437+ reply.messageType = Message_Normal;
438+ reply.contenType = Content_Text;
439+ reply.toUserID = obj.fromUserID ;
440+ reply.chatType = @" userChat" ;
441+ ChatMessageType type = ChatMessageType_Normal;
442+ for (id <ChatHandlerDelegate>delegate in self.delegates ) {
443+ if ([delegate respondsToSelector: @selector (didReceiveMessage:type: )]) {
444+ [delegate didReceiveMessage: reply type: type];
445+ }
446+ }
447+ });
448+ }];
449+ }
450+
451+ // 发送视频消息
452+ - (void )sendVideoMessage : (ChatModel *)videoModel
453+ {
454+ }
455+
456+
336457@end
0 commit comments