Skip to content

Instantly share code, notes, and snippets.

@ysugimoto
Last active April 7, 2017 09:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ysugimoto/9bd511f417e96dc70c79 to your computer and use it in GitHub Desktop.
Save ysugimoto/9bd511f417e96dc70c79 to your computer and use it in GitHub Desktop.
cocos2d-x simple websocket wrapper class
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);
});
});
});
//
// 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();
}
}
//
// 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