0

I'm trying to connect a socket in Cocos2dx with a server in Nodejs but it does not work as expected. Here is my code:

In HelloWorldScene.h

class HelloWorld : public cocos2d::Layer, public SocketIO::SIODelegate { public: ... // socket.io even\vent 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; }; 

In HelloWordScene.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 } 

And my server, (which is working well with connect in a browser)

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; 

This is the error log: Error buffer is empty

Is there any solution to this ? I have tried many version of SocketIO but so far nothing works.

1 Answer 1

0

I think the SocketIO implemented by cocos is incompatible with the version from node. Try allow backward compatible by adding this var io = require('socket.io')(http, {allowEIO3: true});

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.