mirror of https://github.com/axmolengine/axmol.git
SocketIO#emit/send support multiple parameters (#18944)
This commit is contained in:
parent
ec1fc92e37
commit
81b1e904cd
|
@ -384,10 +384,12 @@ public:
|
|||
void disconnectFromEndpoint(const std::string& endpoint);
|
||||
|
||||
void send(const std::string& endpoint, const std::string& s);
|
||||
void send(const std::string& endpoint, const std::vector<std::string>& s);
|
||||
void send(SocketIOPacket *packet);
|
||||
void emit(const std::string& endpoint, const std::string& eventname, const std::string& args);
|
||||
void emit(const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args);
|
||||
|
||||
|
||||
friend class SIOClient;
|
||||
};
|
||||
|
||||
|
||||
|
@ -487,7 +489,7 @@ void SIOClientImpl::handshakeResponse(HttpClient* /*sender*/, HttpResponse *resp
|
|||
std::string sid = "";
|
||||
int heartbeat = 0, timeout = 0;
|
||||
|
||||
if (res.at(res.size() - 1) == '}') {
|
||||
if (res.find("}") != std::string::npos) {
|
||||
|
||||
CCLOGINFO("SIOClientImpl::handshake() Socket.IO 1.x detected");
|
||||
_version = SocketIOPacket::SocketIOVersion::V10x;
|
||||
|
@ -691,23 +693,32 @@ void SIOClientImpl::heartbeat(float /*dt*/)
|
|||
}
|
||||
|
||||
|
||||
void SIOClientImpl::send(const std::string& endpoint, const std::string& s)
|
||||
void SIOClientImpl::send(const std::string& endpoint, const std::vector<std::string>& s)
|
||||
{
|
||||
switch (_version) {
|
||||
case SocketIOPacket::SocketIOVersion::V09x:
|
||||
{
|
||||
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("message", _version);
|
||||
packet->setEndpoint(endpoint);
|
||||
for(auto &i : s)
|
||||
{
|
||||
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("message", _version);
|
||||
packet->setEndpoint(endpoint);
|
||||
packet->addData(s);
|
||||
this->send(packet);
|
||||
break;
|
||||
}
|
||||
case SocketIOPacket::SocketIOVersion::V10x:
|
||||
{
|
||||
this->emit(endpoint, "message", s);
|
||||
break;
|
||||
packet->addData(i);
|
||||
}
|
||||
this->send(packet);
|
||||
break;
|
||||
}
|
||||
case SocketIOPacket::SocketIOVersion::V10x:
|
||||
{
|
||||
this->emit(endpoint, "message", s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SIOClientImpl::send(const std::string& endpoint, const std::string& s)
|
||||
{
|
||||
std::vector<std::string> t{s};
|
||||
send(endpoint, t);
|
||||
}
|
||||
|
||||
void SIOClientImpl::send(SocketIOPacket *packet)
|
||||
|
@ -732,6 +743,18 @@ void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventna
|
|||
this->send(packet);
|
||||
}
|
||||
|
||||
void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args)
|
||||
{
|
||||
CCLOGINFO("Emitting event \"%s\"", eventname.c_str());
|
||||
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("event", _version);
|
||||
packet->setEndpoint(endpoint == "/" ? "" : endpoint);
|
||||
packet->setEvent(eventname);
|
||||
for (auto &arg : args) {
|
||||
packet->addData(arg);
|
||||
}
|
||||
this->send(packet);
|
||||
}
|
||||
|
||||
void SIOClientImpl::onOpen(WebSocket* /*ws*/)
|
||||
{
|
||||
_connected = true;
|
||||
|
@ -1021,7 +1044,7 @@ SIOClient::SIOClient(const std::string& path, SIOClientImpl* impl, SocketIO::SIO
|
|||
|
||||
SIOClient::~SIOClient()
|
||||
{
|
||||
if (_connected)
|
||||
if (isConnected())
|
||||
{
|
||||
_socket->disconnectFromEndpoint(_path);
|
||||
}
|
||||
|
@ -1033,16 +1056,24 @@ void SIOClient::onOpen()
|
|||
{
|
||||
_socket->connectToEndpoint(_path);
|
||||
}
|
||||
|
||||
setConnected(true);
|
||||
}
|
||||
|
||||
void SIOClient::onConnect()
|
||||
{
|
||||
_connected = true;
|
||||
setConnected(true);
|
||||
}
|
||||
|
||||
void SIOClient::send(const std::string& s)
|
||||
{
|
||||
if (_connected)
|
||||
std::vector<std::string> t{s};
|
||||
send(t);
|
||||
}
|
||||
|
||||
void SIOClient::send(const std::vector<std::string>& s)
|
||||
{
|
||||
if (isConnected())
|
||||
{
|
||||
_socket->send(_path, s);
|
||||
}
|
||||
|
@ -1055,7 +1086,7 @@ void SIOClient::send(const std::string& s)
|
|||
|
||||
void SIOClient::emit(const std::string& eventname, const std::string& args)
|
||||
{
|
||||
if(_connected)
|
||||
if(isConnected())
|
||||
{
|
||||
_socket->emit(_path, eventname, args);
|
||||
}
|
||||
|
@ -1066,10 +1097,23 @@ void SIOClient::emit(const std::string& eventname, const std::string& args)
|
|||
|
||||
}
|
||||
|
||||
void SIOClient::emit(const std::string& eventname, const std::vector<std::string>& args)
|
||||
{
|
||||
if (isConnected())
|
||||
{
|
||||
_socket->emit(_path, eventname, args);
|
||||
}
|
||||
else
|
||||
{
|
||||
_delegate->onError(this, "Client not yet connected");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void SIOClient::disconnect()
|
||||
{
|
||||
_connected = false;
|
||||
|
||||
setConnected(false);
|
||||
_socket->disconnectFromEndpoint(_path);
|
||||
|
||||
this->release();
|
||||
|
@ -1077,13 +1121,22 @@ void SIOClient::disconnect()
|
|||
|
||||
void SIOClient::socketClosed()
|
||||
{
|
||||
_connected = false;
|
||||
|
||||
setConnected(false);
|
||||
_delegate->onClose(this);
|
||||
|
||||
this->release();
|
||||
}
|
||||
|
||||
bool SIOClient::isConnected() const
|
||||
{
|
||||
return _socket && _socket->_connected && _connected;
|
||||
}
|
||||
|
||||
void SIOClient::setConnected(bool connected)
|
||||
{
|
||||
_connected = connected;
|
||||
}
|
||||
|
||||
void SIOClient::on(const std::string& eventName, SIOEvent e)
|
||||
{
|
||||
_eventRegistry[eventName] = e;
|
||||
|
@ -1196,7 +1249,7 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
|
|||
return newC;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
|
|
@ -62,6 +62,7 @@ in the onClose method the pointer should be set to NULL or used to connect to a
|
|||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include "platform/CCPlatformMacros.h"
|
||||
#include "base/CCMap.h"
|
||||
|
||||
|
@ -223,6 +224,9 @@ private:
|
|||
|
||||
friend class SIOClientImpl;
|
||||
|
||||
bool isConnected() const;
|
||||
void setConnected(bool);
|
||||
|
||||
/**
|
||||
* Constructor of SIOClient class.
|
||||
*
|
||||
|
@ -237,7 +241,6 @@ private:
|
|||
* Destructor of SIOClient class.
|
||||
*/
|
||||
virtual ~SIOClient();
|
||||
|
||||
public:
|
||||
/**
|
||||
* Get the delegate for the client
|
||||
|
@ -255,12 +258,18 @@ public:
|
|||
* @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).
|
||||
|
|
|
@ -225,19 +225,21 @@ bool js_cocos2dx_SocketIO_send(JSContext* cx, uint32_t argc, jsval* vp)
|
|||
SIOClient* cobj = (SIOClient *)(proxy ? proxy->ptr : NULL);
|
||||
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
|
||||
|
||||
if (argc == 1)
|
||||
if (argc >= 1)
|
||||
{
|
||||
std::vector<std::string> eventArgs;
|
||||
std::string payload;
|
||||
|
||||
do
|
||||
for(int idx = 0; idx < argc; idx++)
|
||||
{
|
||||
bool ok = jsval_to_std_string(cx, args.get(0), &payload);
|
||||
bool ok = jsval_to_std_string(cx, args.get(idx), &payload);
|
||||
JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
|
||||
} while (0);
|
||||
eventArgs.push_back(payload);
|
||||
}
|
||||
|
||||
CCLOG("JSB SocketIO send mesage: %s", payload.c_str());
|
||||
|
||||
cobj->send(payload);
|
||||
cobj->send(eventArgs);
|
||||
return true;
|
||||
|
||||
}
|
||||
|
@ -255,24 +257,27 @@ bool js_cocos2dx_SocketIO_emit(JSContext* cx, uint32_t argc, jsval* vp)
|
|||
SIOClient* cobj = (SIOClient *)(proxy ? proxy->ptr : NULL);
|
||||
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
|
||||
|
||||
if (argc == 2)
|
||||
if (argc >= 2)
|
||||
{
|
||||
std::string eventName;
|
||||
|
||||
do
|
||||
{
|
||||
bool ok = jsval_to_std_string(cx, args.get(0), &eventName);
|
||||
JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
|
||||
} while (0);
|
||||
|
||||
std::vector<std::string> eventArgs;
|
||||
std::string payload;
|
||||
do {
|
||||
bool ok = jsval_to_std_string(cx, args.get(1), &payload);
|
||||
for(int idx = 1; idx < argc; idx ++) {
|
||||
bool ok = jsval_to_std_string(cx, args.get(idx), &payload);
|
||||
JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
|
||||
} while (0);
|
||||
eventArgs.push_back(payload);
|
||||
}
|
||||
|
||||
CCLOG("JSB SocketIO emit event '%s' with payload: %s", eventName.c_str(), payload.c_str());
|
||||
|
||||
cobj->emit(eventName, payload);
|
||||
cobj->emit(eventName, eventArgs);
|
||||
return true;
|
||||
}
|
||||
JS_ReportError(cx, "JSB SocketIO.emit: Wrong number of arguments");
|
||||
|
|
Loading…
Reference in New Issue