I will write it
@You need to get the following library.
https://github.com/msgpack/msgpack-objectivec
@ On the Iphone
static void listenerCallback(CFSocketRef socket, CFSocketCallBackType type,
CFDataRef address, const void *data, void *info) {
NSString* str = nil;
switch (type) {
case kCFSocketNoCallBack:
str = @"kCFSocketNoCallBack";
break;
case kCFSocketReadCallBack:
str = @"kCFSocketReadCallBack";
break;
case kCFSocketAcceptCallBack:
str = @"kCFSocketAcceptCallBack";
break;
case kCFSocketDataCallBack:
str = @"kCFSocketDataCallBack";
break;
case kCFSocketConnectCallBack:
str = @"kCFSocketConnectCallBack";
break;
case kCFSocketWriteCallBack:
str = @"kCFSocketWriteCallBack";
break;
default:
break;
}
if(type == kCFSocketDataCallBack) {
// Get a message from server
NSData* receiveData = (NSData*)data;
NSDictionary* parsed = [receiveData messagePackParse];
NSNumber *numx = [parsed objectForKey:@"x"];
NSNumber *numy = [parsed objectForKey:@"y"];
NSLog(@"numx is %f", [numx floatValue]);
NSLog(@"numy is %f", [numy floatValue]);
/* another way to print
UInt8 *gotData = CFDataGetBytePtr((CFDataRef)data);
int len = CFDataGetLength((CFDataRef)data);
for(int i=0; i < len; i++) {
NSLog(@"%c",*(gotData+i));
}
*/
} else if(type == kCFSocketWriteCallBack) {
// Send a message to server
CGPoint translation = CGPointMake(5.0, 6.0);
NSNumber *numx = [NSNumber numberWithFloat:translation.x];
NSNumber *numy = [NSNumber numberWithFloat:translation.y];
NSDictionary *someDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
numx, @"x",
numy, @"y",
nil];
NSData* packed = [someDictionary messagePack];
CFSocketSendData(socket, NULL, (CFDataRef)packed, 10);
// CFRelease((CFDataRef)packed);
}
}
@ On the Server
....
No comments:
Post a Comment