标签:ble 标识 warning bsp led fine 是什么 个数 XML
目前及时通讯可以使用环信、柔云、腾讯云,这些都是基于TCP连接的,UI也是高度定制的,而且它们的技术也是比较成熟的。
XMPP比较早,是开源的,但是坑也比较多。传输的数据是XML,造成了很多流量的雍余。
数据格式
Socket通讯报文是没有结束标识的,通讯报文保留前8个字节的,给我们字节扩展用的。可以利用这前八个字节做些事情。比如:
我们传一个图片,首先要知道它怎么结束,可以给他一个结束标识,如,“/n/n”。第一个4字节我们可以把图片的总长度传过去,根据这个长度判断ImageDate是否传完。第二个字节可以传它的数据格式,定义为content,用来告诉客户端和服务器端传的是什么。然后再拼接为ImageData,并传送到Action。
代码Demo
1 #import "ViewController.h" 2 #import "GCDAsyncSocket.h" 3 4 static NSString *server_host = @"127.0.0.1"; 5 static const short server_port = 6969; 6 7 #define VA_Commadn_id 0x00000001 8 9 @interface ViewController ()<GCDAsyncSocketDelegate> 10 11 @property (strong, nonatomic) GCDAsyncSocket *clientSocket; 12 @property (weak, nonatomic) IBOutlet UITextField *textField; 13 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 // Do any additional setup after loading the view, typically from a nib. 21 [self initGCDAsyncSocket]; 22 } 23 24 - (void)initGCDAsyncSocket{ 25 //创建 Socket 26 if (_clientSocket == nil) { 27 _clientSocket = [[GCDAsyncSocket alloc] initWithDelegate:self 28 delegateQueue:dispatch_get_main_queue()]; 29 } 30 } 31 32 - (BOOL)connect{ 33 NSError *error = nil; 34 BOOL connectFlag = [_clientSocket connectToHost:server_host onPort:server_port error:&error]; 35 if (error) { 36 NSLog(@"%@",error); 37 } 38 return connectFlag; 39 } 40 41 - (void)disConnect{ 42 [_clientSocket disconnect]; 43 } 44 45 #pragma mark -- GCDAsyncSocketDelegate 46 //连接成功回调 47 - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{ 48 NSLog(@"连接成功"); 49 50 [_clientSocket readDataWithTimeout:-1 tag:0]; 51 } 52 53 //断开连接 54 - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{ 55 NSLog(@"断开连接%@",err.localizedDescription); 56 } 57 58 //接受消息 59 - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{ 60 NSLog(@"接受到消息"); 61 [_clientSocket readDataWithTimeout:-1 tag:0]; 62 } 63 64 - (void)sendImage{ 65 //图片数据 66 UIImage *img = [UIImage imageNamed:@"icon_socket"]; 67 NSData *imgData = UIImagePNGRepresentation(img); 68 69 NSMutableData *totalData = [NSMutableData data]; 70 71 //0~3 总长度,前四个字节的总长度 72 73 unsigned int totalSize = (int)imgData.length + 4 + 4; 74 75 NSData *totalSizeData = [NSData dataWithBytes:&totalSize length:4]; 76 //数据拼接 77 [totalData appendData:totalSizeData]; 78 79 // 拼接标识 80 unsigned int commandId = VA_Commadn_id; 81 82 NSData *commandIdData = [NSData dataWithBytes:&commandId length:4]; 83 84 [totalData appendData:commandIdData]; 85 86 //拼接图片数据 87 [totalData appendData:imgData]; 88 89 //发送数据 90 [_clientSocket writeData:totalData withTimeout:-1 tag:0]; 91 92 } 93 94 95 #pragma mark -- Button Action 96 97 - (IBAction)connectAction:(UIButton *)sender{ 98 [self connect]; 99 } 100 101 - (IBAction)disConnectAction:(UIButton *)sender{ 102 [self disConnect]; 103 } 104 105 - (IBAction)sendAction:(UIButton *)sender{ 106 NSData *data = [_textField.text dataUsingEncoding:NSUTF8StringEncoding]; 107 108 [_clientSocket writeData:data withTimeout:-1 tag:0]; 109 } 110 111 - (IBAction)sendImageAction:(id)sender { 112 [self sendImage]; 113 } 114 115 116 - (void)didReceiveMemoryWarning { 117 [super didReceiveMemoryWarning]; 118 // Dispose of any resources that can be recreated. 119 } 120 121 122 @end
使用的第三方库的Podfile:
1 platform:ios, ‘9.0‘ 2 3 target "002--AsyncSocket" do 4 pod ‘CocoaAsyncSocket‘ 5 end
即时通讯通过画板来绘图可以将绘制贝塞尔尔曲线的CGPoint点放入一个数组,但是CGPoint点是不能传递过去的,会崩溃的。可以将CGPoint点转化为字典,再传递过去。
标签:ble 标识 warning bsp led fine 是什么 个数 XML
原文地址:https://www.cnblogs.com/EchoHG/p/9193740.html