-
-
Save greentornado/35aab21ef6112cae2af8be68b709a897 to your computer and use it in GitHub Desktop.
cocos2d-x simple websocket wrapper class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ws = require('websocket.io'); | |
var server = ws.listen(8126); | |
server.on('connection', function(socket) { | |
console.log('Connection started'); | |
console.log(socket.constructor); | |
socket.on('message', function(data) { | |
console.log('Message received:' + data); | |
server.clients.forEach(function(client) { | |
client && client.send(data); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SWebSocket.cpp | |
// | |
// Created by Yoshiaki Sugimoto on 2014/08/04. | |
// | |
// | |
#include "SWebSocket.h" | |
USING_NS_CC; | |
using namespace cocos2d::network; | |
SWebSocket* SWebSocket::create(std::string url) | |
{ | |
SWebSocket* ws = new SWebSocket(url); | |
return ws; | |
} | |
SWebSocket::SWebSocket(std::string url) | |
{ | |
_url = url; | |
}; | |
void SWebSocket::connect() | |
{ | |
_websocket = new WebSocket(); | |
_websocket->init(*this, _url.c_str()); | |
} | |
void SWebSocket::close() | |
{ | |
if (_websocket->getReadyState() == WebSocket::State::OPEN) | |
{ | |
_websocket->close(); | |
} | |
} | |
SWebSocket::~SWebSocket() | |
{ | |
_websocket->close(); | |
} | |
void SWebSocket::send(std::string message) | |
{ | |
if (_websocket->getReadyState() == WebSocket::State::OPEN) | |
{ | |
_websocket->send(message); | |
} | |
} | |
void SWebSocket::onOpen(WebSocket* ws) | |
{ | |
CCLOG("WebSocket connection opened: %s", _url.c_str()); | |
if ( onConnectionOpened ) | |
{ | |
onConnectionOpened(); | |
} | |
} | |
void SWebSocket::onMessage(WebSocket* ws, const WebSocket::Data &data) | |
{ | |
if ( onMessageReceived ) | |
{ | |
onMessageReceived(data.bytes); | |
} | |
} | |
void SWebSocket::onError(WebSocket* ws, const WebSocket::ErrorCode& error) | |
{ | |
if ( onErrorOccurred ) | |
{ | |
onErrorOccurred(error); | |
} | |
} | |
void SWebSocket::onClose(WebSocket* ws) | |
{ | |
if ( onConnectionClosed ) | |
{ | |
onConnectionClosed(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SWebSocket.h | |
// | |
// Created by Yoshiaki Sugimoto on 2014/08/04. | |
// | |
// | |
#ifndef __SWebSocket_H__ | |
#define __SWebSocket_H__ | |
#include "cocos2d.h" | |
#include "network/WebSocket.h" | |
class SWebSocket: public cocos2d::network::WebSocket::Delegate | |
{ | |
private: | |
std::string _url; | |
cocos2d::network::WebSocket* _websocket; | |
public: | |
std::function<void(std::string message)> onMessageReceived; | |
std::function<void()> onConnectionClosed; | |
std::function<void(const cocos2d::network::WebSocket::ErrorCode &error)> onErrorOccurred; | |
std::function<void()> onConnectionOpened; | |
SWebSocket(std::string url); | |
void connect(); | |
static SWebSocket* create(std::string url); | |
virtual void onOpen(cocos2d::network::WebSocket* ws); | |
virtual void onMessage(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::Data& data); | |
virtual void onError(cocos2d::network::WebSocket* ws, const cocos2d::network::WebSocket::ErrorCode& error); | |
virtual void onClose(cocos2d::network::WebSocket* ws); | |
void close(); | |
void send(std::string mesage); | |
~SWebSocket(); | |
}; | |
#endif /* defined(__SWebSocket_H__) */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment