我正在尝试使用http://code.google.com/p/iphonebits/source/browse/trunk/src/TicTacToeNew/中的示例代码在两个 iOS 设备之间发送数据
客户端连接良好,数据在两个设备之间传输得很好。客户端以大约 10 次/秒的速度发送数据包。但是,我在接收数据之间遇到了非常奇怪的停顿。
在接收代码中,每次收到请求时我都会放置一个调试语句:
- (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
NSLog(@"stream: handleEvent : %d", eventCode);
switch(eventCode) {
// ... handle event and print out received data
}
}
相关的发送代码在此处的 sendQueuedData 函数中:http ://code.google.com/p/iphonebits/source/browse/trunk/src/TicTacToeNew/Classes/OnlineSession.m?r=19
第一秒左右的请求按计划进行(尽管它似乎将请求一起批处理):
2011-03-13 12:28:08.759 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":1.98,"y":0.01}
[DEBUG] Fire Event: data: {"x":1.98,"y":0.00}
[DEBUG] Fire Event: data: {"x":2.11,"y":0.01}
2011-03-13 12:28:08.781 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":2.07,"y":0.03}
[DEBUG] Fire Event: data: {"x":2.02,"y":0.03}
2011-03-13 12:28:08.923 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":1.98,"y":0.01}
[DEBUG] Fire Event: data: {"x":1.86,"y":0.01}
2011-03-13 12:28:08.996 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":1.73,"y":0.01}
2011-03-13 12:28:09.171 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":2.38,"y":-0.56}
2011-03-13 12:28:09.182 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.332 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.496 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.501 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:09.598 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
...但随后请注意后续调用的时间戳:
2011-03-13 12:28:10.163 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.770 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.772 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.777 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:13.868 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.016 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.177 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.340 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:14.424 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:15.065 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:16.338 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:18.740 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
2011-03-13 12:28:18.741 bluetoothmodule[52188:207] stream: handleEvent : 2
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
[DEBUG] Fire Event: data: {"x":0.00,"y":-0.00}
如您所见,handleEvent() 回调被偶尔调用,显然不适合实时应用程序。有谁知道为什么?我是否应该从使用 NSStreams 切换到使用UDP 的http://code.google.com/p/cocoaasyncsocket/之类的东西?