效果(分别是模拟器和手机截图)
git
基于 GCDAsyncSocket 框架进行,关于 GCDAsyncSocket 的介绍可自行了解github
/*! @method 开启服务 @abstract 开启服务器 TCP 链接服务 */ - (void)startServer { self.serverSocket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; NSError *error = nil; [self.serverSocket acceptOnPort:5555 error:&error]; if (error) { NSLog(@"服务开启失败"); } else { NSLog(@"服务开启成功"); } }
#pragma mark - GCDAsyncSocketDelegate /*! @method 收到socket端链接回调 @abstract 服务器收到socket端链接回调 */ - (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket { [self.clientSocketArray addObject:newSocket]; [newSocket readDataWithTimeout:-1 tag:self.clientSocketArray.count]; }
/*! @method 收到socket端数据的回调 @abstract 服务器收到socket端数据的回调 */ - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { // 直接进行转发数据 for (GCDAsyncSocket *clientSocket in self.clientSocketArray) { if (sock != clientSocket) { [clientSocket writeData:data withTimeout:-1 tag:0]; } } [sock readDataWithTimeout:-1 tag:0]; }
int main(int argc, const char * argv[]) { @autoreleasepool { Server *chatServer = [[Server alloc]init]; [chatServer startServer]; // 开启主运行循环 [[NSRunLoop mainRunLoop] run]; } return 0; }
// 回调 发送图片 __weak typeof(self) weakSelf = self; bgImgView.block = ^(UIImage *img) { weakSelf.drawView.drawImg = img; // image NSData *imgData = UIImageJPEGRepresentation(weakSelf.drawView.drawImg, 0.2); NSMutableData *dat = [NSMutableData data]; [dat appendData:imgData]; // 拼接二进制数据流的结束符 NSData *endData = [@"$" dataUsingEncoding:NSUTF8StringEncoding]; [dat appendData:endData]; // 发送数据 [weakSelf.clientSocket writeData:dat withTimeout:-1 tag:111111]; };
// 拼接数据 转成图片 [self.socketReadData appendData:data]; NSData *endData = [data subdataWithRange:NSMakeRange(data.length -1, 1)]; NSString *end= [[NSString alloc] initWithData:endData encoding:NSUTF8StringEncoding]; if ([end isEqualToString:@"$"]) { UIImage *tmpImg = [UIImage imageWithData:self.socketReadData]; self.drawView.drawImg = tmpImg; [self.drawView setNeedsDisplay]; [self.clientSocket readDataWithTimeout:-1 tag:111111]; // 拼完图片 恢复默认 self.socketReadData = nil; }
/*! @method 发送路径 @abstract 经过socket 发送路径信息 */ - (void)sendPath { // path 坐标点及 转换 NSArray *points = [(UIBezierPath *)self.dataModel.path points]; NSMutableArray *tmp = [NSMutableArray array]; for (id value in points) { CGPoint point = [value CGPointValue]; NSDictionary *dic = @{@"x" : @(point.x), @"y": @(point.y)}; [tmp addObject:dic]; } // 颜色类别 NSInteger colorNum = 0; if (CGColorEqualToColor(self.drawView.color.CGColor, [UIColor redColor].CGColor)) { colorNum = 1; } else if (CGColorEqualToColor(self.drawView.color.CGColor, [UIColor blueColor].CGColor) ){ colorNum = 2; } else if (CGColorEqualToColor(self.drawView.color.CGColor, [UIColor greenColor].CGColor) ) { colorNum = 3; } // 传递数据格式 NSDictionary *pathDataDict = @{ @"path" : tmp, @"width" : @(self.drawView.width), @"color" : @(colorNum), @"screenW": @([UIScreen mainScreen].bounds.size.width), @"screenH": @([UIScreen mainScreen].bounds.size.height) }; NSData *pathData = [NSJSONSerialization dataWithJSONObject:pathDataDict options:NSJSONWritingPrettyPrinted error:nil]; [self.clientSocket writeData:pathData withTimeout:-1 tag:111111]; }
// 一、接受坐标点 NSInteger w = [tmpDict[@"screenW"] integerValue]; NSInteger h = [tmpDict[@"screenH"] integerValue]; CGFloat scaleW = [UIScreen mainScreen].bounds.size.width / w; CGFloat scaleH = [UIScreen mainScreen].bounds.size.height / h; // 处理点 NSArray *pointDict = tmpDict[@"path"]; DIYBezierPath *path = [[DIYBezierPath alloc]init]; for (NSDictionary *tmpDict in pointDict) { CGPoint point = CGPointMake([tmpDict[@"x"] floatValue] * scaleW, [tmpDict[@"y"] floatValue] * scaleH); NSInteger index = [pointDict indexOfObject:tmpDict]; if (index == 0) { [path moveToPoint:point]; } else { [path addLineToPoint:point]; } } switch ([tmpDict[@"color"] integerValue]) { case 0: self.drawView.color = [UIColor blackColor]; break; case 1: self.drawView.color = [UIColor redColor]; break; case 2: self.drawView.color = [UIColor blueColor]; break; case 3: self.drawView.color = [UIColor greenColor]; break; default: break; } self.drawView.width = [tmpDict[@"width"] floatValue]; self.drawView.currentPath = path; self.drawView.currentPath.pathColor = self.drawView.color; self.drawView.currentPath.lineWidth = self.drawView.width; [self.drawView.pathArray addObject:path]; [self.drawView setNeedsDisplay];
https://github.com/HOWIE-CH/-You-guess-I-painted-_socket.gitweb