Cocos2d-x 3.x如何通过WebSocket连接服务器进行数据传输

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.x如何通过WebSocket连接服务器进行数据传输前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

WebSocket

首先新建一个空的文件夹,通过npm安装nodejs-websocket:

1
npminstallnodejs-websocket


新建app.js文件

1
2
3
4
5
6
7
8
9
10
11
12
13
varws=require( "nodejs-websocket" );
ws.createServer(function(conn){
conn.on( "text" ,function(str){
console. log ( "getthemessage:" +str);
conn.sendText( "theservergotthemessage" );
})
conn.on( "close" ,function(code,reason){
console. log ( "connectionclosed" );
});
conn.on( "error" ,reason){
console. log ( "anerror!" );
});
}).listen(8001);


通过node app.js启动,这样服务器就搭建好了。


Cocos2d-x

首先在头文件中include头文件

1
#include"network/WebSocket.h"


实现WebSocket的委托:

1
class HelloWorld: public cocos2d::Layer, public cocos2d::network::WebSocket::Delegate


四个委托中定义的函数接口以及一个用来连接的socketClient对象:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//forvirtualfunctioninwebsocketdelegate
virtual void onOpen(cocos2d::network::WebSocket*ws);
virtual void onMessage(cocos2d::network::WebSocket*ws, const cocos2d::network::WebSocket::Data&data);
virtual void onClose(cocos2d::network::WebSocket*ws);
virtual void onError(cocos2d::network::WebSocket*ws, const cocos2d::network::WebSocket::ErrorCode&error);
//thewebsocketioclient
cocos2d::network::WebSocket*_wsiClient;
初始化client:
_wsiClient= new cocos2d::network::WebSocket();
_wsiClient->init(* this , "ws://localhost:8001" );
在cpp文件中实现这些函数
//开始socket连接
void HelloWorld::onOpen(cocos2d::network::WebSocket*ws)
{
CCLOG( "OnOpen" );
}
//接收到了消息
void HelloWorld::onMessage(cocos2d::network::WebSocket*ws, const cocos2d::network::WebSocket::Data&data)
{
std::stringtextStr=data.bytes;
textStr.c_str();
CCLOG(textStr.c_str());
}
//连接关闭
void HelloWorld::onClose(cocos2d::network::WebSocket*ws)
{
if (ws==_wsiClient)
{
_wsiClient=NULL;
}
CC_SAFE_DELETE(ws);
CCLOG( "onClose" );
}
//遇到错误
void HelloWorld::onError(cocos2d::network::WebSocket*ws, const cocos2d::network::WebSocket::ErrorCode&error)
{
if (ws==_wsiClient)
{
char buf[100]={0};
sprintf (buf, "anerrorwasfired,code:%d" ,error);
}
CCLOG( "Errorwasfired,errorcode:%d" ,error);
}


还有一个使用SocketIO的方案(本文作者只提供了源码,但是尚未测试,您可以与作者一起测试,并可以提供反馈):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//RequireHTTPmodule(tostartserver)andSocket.IO
varhttp=require( 'http' ),io=require( 'socket.io' );
//Starttheserveratport8080
varserver=http.createServer(function(req,res){
//SendHTMLheadersandmessage
res.writeHead(200,{ 'Content-Type' : 'text/html' });
res.end( '<h1>HelloSocketLover!</h1>' );
});
server.listen(8080);
//CreateaSocket.IOinstance,passingitourserver
varsocket=io.listen(server);
//Addaconnectlistener
socket.on( 'connection' ,function(client){
//Createperiodicalwhichendsamessagetotheclientevery5seconds
varinterval=setInterval(function(){
client.send( 'Thisisamessagefromtheserver!' + new Date().getTime());
},5000);
//Success!Nowlistentomessagestobereceived
client.on( 'message' ,function(event){
console. log ( 'Receivedmessagefromclient!' ,event);
});
client.on( 'disconnect' ,function(){
clearInterval(interval);
console. log ( 'Serverhasdisconnected' );
});
});
原文链接:https://www.f2er.com/cocos2dx/344949.html

猜你在找的Cocos2d-x相关文章