Cocos2d-x
发布时间:2022-05-02 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2dx3.x利用sokcet创建客户端和服务端 (二),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
服务端:
初始化服务端
向指定客户端发送消息
voidsendmessage(HSocketsocket,constchar*data,255); font-size:9.5pt; font-family:新宋体">intcount);
向所有客户端发送消息
void sendmessage(const char* data,int count);
当服务端开启后的回调函数
std::function<void(constchar*ip)>onStart;
当有新连接时的回调函数
void(HSocketsocket)>onNewConnection;
当有消息时的回调函数
std::function<void(intcount)>onRecv;
当有客户端断开连接时的回调函数
HSocketsocket)>onDisconnect;
SocketServer.h
- #ifndef__SOCKET_SERVER_H__@H_607_104@
- #define__SOCKET_SERVER_H__@H_607_104@
- @H_607_104@
- #include"SocketBase.h"@H_607_104@
- classSocketServer:publicSocketBase@H_607_104@
- {@H_607_104@
- public:@H_607_104@
- staticSocketServer*create();@H_607_104@
- @H_607_104@
- SocketServer();@H_607_104@
- ~SocketServer();@H_607_104@
- boolstartServer();@H_607_104@
- voidsendmessage(HSocketsocket,constchar*data,intcount);@H_607_104@
- voidsendmessage(@H_801_197@intcount);@H_607_104@
- std::function<void(@H_801_197@char*ip)>onStart;@H_607_104@
- std::function<void(HSocketsocket)>onNewConnection;@H_607_104@
- intcount)>onRecv;@H_607_104@
- void(HSocketsocket)>onDisconnect;@H_607_104@
- private:@H_607_104@
- boolinitServer();@H_607_104@
- voidacceptClient();@H_607_104@
- voidacceptFunc();@H_607_104@
- voidnewClientConnected(HSocketsocket);@H_607_104@
- voidrecvmessage(HSocketsocket);@H_607_104@
- private:@H_607_104@
- HSocket_socketServer;@H_607_104@
- std::list<HSocket>_clientSockets;@H_607_104@
- };@H_607_104@
- #endif@H_607_104@
SocketServer.cpp