大佬教程收集整理的这篇文章主要介绍了[Cocos2d-x]在Cocos2d-x 3.x版本中如何通过WebSocket连接服务器进行数据传输,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
首先新建一个空的文件夹,通过npm安装nodejs-websocket
:
npm install nodejs-websocket
新建app.js
文件:
var ws = require("nodejs-websocket"); ws.createServer(function(conn){ conn.on("text",function (str) { console.log("get the message: "+str); conn.sendText("the server got the message"); }) conn.on("close",function (code,reason) { console.log("connection closed"); }); conn.on("error",68)">"an error !"); }); }).listen(8001);
通过node app.js
启动,这样服务器就搭建好了。
#include "network/WebSocket.h"
class HelloWorld : public cocos2d::Layer,public cocos2d::network::WebSocket::Delegate
// for virtual function in websocket delegate 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); // the websocket io client cocos2d::network::WebSocket* _wsiClient;
_wsiClient = new cocos2d::network::WebSocket(); _wsiClient->init(*this,"ws://localhost:8001");
// 开始socket连接 void HelloWorld::onOpen(cocos2d::network::WebSocket* ws) { CCLOG("OnOpen"); } // 接收到了消息 void HelloWorld::onmessage(cocos2d::network::WebSocket* ws,const cocos2d::network::WebSocket::Data& data) { std::String textStr = 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,68)">"an error was fired,code: %d",error); } CCLOG("Error was fired,error code: %d",error); }
还有一个使用SocketIO的方案,尚未尝试,明天测试一下:
// require http module (to start server) and Socket.IO var http = require('http'),io = require('socket.io'); // Start the server at port 8080 var server = http.createServer(function(req,res){ // Send HTML headers and message res.writeHead(200,{ 'Content-Type': 'text/html' }); res.end('<h1>Hello Socket Lover!</h1>'); }); server.listen(8080); // Create a Socket.IO instance,passing it our server var socket = io.listen(server); // Add a connect listener socket.on('connection',function(client){ // Create perioDical which ends a message to the client every 5 seconds var interval = seTinterval(function() { client.send('This is a message from the server! ' + new Date().getTime()); },5000); // success! Now listen to messages to be received client.on('message',function(event){ console.log('Received message from client!',event); }); client.on('disconnect',function(){ clearInterval(interval); console.log('Server has disconnected'); }); });
以上是大佬教程为你收集整理的[Cocos2d-x]在Cocos2d-x 3.x版本中如何通过WebSocket连接服务器进行数据传输全部内容,希望文章能够帮你解决[Cocos2d-x]在Cocos2d-x 3.x版本中如何通过WebSocket连接服务器进行数据传输所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。