gpt4 book ai didi

xcode - xcode 中的 cocos2d 通过名为 socket.io 的 Objective-C 库与 Node.js 服务器服务进行通信

转载 作者:太空宇宙 更新时间:2023-11-03 22:38:46 24 4
gpt4 key购买 nike

我使用 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)

最佳答案

我发现,

cocos2d 禁用了 ARC,因此不要使用 SRWebSocket。

解决ARC启用的方法是使用。

在cocos2d中使用arc http://www.raywenderlich.com/23854/arc-and-cocos2d-v2-x

关于xcode - xcode 中的 cocos2d 通过名为 socket.io 的 Objective-C 库与 Node.js 服务器服务进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15851165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com