Skip to content

Instantly share code, notes, and snippets.

@hanfengs hanfengs/ws.m
Last active Sep 17, 2019

Embed
What would you like to do?
[webSocket发送text and binary message]
- (void)SRWebSocketDidOpen {
NSLog(@"开启成功");
//在成功后需要做的操作。。。
NSDictionary *dict = @{
@"ref_text": @"sunny",
@"audio_format": @{
@"type": @"WAV",
@"audio_channel": @(1),
@"sample_rate": @(16000)
}
};
if ([NSJSONSerialization isValidJSONObject:dict]){
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[[SocketRocketUtility instance] sendData:jsonString];
}
NSString *path= [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"5d71c6b41faee04074000001.wav"];
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]];
NSMutableData *dataM = [NSMutableData dataWithData:data];
unsigned char endBytes[3] = {0x45, 0x4f, 0x53};
[dataM appendBytes:&endBytes length:3];
[[SocketRocketUtility instance] sendData:dataM];
}
-------------------------------------------------------------------------------
NSMutableData *data = [[NSMutableData alloc] init];
uint8_t number = 255;
[data appendBytes:&number length:2];
http://cn.voidcc.com/question/p-ckiotlso-sk.html
const NSUInteger dataLength = 130;
unsigned char dataBytes[dataLength] = {
0x01,0x00,0x00,0x04,0x40,0x00,0x00,0x00,0x00,0x41,0x41,0x41,0x41,0x48,0x47,0x5A,
0x30,0x65,0x58,0x42,0x4E,0x4E,0x45,0x45,0x67,0x41,0x41,0x41,0x41,0x41,0x45,0x30,
0x30,0x51,0x53,0x42,0x74,0x63,0x44,0x51,0x79,0x61,0x58,0x4E,0x76,0x62,0x51,0x41,
0x41,0x41,0x2B,0x39,0x74,0x62,0x32,0x39,0x32,0x41,0x41,0x41,0x41,0x62,0x47,0x31,
0x32,0x61,0x47,0x51,0x41,0x41,0x41,0x41,0x41,0x71,0x67,0x59,0x35,0x79,0x36,0x6F,
0x47,0x4F,0x63,0x73,0x41,0x41,0x4B,0x78,0x45,0x41,0x41,0x43,0x34,0x41,0x41,0x41,
0x42,0x41,0x41,0x41,0x42,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
0x41,0x41,0x41,0x41,0x41,0x41,0x51,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
0x41,0x41};
NSData *data = [NSData dataWithBytes:dataBytes length:130];
const NSUInteger startBytesLength = 4;
unsigned char startBytes[startBytesLength] = {0x40, 0x00, 0x00, 0x00};
const NSUInteger stopBytesLength = 1;
unsigned char stopBytes[stopBytesLength] = {0x34};
NSData *startData = [NSData dataWithBytes:startBytes length:startBytesLength];
NSData *stopData = [NSData dataWithBytes:stopBytes length:stopBytesLength];
NSRange startRange = [data rangeOfData:startData options:0 range:NSMakeRange(0, dataLength)];
NSUInteger start = startRange.location;
NSRange stopRange = [data rangeOfData:stopData options:0 range:NSMakeRange(start+startBytesLength, dataLength-(start+startBytesLength))];
NSUInteger length = stopRange.location - start;
NSUInteger stopLocation = stopRange.location+stopBytesLength;
NSData *extractData = [data subdataWithRange:NSMakeRange(start, length)];
NSLog(@"extractData: %@", extractData);
NSMutableData *newData = [NSMutableData data];
[newData appendData:[data subdataWithRange:NSMakeRange(0, start)]];
[newData appendData:[data subdataWithRange:NSMakeRange(stopLocation, dataLength-stopLocation)]];
NSLog(@"newData: %@", newData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.