2013-05-31 23:13:03 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
http://www.cocos2d-x.org
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
2013-06-06 14:16:57 +08:00
|
|
|
|
|
|
|
"[WebSocket module] is based in part on the work of the libwebsockets project
|
|
|
|
(http://libwebsockets.org)"
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CC_WEBSOCKET_H__
|
|
|
|
#define __CC_WEBSOCKET_H__
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCPlatformMacros.h"
|
2014-01-21 09:52:43 +08:00
|
|
|
#include "CCStdC.h"
|
2013-05-31 23:13:03 +08:00
|
|
|
#include <list>
|
2014-01-20 15:03:30 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2013-07-25 10:33:19 +08:00
|
|
|
struct libwebsocket;
|
|
|
|
struct libwebsocket_context;
|
|
|
|
struct libwebsocket_protocols;
|
|
|
|
|
2014-01-02 16:25:35 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
namespace network {
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
class WsThreadHelper;
|
|
|
|
class WsMessage;
|
|
|
|
|
|
|
|
class WebSocket
|
|
|
|
{
|
|
|
|
public:
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-05-31 23:13:03 +08:00
|
|
|
WebSocket();
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-05-31 23:13:03 +08:00
|
|
|
virtual ~WebSocket();
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
|
|
|
* @brief Data structure for message
|
|
|
|
*/
|
|
|
|
struct Data
|
|
|
|
{
|
2014-01-09 15:18:06 +08:00
|
|
|
Data():bytes(nullptr), len(0), issued(0), isBinary(false){}
|
2013-05-31 23:13:03 +08:00
|
|
|
char* bytes;
|
2014-01-09 13:29:56 +08:00
|
|
|
ssize_t len, issued;
|
2013-05-31 23:13:03 +08:00
|
|
|
bool isBinary;
|
|
|
|
};
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
|
|
|
* @brief Errors in websocket
|
|
|
|
*/
|
2013-07-26 12:08:18 +08:00
|
|
|
enum class ErrorCode
|
2013-05-31 23:13:03 +08:00
|
|
|
{
|
2013-07-26 12:08:18 +08:00
|
|
|
TIME_OUT,
|
|
|
|
CONNECTION_FAILURE,
|
|
|
|
UNKNOWN,
|
|
|
|
};
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-07-26 12:08:18 +08:00
|
|
|
/**
|
|
|
|
* Websocket state
|
|
|
|
*/
|
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
CONNECTING,
|
|
|
|
OPEN,
|
|
|
|
CLOSING,
|
|
|
|
CLOSED,
|
2013-05-31 23:13:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The delegate class to process websocket events.
|
|
|
|
*/
|
|
|
|
class Delegate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~Delegate() {}
|
|
|
|
virtual void onOpen(WebSocket* ws) = 0;
|
|
|
|
virtual void onMessage(WebSocket* ws, const Data& data) = 0;
|
|
|
|
virtual void onClose(WebSocket* ws) = 0;
|
2013-06-03 09:55:43 +08:00
|
|
|
virtual void onError(WebSocket* ws, const ErrorCode& error) = 0;
|
2013-05-31 23:13:03 +08:00
|
|
|
};
|
2014-03-24 17:58:35 +08:00
|
|
|
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
|
|
|
* @brief The initialized method for websocket.
|
|
|
|
* It needs to be invoked right after websocket instance is allocated.
|
|
|
|
* @param delegate The delegate which want to receive event from websocket.
|
|
|
|
* @param url The URL of websocket server.
|
|
|
|
* @return true: Success, false: Failure
|
|
|
|
*/
|
|
|
|
bool init(const Delegate& delegate,
|
|
|
|
const std::string& url,
|
2014-01-09 15:18:06 +08:00
|
|
|
const std::vector<std::string>* protocols = nullptr);
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
|
|
|
* @brief Sends string data to websocket server.
|
|
|
|
*/
|
|
|
|
void send(const std::string& message);
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
|
|
|
* @brief Sends binary data to websocket server.
|
|
|
|
*/
|
|
|
|
void send(const unsigned char* binaryMsg, unsigned int len);
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
|
|
|
* @brief Closes the connection to server.
|
|
|
|
*/
|
|
|
|
void close();
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
/**
|
|
|
|
* @brief Gets current state of connection.
|
|
|
|
*/
|
2013-06-03 09:55:43 +08:00
|
|
|
State getReadyState();
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
private:
|
|
|
|
virtual void onSubThreadStarted();
|
|
|
|
virtual int onSubThreadLoop();
|
|
|
|
virtual void onSubThreadEnded();
|
|
|
|
virtual void onUIThreadReceiveMessage(WsMessage* msg);
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
|
|
|
|
friend class WebSocketCallbackWrapper;
|
|
|
|
int onSocketCallback(struct libwebsocket_context *ctx,
|
|
|
|
struct libwebsocket *wsi,
|
2013-07-25 10:33:19 +08:00
|
|
|
int reason,
|
2013-12-06 16:32:06 +08:00
|
|
|
void *user, void *in, ssize_t len);
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
private:
|
2014-03-24 17:58:35 +08:00
|
|
|
State _readyState;
|
2013-05-31 23:13:03 +08:00
|
|
|
std::string _host;
|
|
|
|
unsigned int _port;
|
|
|
|
std::string _path;
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2014-01-20 15:03:30 +08:00
|
|
|
ssize_t _pendingFrameDataLen;
|
|
|
|
ssize_t _currentDataLen;
|
2014-01-09 15:18:06 +08:00
|
|
|
char *_currentData;
|
2014-01-09 13:29:56 +08:00
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
friend class WsThreadHelper;
|
|
|
|
WsThreadHelper* _wsHelper;
|
2014-03-24 17:58:35 +08:00
|
|
|
|
2013-06-03 09:55:43 +08:00
|
|
|
struct libwebsocket* _wsInstance;
|
2013-05-31 23:13:03 +08:00
|
|
|
struct libwebsocket_context* _wsContext;
|
|
|
|
Delegate* _delegate;
|
|
|
|
int _SSLConnection;
|
2013-06-07 21:23:15 +08:00
|
|
|
struct libwebsocket_protocols* _wsProtocols;
|
2013-05-31 23:13:03 +08:00
|
|
|
};
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|
2013-05-31 23:13:03 +08:00
|
|
|
|
2014-01-02 16:25:35 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2013-05-31 23:13:03 +08:00
|
|
|
#endif /* defined(__CC_JSB_WEBSOCKET_H__) */
|