JXChatLogQRCodeVC.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // JXChatLogQRCodeVC.m
  3. // shiku_im
  4. //
  5. // Created by p on 2019/6/5.
  6. // Copyright © 2019年 Reese. All rights reserved.
  7. //
  8. #import "JXChatLogQRCodeVC.h"
  9. #import <ifaddrs.h>
  10. #import <arpa/inet.h>
  11. #import <SystemConfiguration/CaptiveNetwork.h>
  12. #import "QRImage.h"
  13. #import "GCDAsyncSocket.h"
  14. @interface JXChatLogQRCodeVC ()<GCDAsyncSocketDelegate>
  15. @property(nonatomic,strong)GCDAsyncSocket *tcpScoket;
  16. @property (nonatomic, strong) NSMutableArray *clientSocketMuArr;
  17. @end
  18. @implementation JXChatLogQRCodeVC
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. // Do any additional setup after loading the view.
  22. self.isGotoBack = YES;
  23. self.title = Localized(@"JX_ChatLogMove");
  24. self.heightHeader = JX_SCREEN_TOP;
  25. self.heightFooter = 0;
  26. //self.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  27. [self createHeadAndFoot];
  28. g_xmpp.isChatLogMove = YES;
  29. NSString *port = @"8888";
  30. NSLog(@"ipipip == %@", [self getCurrentLocalIP]);
  31. NSMutableString * qrStr = [NSMutableString stringWithFormat:@"%@?action=sendChatHistory&ip=%@&port=%@&userId=%@",g_config.website,[self getCurrentLocalIP],port,g_myself.userId];
  32. UIImage * qrImage = [QRImage qrImageForString:qrStr imageSize:200 logoImage:nil logoImageSize:0];
  33. UIImageView *qrImageView = [[UIImageView alloc] initWithFrame:CGRectMake((JX_SCREEN_WIDTH-200)/2, 100, 200, 200)];
  34. qrImageView.image = qrImage;
  35. [self.tableBody addSubview:qrImageView];
  36. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(qrImageView.frame) + 20, JX_SCREEN_WIDTH, 50)];
  37. label.numberOfLines = 0;
  38. label.textAlignment = NSTextAlignmentCenter;
  39. label.font = [UIFont systemFontOfSize:16.0];
  40. label.text = [NSString stringWithFormat:@"%@\n%@", Localized(@"JX_LoginAccount"), Localized(@"JX_ScanQRCode")];
  41. [self.tableBody addSubview:label];
  42. self.clientSocketMuArr = [NSMutableArray array];
  43. self.tcpScoket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
  44. [self.tcpScoket acceptOnInterface:[self getCurrentLocalIP] port:[port intValue] error:nil];
  45. }
  46. - (nullable NSString*)getCurrentLocalIP
  47. {
  48. NSString *address = nil;
  49. struct ifaddrs *interfaces = NULL;
  50. struct ifaddrs *temp_addr = NULL;
  51. int success = 0;
  52. // retrieve the current interfaces - returns 0 on success
  53. success = getifaddrs(&interfaces);
  54. if (success == 0) {
  55. // Loop through linked list of interfaces
  56. temp_addr = interfaces;
  57. while(temp_addr != NULL) {
  58. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  59. // Check if interface is en0 which is the wifi connection on the iPhone
  60. if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
  61. // Get NSString from C String
  62. address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
  63. }
  64. }
  65. temp_addr = temp_addr->ifa_next;
  66. }
  67. }
  68. // Free memory
  69. freeifaddrs(interfaces);
  70. return address;
  71. }
  72. #pragma mark - AsyncSocketDelegate
  73. - (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket {
  74. [self.clientSocketMuArr addObject:newSocket];
  75. [newSocket readDataWithTimeout:-1 tag:0];
  76. [_wait start:Localized(@"JX_SendNow")];
  77. for (NSInteger i = 0; i < _selUserIdArray.count; i ++) {
  78. NSString *userId = _selUserIdArray[i];
  79. NSMutableArray *msgArr = [[JXMessageObject sharedInstance] fetchAllMessageListWithUser:userId];
  80. // 先发送标识字符串,表明接下来发送的是哪个用户的聊天记录
  81. NSString *str = [NSString stringWithFormat:@"%@,%@",g_myself.userId, userId];
  82. NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
  83. data = [self getAppendLengthData:data];
  84. [newSocket writeData:data withTimeout:-1 tag:10];
  85. // for (GCDAsyncSocket *soc in self.clientSocketMuArr) {
  86. // [soc writeData:data withTimeout:-1 tag:10];
  87. // }
  88. // 在发送该用户聊天记录
  89. for (NSInteger j = 0; j < msgArr.count; j ++) {
  90. JXMessageObject *msg = msgArr[j];
  91. SBJsonWriter * OderJsonwriter = [SBJsonWriter new];
  92. NSString * jsonString = [OderJsonwriter stringWithObject:[msg toDictionary]];
  93. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  94. jsonData = [self getAppendLengthData:jsonData];
  95. NSLog(@"jsonString === %@,,,,,jsonData = %@", jsonString, jsonData);
  96. [newSocket writeData:jsonData withTimeout:-1 tag:10];
  97. // for (GCDAsyncSocket *soc in self.clientSocketMuArr) {
  98. // [soc writeData:jsonData withTimeout:-1 tag:10];
  99. // }
  100. }
  101. }
  102. [_wait stop];
  103. [JXMyTools showTipView:Localized(@"JXAlert_SendOK")];
  104. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  105. [self.clientSocketMuArr removeAllObjects];
  106. [self.tcpScoket disconnectAfterWriting];
  107. [self actionQuit];
  108. });
  109. }
  110. - (NSData *)getAppendLengthData:(NSData *)data {
  111. NSMutableData *da = [NSMutableData data];
  112. // 消息长度 4个字节
  113. int len = (int)data.length;
  114. HTONL(len);
  115. NSData *da1 = [NSData dataWithBytes:&len length:sizeof(len)];
  116. [da appendData:da1];
  117. [da appendData:data];
  118. return da;
  119. }
  120. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  121. }
  122. - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
  123. NSString *readStr = [[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding];
  124. NSLog(@"读到的数据:%@",readStr);
  125. // [sock writeData:[readStr dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:101];
  126. [sock readDataWithTimeout:-1 tag:0];
  127. }
  128. -(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
  129. NSLog(@"已连接:host = %@, port = %d", host, port);
  130. }
  131. -(void)socket:(GCDAsyncSocket *)sock willDisconnectWithError:(NSError *)err{
  132. }
  133. -(void)socketDidDisconnect:(GCDAsyncSocket *)sock{
  134. NSLog(@"断开连接");
  135. }
  136. -(void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
  137. NSLog(@"sock:%@ %ld",sock,tag);
  138. }
  139. - (void)actionQuit {
  140. g_xmpp.isChatLogMove = NO;
  141. [self.tcpScoket disconnect];
  142. if (g_xmpp.isLogined != login_status_yes) {
  143. [g_server performSelector:@selector(showLogin) withObject:nil afterDelay:0.5];
  144. }else {
  145. [super actionQuit];
  146. }
  147. }
  148. /*
  149. #pragma mark - Navigation
  150. // In a storyboard-based application, you will often want to do a little preparation before navigation
  151. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  152. // Get the new view controller using [segue destinationViewController].
  153. // Pass the selected object to the new view controller.
  154. }
  155. */
  156. @end