Remove websockets.

This commit is contained in:
halx99 2019-11-24 15:08:59 +08:00
parent c8f85a4a95
commit 70b71c85cc
5 changed files with 0 additions and 3211 deletions

View File

@ -4,8 +4,6 @@ if(ANDROID)
)
set(COCOS_NETWORK_SRC
network/HttpClient-android.cpp
network/SocketIO.cpp
network/WebSocket.cpp
network/CCDownloader.cpp
network/CCDownloader-android.cpp
network/Uri.cpp
@ -18,8 +16,6 @@ elseif(APPLE)
set(COCOS_NETWORK_SRC
network/CCDownloader-apple.mm
network/HttpClient.cpp
network/SocketIO.cpp
network/WebSocket.cpp
network/CCDownloader.cpp
network/CCDownloader-curl.cpp
network/Uri.cpp
@ -27,8 +23,6 @@ elseif(APPLE)
else()
set(COCOS_NETWORK_SRC
network/HttpClient.cpp
network/SocketIO.cpp
network/WebSocket.cpp
network/CCDownloader.cpp
network/CCDownloader-curl.cpp
network/Uri.cpp
@ -40,9 +34,7 @@ set(COCOS_NETWORK_HEADER
network/CCDownloader-curl.h
network/CCIDownloaderImpl.h
network/CCDownloader.h
network/WebSocket.h
network/Uri.h
network/SocketIO.h
network/HttpClient.h
network/HttpResponse.h
network/HttpRequest.h

File diff suppressed because it is too large Load Diff

View File

@ -1,300 +0,0 @@
/****************************************************************************
Copyright (c) 2015 Chris Hannon http://www.channon.us
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*based on the SocketIO library created by LearnBoost at http://socket.io
*using spec version 1 found at https://github.com/LearnBoost/socket.io-spec
Usage is described below, a full working example can be found in TestCpp under ExtionsTest/NetworkTest/SocketIOTest
creating a new connection to a socket.io server running at localhost:3000
SIOClient *client = SocketIO::connect(*delegate, "ws://localhost:3000");
the connection process will begin and if successful delegate::onOpen will be called
if the connection process results in an error, delegate::onError will be called with the err msg
sending a message to the server
client->send("Hello!");
emitting an event to be handled by the server, argument json formatting is up to you
client->emit("eventname", "[{\"arg\":\"value\"}]");
registering an event callback, target should be a member function in a subclass of SIODelegate
CC_CALLBACK_2 is used to wrap the callback with std::bind and store as an SIOEvent
client->on("eventname", CC_CALLBACK_2(TargetClass::targetfunc, *targetclass_instance));
event target function should match this pattern, *this pointer will be made available
void TargetClass::targetfunc(SIOClient *, const std::string&)
disconnect from the endpoint by calling disconnect(), onClose will be called on the delegate once complete
in the onClose method the pointer should be set to NULL or used to connect to a new endpoint
client->disconnect();
****************************************************************************/
#ifndef __CC_SOCKETIO_H__
#define __CC_SOCKETIO_H__
#include <string>
#include <unordered_map>
#include <vector>
#include "platform/CCPlatformMacros.h"
#include "base/CCMap.h"
/**
* @addtogroup network
* @{
*/
NS_CC_BEGIN
namespace network {
//forward declarations
class SIOClientImpl;
class SIOClient;
/**
* Singleton and wrapper class to provide static creation method as well as registry of all sockets.
*
* @lua NA
*/
class CC_DLL SocketIO
{
public:
/**
* Get instance of SocketIO.
*
* @return SocketIO* the instance of SocketIO.
*/
static SocketIO* getInstance();
static void destroyInstance();
/**
* The delegate class to process socket.io events.
* @lua NA
*/
class SIODelegate
{
public:
/** Destructor of SIODelegate. */
virtual ~SIODelegate() {}
/**
* This is kept for backwards compatibility, connect is now fired as a socket.io event "connect"
*
* This function would be called when the related SIOClient object receive messages that mean it have connected to endpoint successfully.
*
* @param client the connected SIOClient object.
*/
virtual void onConnect(SIOClient* client) { CCLOG("SIODelegate onConnect fired"); };
/**
* This is kept for backwards compatibility, message is now fired as a socket.io event "message"
*
* This function would be called when the related SIOClient object receive message or json message.
*
* @param client the connected SIOClient object.
* @param data the message,it could be json message
*/
virtual void onMessage(SIOClient* client, const std::string& data) { CCLOG("SIODelegate onMessage fired with data: %s", data.c_str()); };
/**
* Pure virtual callback function, this function should be overridden by the subclass.
*
* This function would be called when the related SIOClient object disconnect or receive disconnect signal.
*
* @param client the connected SIOClient object.
*/
virtual void onClose(SIOClient* client) = 0;
/**
* Pure virtual callback function, this function should be overridden by the subclass.
*
* This function would be called when the related SIOClient object receive error signal or didn't connect the endpoint but do some network operation, eg.,send and emit,etc.
*
* @param client the connected SIOClient object.
* @param data the error message
*/
virtual void onError(SIOClient* client, const std::string& data) = 0;
/**
* Fire event to script when the related SIOClient object receive the fire event signal.
*
* @param client the connected SIOClient object.
* @param eventName the event's name.
* @param data the event's data information.
*/
virtual void fireEventToScript(SIOClient* client, const std::string& eventName, const std::string& data) { CCLOG("SIODelegate event '%s' fired with data: %s", eventName.c_str(), data.c_str()); };
};
/**
* Static client creation method, similar to socketio.connect(uri) in JS.
* @param uri the URI of the socket.io server.
* @param delegate the delegate which want to receive events from the socket.io client.
* @return SIOClient* an initialized SIOClient if connected successfully, otherwise nullptr.
*/
static SIOClient* connect(const std::string& uri, SocketIO::SIODelegate& delegate);
/**
* Static client creation method, similar to socketio.connect(uri) in JS.
* @param uri the URI of the socket.io server.
* @param delegate the delegate which want to receive events from the socket.io client.
* @param caFilePath The ca file path for wss connection
* @return SIOClient* an initialized SIOClient if connected successfully, otherwise nullptr.
*/
static SIOClient* connect(const std::string& uri, SocketIO::SIODelegate& delegate, const std::string& caFilePath);
private:
SocketIO();
virtual ~SocketIO();
static SocketIO *_inst;
std::unordered_map<std::string, std::weak_ptr<SIOClientImpl
>> _sockets;
std::shared_ptr<SIOClientImpl> getSocket(const std::string& uri);
void addSocket(const std::string& uri, std::shared_ptr<SIOClientImpl>& socket);
void removeSocket(const std::string& uri);
friend class SIOClientImpl;
private:
CC_DISALLOW_COPY_AND_ASSIGN(SocketIO)
};
//c++11 style callbacks entities will be created using CC_CALLBACK (which uses std::bind)
typedef std::function<void(SIOClient*, const std::string&)> SIOEvent;
//c++11 map to callbacks
typedef std::unordered_map<std::string, SIOEvent> EventRegistry;
/**
* A single connection to a socket.io endpoint.
*
* @lua NA
*/
class CC_DLL SIOClient
: public cocos2d::Ref
{
private:
friend class SocketIO; // Only SocketIO class could contruct a SIOClient instance.
std::string _path, _tag;
bool _connected;
std::shared_ptr<SIOClientImpl> _socket;
SocketIO::SIODelegate* _delegate = nullptr;
EventRegistry _eventRegistry;
void fireEvent(const std::string& eventName, const std::string& data);
void onOpen();
void onConnect();
void socketClosed();
friend class SIOClientImpl;
bool isConnected() const;
void setConnected(bool);
/**
* Constructor of SIOClient class.
*
* @param host the string that represent the host address.
* @param port the int value represent the port number.
* @param path the string that represent endpoint.
* @param impl the SIOClientImpl object.
* @param delegate the SIODelegate object.
*/
SIOClient(const std::string& path, std::shared_ptr<SIOClientImpl>& impl, SocketIO::SIODelegate& delegate);
/**
* Destructor of SIOClient class.
*/
virtual ~SIOClient();
public:
/**
* Get the delegate for the client
* @return the delegate object for the client
*/
SocketIO::SIODelegate* getDelegate() { return _delegate; };
/**
* Disconnect from the endpoint, onClose will be called for the delegate when complete
*/
void disconnect();
/**
* Send a message to the socket.io server.
*
* @param s message.
*/
void send(const std::string& s);
void send(const std::vector<std::string>& s);
/**
* Emit the eventname and the args to the endpoint that _path point to.
* @param eventname
* @param args
*/
void emit(const std::string& eventname, const std::string& args);
void emit(const std::string& eventname, const std::vector<std::string> &args);
/**
* Used to register a socket.io event callback.
* Event argument should be passed using CC_CALLBACK2(&Base::function, this).
* @param eventName the name of event.
* @param e the callback function.
*/
void on(const std::string& eventName, SIOEvent e);
/**
* Set tag of SIOClient.
* The tag is used to distinguish the various SIOClient objects.
* @param tag string object.
*/
void setTag(const char* tag);
/**
* Get tag of SIOClient.
* @return const char* the pointer point to the _tag.
*/
const char* getTag()
{
return _tag.c_str();
}
};
}
NS_CC_END
// end group
/// @}
#endif /* defined(__CC_JSB_SOCKETIO_H__) */

File diff suppressed because it is too large Load Diff

View File

@ -1,283 +0,0 @@
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
"[WebSocket module] is based in part on the work of the libwebsockets project
(http://libwebsockets.org)"
****************************************************************************/
#pragma once
#include <string>
#include <vector>
#include <mutex>
#include <memory> // for std::shared_ptr
#include <atomic>
#include <condition_variable>
#include "platform/CCPlatformMacros.h"
#include "platform/CCStdC.h"
struct lws;
struct lws_protocols;
struct lws_vhost;
/**
* @addtogroup network
* @{
*/
NS_CC_BEGIN
class EventListenerCustom;
namespace network {
class WsThreadHelper;
/**
* WebSocket is wrapper of the libwebsockets-protocol, let the develop could call the websocket easily.
* Please note that all public methods of WebSocket have to be invoked on Cocos Thread.
*/
class CC_DLL WebSocket
{
public:
/**
* Close all connections and wait for all websocket threads to exit
* @note This method has to be invoked on Cocos Thread
*/
static void closeAllConnections();
/**
* Constructor of WebSocket.
*
* @js ctor
*/
WebSocket();
/**
* Destructor of WebSocket.
*
* @js NA
* @lua NA
*/
virtual ~WebSocket();
/**
* Data structure for message
*/
struct Data
{
Data():bytes(nullptr), len(0), issued(0), isBinary(false), ext(nullptr){}
char* bytes;
ssize_t len, issued;
bool isBinary;
void* ext;
};
/**
* ErrorCode enum used to represent the error in the websocket.
*/
enum class ErrorCode
{
TIME_OUT, /** &lt; value 0 */
CONNECTION_FAILURE, /** &lt; value 1 */
UNKNOWN, /** &lt; value 2 */
};
/**
* State enum used to represent the Websocket state.
*/
enum class State
{
CONNECTING, /** &lt; value 0 */
OPEN, /** &lt; value 1 */
CLOSING, /** &lt; value 2 */
CLOSED, /** &lt; value 3 */
};
/**
* The delegate class is used to process websocket events.
*
* The most member function are pure virtual functions,they should be implemented the in subclass.
* @lua NA
*/
class Delegate
{
public:
/** Destructor of Delegate. */
virtual ~Delegate() {}
/**
* This function to be called after the client connection complete a handshake with the remote server.
* This means that the WebSocket connection is ready to send and receive data.
*
* @param ws The WebSocket object connected
*/
virtual void onOpen(WebSocket* ws) = 0;
/**
* This function to be called when data has appeared from the server for the client connection.
*
* @param ws The WebSocket object connected.
* @param data Data object for message.
*/
virtual void onMessage(WebSocket* ws, const Data& data) = 0;
/**
* When the WebSocket object connected wants to close or the protocol won't get used at all and current _readyState is State::CLOSING,this function is to be called.
*
* @param ws The WebSocket object connected.
*/
virtual void onClose(WebSocket* ws) = 0;
/**
* This function is to be called in the following cases:
* 1. client connection is failed.
* 2. the request client connection has been unable to complete a handshake with the remote server.
* 3. the protocol won't get used at all after this callback and current _readyState is State::CONNECTING.
* 4. when a socket descriptor needs to be removed from an external polling array. in is again the struct libwebsocket_pollargs containing the fd member to be removed. If you are using the internal polling loop, you can just ignore it and current _readyState is State::CONNECTING.
*
* @param ws The WebSocket object connected.
* @param error WebSocket::ErrorCode enum,would be ErrorCode::TIME_OUT or ErrorCode::CONNECTION_FAILURE.
*/
virtual void onError(WebSocket* ws, const ErrorCode& error) = 0;
};
/**
* @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.
* @param protocols The websocket protocols that agree with websocket server
* @param caFilePath The ca file path for wss connection
* @return true: Success, false: Failure.
* @lua NA
*/
bool init(const Delegate& delegate,
const std::string& url,
const std::vector<std::string>* protocols = nullptr,
const std::string& caFilePath = "");
/**
* @brief Sends string data to websocket server.
*
* @param message string data.
* @lua sendstring
*/
void send(const std::string& message);
/**
* @brief Sends binary data to websocket server.
*
* @param binaryMsg binary string data.
* @param len the size of binary string data.
* @lua sendstring
*/
void send(const unsigned char* binaryMsg, unsigned int len);
/**
* @brief Closes the connection to server synchronously.
* @note It's a synchronous method, it will not return until websocket thread exits.
*/
void close();
/**
* @brief Closes the connection to server asynchronously.
* @note It's an asynchronous method, it just notifies websocket thread to exit and returns directly,
* If using 'closeAsync' to close websocket connection,
* be careful of not using destructed variables in the callback of 'onClose'.
*/
void closeAsync();
/**
* @brief Gets current state of connection.
* @return State the state value could be State::CONNECTING, State::OPEN, State::CLOSING or State::CLOSED
*/
State getReadyState();
/**
* @brief Gets the URL of websocket connection.
*/
inline const std::string& getUrl() const { return _url; }
/**
* @brief Gets the protocol selected by websocket server.
*/
inline const std::string& getProtocol() const { return _selectedProtocol; }
private:
// The following callback functions are invoked in websocket thread
void onClientOpenConnectionRequest();
int onSocketCallback(struct lws *wsi, int reason, void *in, ssize_t len);
int onClientWritable();
int onClientReceivedData(void* in, ssize_t len);
int onConnectionOpened();
int onConnectionError();
int onConnectionClosed();
struct lws_vhost* createVhost(struct lws_protocols* protocols, int& sslConnection);
private:
std::mutex _readyStateMutex;
State _readyState;
std::string _url;
std::vector<char> _receivedData;
struct lws* _wsInstance;
struct lws_protocols* _lwsProtocols;
std::string _clientSupportedProtocols;
std::string _selectedProtocol;
std::shared_ptr<std::atomic<bool>> _isDestroyed;
Delegate* _delegate;
std::mutex _closeMutex;
std::condition_variable _closeCondition;
std::vector<char*> _protocolNames;
enum class CloseState
{
NONE,
SYNC_CLOSING,
SYNC_CLOSED,
ASYNC_CLOSING
};
CloseState _closeState;
std::string _caFilePath;
EventListenerCustom* _resetDirectorListener;
friend class WsThreadHelper;
friend class WebSocketCallbackWrapper;
};
} // namespace network {
NS_CC_END
// end group
/// @}