我使用 xcode 4.6.1 cocos2d 2.x、ios sdk 6.1、iphone 6.1 模拟器。
我想通过node.js和socket.io从objective-c到服务器通信。
所以,我尝试了名为 socket.io “socket.IO-objc”的objective-c库
https://github.com/pkyeck/socket.IO-objc
我在 socket.IO-objc 中成功了一个示例“SocketTesterARC”,以与服务器“node.js with socket.io”进行通信。
SocketTesterARC:
(void) viewDidLoad {
[super viewDidLoad];
socketIO = [[SocketIO alloc] initWithDelegate:self];
[socketIO connectToHost:@"localhost" onPort:8888];
[socketIO sendEvent:@"join" withData:@"first room"];
[socketIO sendMessage:@"hihihi"];
}
所以,我将“socket.io”库应用于新的 cocos2d 项目。我从示例“SocketTesterARC”的代码复制到新的 cocos2d 项目。
但是,错误而不是处理!
Connecting to socket with URL: http://localhost:8888/socket.io/1/?t=16807
2013-04-06 21:26:35.129 cocos2dSocketioTest[4311:c07] send()
2013-04-06 21:26:35.130 cocos2dSocketioTest[4311:c07] queue >>> 5:::{"args":["first room"],"name":"join"}
2013-04-06 21:26:35.131 cocos2dSocketioTest[4311:c07] send()
2013-04-06 21:26:35.133 cocos2dSocketioTest[4311:c07] queue >>> 3:::hihihi
2013-04-06 21:26:35.154 cocos2dSocketioTest[4311:1b03] ADDRESPONSE - ADDING TO MEMORY ONLY: http://localhost:8888/socket.io/1/?t=16807
2013-04-06 21:26:35.325 cocos2dSocketioTest[4311:c07] didReceiveResponse() 200
服务器错误日志:
info - handshake authorized brKX0Zfj9Vge80rxJfig
info - handshake authorized JsDnUP2nPXUJh83UJfih
info - handshake authorized pqoc5KS5hD2GsXcsJfii
info - handshake authorized tX3OQfsgbs-pdWFIJfij
服务器代码:
io.sockets.on('connection', function (socket) {
console.log("connection");
//join event
socket.on('join', function(data) {
console.log('join : ' + data);
socket.join(data);
socket.set('room', data);
});
//meesage event
socket.on( 'message', function(data) {
console.log('room : ' + data );
socket.get( 'room', function(error, room) {
io.sockets['in'](room).emit('message', data);
});
});
});
请回答
添加,成功客户端日志:
2013-04-07 13:47:37.417 SocketTesterARC[1274:c07] Connecting to socket with URL:
http://localhost:8888/socket.io/1/?t=16807
2013-04-07 13:47:37.424 SocketTesterARC[1274:c07] send()
2013-04-07 13:47:37.424 SocketTesterARC[1274:c07] queue >>> 5:::{"args":["first room"],"name":"join"}
2013-04-07 13:47:37.425 SocketTesterARC[1274:c07] send()
2013-04-07 13:47:37.425 SocketTesterARC[1274:c07] queue >>> 3:::hihihi
2013-04-07 13:47:37.430 SocketTesterARC[1274:1b03] ADDRESPONSE - ADDING TO MEMORY ONLY: http://localhost:8888/socket.io/1/?t=16807
2013-04-07 13:47:37.436 SocketTesterARC[1274:c07] didReceiveResponse() 200
2013-04-07 13:47:37.436 SocketTesterARC[1274:c07] connectionDidFinishLoading() nTOwJjgvumKn_kNM0alf:60:60:websocket,htmlfile,xhr-polling,jsonp-polling
2013-04-07 13:47:37.436 SocketTesterARC[1274:c07] sid: nTOwJjgvumKn_kNM0alf
2013-04-07 13:47:37.448 SocketTesterARC[1274:c07] heartbeatTimeout: 67.000000
2013-04-07 13:47:37.449 SocketTesterARC[1274:c07] transports: (
websocket,
htmlfile,
"xhr-polling",
"jsonp-polling")
服务器日志:
info - handshake authorized nTOwJjgvumKn_kNM0alf
connection
join : first room
room : hihihi
info - transport end (socket end)