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

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

概述WebSocket 首先新建一个空的文件夹,通过npm安装nodejs-websocket: 1 npm install nodejs-websocket 新建app.js文件: 1 2 3 4 5 6 7 8 9 10 11 12 13 var ws = require( "nodejs-websocket" ); ws.createServer(function(conn){      con

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' ); }); });
总结

以上是内存溢出为你收集整理的Cocos2d-x 3.x如何通过WebSocket连接服务器进行数据传输全部内容,希望文章能够帮你解决Cocos2d-x 3.x如何通过WebSocket连接服务器进行数据传输所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1031267.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存