Cocos2dx连接Node.js SocketIO服务器握手失败求助
Cocos2dx SocketIO 连接 Node.js 服务器握手失败解决方案
问题描述
在Cocos2dx中通过SocketIO连接Node.js搭建的SocketIO服务器时,出现握手失败问题,无法正常建立连接,但该服务器可正常响应浏览器端的连接请求。错误日志显示Error buffer is empty,尝试过多个SocketIO版本仍未解决。
相关代码
HelloWorldScene.h
class HelloWorld : public cocos2d::Layer, public SocketIO::SIODelegate { public: ... // socket.io event listener void onReceiveEvent(SIOClient* client, const std::string& data); // SIODelegate virtual void onConnect(SIOClient* client); virtual void onMessage(SIOClient* client, const std::string& data); virtual void onClose(SIOClient* client); virtual void onError(SIOClient* client, const std::string& data); CREATE_FUNC(HelloWorld); protected: private: int index; SIOClient* _client; TextField* editBox; };
HelloWorldScene.cpp
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); // connect to server _client = SocketIO::connect("http://192.168.0.105:3010", *this); return true; } void HelloWorld::onConnect(SIOClient* client) { // SocketIO::connect success } void HelloWorld::onMessage(SIOClient* client, const std::string& data) { // SocketIO::send receive } void HelloWorld::onClose(SIOClient* client) { // SocketIO::disconnect success } void HelloWorld::onError(SIOClient* client, const std::string& data) { // SocketIO::failed }
Node.js服务器代码
var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function (req, res) { res.sendfile('index.html'); }); var handleClient = function (socket) { console.log('connection'); //testing simple message socket.on('message', function (msg) { console.log('Default namespace received message: ' + msg); socket.send('echo: ' + msg); }); var listen = function() { http.listen(3010); console.log('listening on *:3010'); } module.exports.listen = listen;
解决方案
一、强制匹配SocketIO版本兼容性
Cocos2dx自带的SocketIO客户端(v1.x分支)仅兼容Node.js SocketIO服务器v2.x版本,v3+版本修改了握手协议,会直接导致握手失败:
- 卸载当前服务器端SocketIO:
npm uninstall socket.io
- 安装指定兼容版本:
npm install socket.io@2.5.0
二、修正客户端连接代码
- 补充连接地址的结尾斜杠,部分客户端版本对路径格式敏感:
_client = SocketIO::connect("http://192.168.0.105:3010/", *this);
- 增强错误回调的日志输出,获取更具体的失败原因:
void HelloWorld::onError(SIOClient* client, const std::string& data) { CCLOG("SocketIO Error Detail: %s", data.c_str()); }
三、修复Node.js服务器代码语法错误
你的服务器代码存在函数未闭合、未监听连接事件的问题,修正后代码:
var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function (req, res) { res.sendFile('index.html'); }); var handleClient = function (socket) { console.log('connection'); socket.on('message', function (msg) { console.log('Default namespace received message: ' + msg); socket.send('echo: ' + msg); }); }; // 必须添加此监听,否则服务器不会处理客户端连接 io.on('connection', handleClient); var listen = function() { http.listen(3010, function(){ console.log('listening on *:3010'); }); } module.exports.listen = listen;
四、排查网络与权限问题
- 确认Cocos2dx运行设备和服务器在同一局域网,用
ping 192.168.0.105测试连通性 - 关闭两端防火墙/安全软件,避免端口3010被拦截
- 配置平台网络权限:
- Android:在
AndroidManifest.xml中添加<uses-permission android:name="android.permission.INTERNET" /> - iOS:在
Info.plist中添加HTTP访问权限(若未用HTTPS):<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
- Android:在
五、启用调试日志定位问题
在服务器端开启SocketIO调试日志,查看握手全过程:
- 安装debug模块:
npm install debug
- 启动服务器时加载调试日志:
# Linux/Mac DEBUG=socket.io* node your-server-file.js # Windows set DEBUG=socket.io* && node your-server-file.js
内容的提问来源于stack exchange,提问作者HƯNG LƯU CHẤN
相关产品推荐
相关产品推荐

