cocos2d-x WebSocket
WebSocket是HTML5开始提供的一种浏览器与服务器间进行全双工通讯的网络技术。在WebSocket API中,浏览器和服务器只需要做一个握手的动作,然后,浏览器和服务器之间就形成了一条快速通道。两者之间就直接可以数据互相传送。
Cocos2d-x引擎集成libwebsockets,并在libwebsockets的客户端API基础上封装了一层易用的接口,使得引擎在C++, JS, Lua层都能方便的使用WebSocket来进行游戏网络通讯。
- #include "network/WebSocket.h"
- class WebSocketTestLayer : public cocos2d::Layer, public cocos2d::network::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);
- cocos2d::network::WebSocket* _wsiSendText = new network::WebSocket();
- _wsiSendText->init(*this, "ws://echo.websocket.org")
- void WebSocketTestLayer::onOpen(network::WebSocket* ws)
- {
- if (ws == _wsiSendText)
- {
- _sendTextStatus->setString("Send Text WS was opened.");
- }
- }
- void WebSocketTestLayer::onMessage(network::WebSocket* ws, const network::WebSocket::Data& data)
- {
- if (!data.isBinary)
- {
- _sendTextTimes++;
- char times[100] = {0};
- sprintf(times, "%d", _sendTextTimes);
- std::string textStr = std::string("response text msg: ")+data.bytes+", "+times;
- log("%s", textStr.c_str());
- _sendTextStatus->setString(textStr.c_str());
- }
- }
- void WebSocketTestLayer::onClose(network::WebSocket* ws)
- {
- if (ws == _wsiSendText)
- {
- _wsiSendText = NULL;
- }
- CC_SAFE_DELETE(ws);
- }
- void WebSocketTestLayer::onError(network::WebSocket* ws, const network::WebSocket::ErrorCode& error)
- {
- log("Error was fired, error code: %d", error);
- if (ws == _wsiSendText)
- {
- char buf[100] = {0};
- sprintf(buf, "an error was fired, code: %d", error);
- _sendTextStatus->setString(buf);
- }
- }
- _wsiSendText->send("Hello WebSocket, I‘m a text message.");
- _wsiSendBinary->send((unsigned char*)buf, sizeof(buf));
- _wsiSendText->close();
- wsSendText = WebSocket:create("ws://echo.websocket.org")
- local function wsSendTextOpen(strData)
- sendTextStatus:setString("Send Text WS was opened.")
- end
- local function wsSendTextMessage(strData)
- receiveTextTimes= receiveTextTimes + 1
- local strInfo= "response text msg: "..strData..", "..receiveTextTimes
- sendTextStatus:setString(strInfo)
- end
- local function wsSendTextClose(strData)
- print("_wsiSendText websocket instance closed.")
- sendTextStatus = nil
- wsSendText = nil
- end
- local function wsSendTextError(strData)
- print("sendText Error was fired")
- end
- if nil ~= wsSendText then
- wsSendText:registerScriptHandler(wsSendTextOpen,cc.WEBSOCKET_OPEN)
- wsSendText:registerScriptHandler(wsSendTextMessage,cc.WEBSOCKET_MESSAGE)
- wsSendText:registerScriptHandler(wsSendTextClose,cc.WEBSOCKET_CLOSE)
- wsSendText:registerScriptHandler(wsSendTextError,cc.WEBSOCKET_ERROR)
- end
- wsSendText:sendString("Hello WebSocket中文, I‘m a text message.")
- wsSendText:close()
- this._wsiSendText = new WebSocket("ws://echo.websocket.org");
- this._wsiSendText.onopen = function(evt) {
- self._sendTextStatus.setString("Send Text WS was opened.");
- };
- this._wsiSendText.onmessage = function(evt) {
- self._sendTextTimes++;
- var textStr = "response text msg: "+evt.data+", "+self._sendTextTimes;
- cc.log(textStr);
- self._sendTextStatus.setString(textStr);
- };
- this._wsiSendText.onerror = function(evt) {
- cc.log("sendText Error was fired");
- };
- this._wsiSendText.onclose = function(evt) {
- cc.log("_wsiSendText websocket instance closed.");
- self._wsiSendText = null;
- };
- this._wsiSendText.send("Hello WebSocket中文, I‘m a text message.");
- _stringConvertToArray:function (strData) {
- if (!strData)
- return null;
- var arrData = new Uint16Array(strData.length);
- for (var i = 0; i < strData.length; i++) {
- arrData[i] = strData.charCodeAt(i);
- }
- return arrData;
- },
- var buf = "Hello WebSocket中文,\0 I‘m\0 a\0 binary\0 message\0.";
- var binary = this._stringConvertToArray(buf);
- this._wsiSendBinary.send(binary.buffer);
- onExit: function() {
- if (this._wsiSendText)
- this._wsiSendText.close();
- },
转自:http://blog.sina.com.cn/s/blog_923fdd9b0101ggao.html
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。