SocketClient callback for Receive method

Hi.

Currently porting my multiplayer game from Cocos2d (Objective-C) to Cocos2dx (C++).
In Cocos2d i used platform dependent Sockets and it is impossible to port this part of code to C++, i am not from C++ world,
so i appeal to you: please help me to implement some code.
My needs is next:
change Sockets to Receive data in another thread using infinite loop and then send data to Delegate.

I found this code somewhere in the Internet and shared it at GitHub:
https://github.com/AlexanderShniperson/socket_cpp

Thank you.

Hi,

I’m doing some work with a couple of cocos2d-x network related clases, like cocos2d::network::HttpRequest and cocos2d::network::HttpResponse, maybe you can see my example here:


If this helps you please let me know if you have any question.

Thanks,

J.

no it can not help me, because HTTP is based on low level Socket programming and source hidden somewhere and i need this low level.
anyway i don’t know how to programming Threads in C++.

What about websockets, socket.io librairies included in Cocos2d-x ?

But my thoughts is you don’t need to worry about sockets, you just made the request like this:
cocos2d::network::HttpRequest* request = new (std::nothrow) cocos2d::network::HttpRequest();
request->setUrl(“https://www.googleapis.com/customsearch/v1?key=AIzaSyDhZSxw5rAjmGLHGBJH5ouHDWnMg42LW4g&cx=003054679763599795063:tka3twkxrbw&searchType=image&fields=items(link,title,image/thumbnailLink)&start=1&num=1&q=capybara”);

request->setRequestType(cocos2d::network::HttpRequest::Type::GET);
request->setResponseCallback(CC_CALLBACK_2(COTDGoogle::onHttpRequestCompleted, this));
request->setTag("GET Google COTD");
cocos2d::network::HttpClient::getInstance()->send(request);
request->release();

And then capture the response, right? Using a call back COTDGoogle::onHttpRequestCompleted:

This works asynchronously so you can add a spinner or block somehow the UI and wait for the response in the callback method.

Another possibility is try to CURL, but I don’t understand how do you work with sockets in cocos2d, and the type of code you are migrating, because with cocos2d you can use the regular iOS frameworks, AFNetworking for example.

Thanks,

J.

thank you, but websocket and also socket.io (it is websocket anyway) is not for me, because server writen with basic stream sockets under Scala.

thank you, but looks like you dont understand the difference between low levele socket and websocket (Sorry I did not mean to offend you).
you suggest use websockets, but it is not my way, server writen with binary stream low level socket and works correctly, i dont want to write a lot of code to support WebSockets.

i also try to use Poco but there is problems with usage (no body knows how to use it) one, two

to understand me correct i try so explain:

  • between client and server i send Protobuf messages;

  • Protobuf gives me full controlled protocol;

  • Protobuf can be serialized to binary then i can send it to server via socket where this message will be deserialized;

  • with Protobuf i working with objects, not json;

If correctly understood you , you want to use Protobuf, I google this and seems there is some articles about how to make it work with cocos2d-x. like this one for IOS https://gist.github.com/BennettSmith/9487468ae3375d0db0cc

i have successfully integrated Protobuf into project, i need only socket client that can receive message at another thread.

Ok, then it seems out of my knowledge scope )

Maybe check Photon SDK implementation, maybe they have something u can use…

In what genre game is, that there is needed such functionality.

dont know what genre is, maybe arcade, my game is based on movie “13” by Gela Babluani, this movie is about Russian Roulette.
Apple blocked my game in case of “Guideline rules”, so i ported it to Android with Cocos2dx, looks like there is a lof of Russian Roulettes :smile: but my will be Real roulette with multiplayer.

no, Photon SDK not for me, all of this function is done at my game, only need socket.

looks like i forgot to answer to your question “In what genre game is, that there is needed such functionality”:
every time you play multiplayer game - know, it is using low level sockets (need for speed, world of warcraft, world of tanks, counter strike etc)

looks like i found some awesome library to work with Sockets, here is that:
http://www.alhem.net/Sockets/

will try to integrate into project.

so i found good solution with help of one good dude.

here is example of usage client & server sockets at cocos2dx: