axmol/cocos/scripting/js-bindings/manual/network/jsb_socketio.cpp

422 lines
14 KiB
C++
Raw Normal View History

2015-05-05 10:50:19 +08:00
/*
* Created by Chris Hannon 2014 http://www.channon.us
* Copyright (c) 2014 Chukong Technologies Inc.
*
* 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.
*/
#include "jsb_socketio.h"
#include "jsb_helper.h"
#include "cocos2d.h"
#include "network/WebSocket.h"
#include "network/SocketIO.h"
#include "spidermonkey_specifics.h"
#include "ScriptingCore.h"
#include "cocos2d_specifics.hpp"
using namespace cocos2d::network;
//c++11 map to callbacks
typedef std::unordered_map<std::string, std::shared_ptr<JSFunctionWrapper>> JSB_SIOCallbackRegistry;
2015-05-05 10:50:19 +08:00
class JSB_SocketIODelegate : public SocketIO::SIODelegate
{
2015-05-05 10:50:19 +08:00
public:
JSB_SocketIODelegate()
{
2015-05-05 10:50:19 +08:00
std::string s = "default";
_eventRegistry[s] = nullptr;
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
_JSDelegate.construct(cx);
}
~JSB_SocketIODelegate()
{
_JSDelegate.destroyIfConstructed();
2015-05-05 10:50:19 +08:00
}
virtual void onConnect(SIOClient* client)
{
2015-05-05 10:50:19 +08:00
CCLOG("JSB SocketIO::SIODelegate->onConnect method called from native");
this->fireEventToScript(client, "connect", "");
}
virtual void onMessage(SIOClient* client, const std::string& data)
{
2015-05-05 10:50:19 +08:00
CCLOG("JSB SocketIO::SIODelegate->onMessage method called from native with data: %s", data.c_str());
this->fireEventToScript(client, "message", data);
}
virtual void onClose(SIOClient* client)
{
2015-05-05 10:50:19 +08:00
CCLOG("JSB SocketIO::SIODelegate->onClose method called from native");
this->fireEventToScript(client, "disconnect", "");
}
virtual void onError(SIOClient* client, const std::string& data)
{
2015-05-05 10:50:19 +08:00
CCLOG("JSB SocketIO::SIODelegate->onError method called from native with data: %s", data.c_str());
this->fireEventToScript(client, "error", data);
}
virtual void fireEventToScript(SIOClient* client, const std::string& eventName, const std::string& data)
{
2015-05-05 10:50:19 +08:00
CCLOG("JSB SocketIO::SIODelegate->fireEventToScript method called from native with name '%s' data: %s", eventName.c_str(), data.c_str());
js_proxy_t * p = jsb_get_native_proxy(client);
if (!p) return;
JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
jsval args;
if(data == "")
{
2015-05-05 10:50:19 +08:00
args = JSVAL_NULL;
} else
{
2015-05-05 10:50:19 +08:00
args = std_string_to_jsval(cx, data);
}
JSB_SIOCallbackRegistry::iterator it = _eventRegistry.find(eventName);
2015-05-05 10:50:19 +08:00
if(it != _eventRegistry.end())
{
std::shared_ptr<JSFunctionWrapper> callback = it->second;
if (callback != nullptr)
{
JS::RootedValue rval(cx);
callback->invoke(1, &args, &rval);
}
2015-05-05 10:50:19 +08:00
}
}
void setJSDelegate(JS::HandleObject pJSDelegate)
{
_JSDelegate.ref() = pJSDelegate;
2015-05-05 10:50:19 +08:00
}
void addEvent(const std::string& eventName, std::shared_ptr<JSFunctionWrapper> callback)
{
_eventRegistry[eventName] = callback;
2015-05-05 10:50:19 +08:00
}
private:
mozilla::Maybe<JS::PersistentRootedObject> _JSDelegate;
JSB_SIOCallbackRegistry _eventRegistry;
2015-05-05 10:50:19 +08:00
};
JSClass *js_cocos2dx_socketio_class;
JSObject *js_cocos2dx_socketio_prototype;
void js_cocos2dx_SocketIO_finalize(JSFreeOp *fop, JSObject *obj)
{
2015-05-05 10:50:19 +08:00
CCLOG("jsbindings: finalizing JS object %p (SocketIO)", obj);
}
bool js_cocos2dx_SocketIO_constructor(JSContext *cx, uint32_t argc, jsval *vp)
{
JS_ReportError(cx, "SocketIO isn't meant to be instantiated, use SocketIO.connect() instead");
return false;
}
bool js_cocos2dx_SocketIO_connect(JSContext* cx, uint32_t argc, jsval* vp)
{
2015-05-05 10:50:19 +08:00
CCLOG("JSB SocketIO.connect method called");
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if (argc == 1 || argc == 2)
{
2015-05-05 10:50:19 +08:00
std::string url;
do
{
2015-05-05 10:50:19 +08:00
bool ok = jsval_to_std_string(cx, args.get(0), &url);
JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
} while (0);
JSB_SocketIODelegate* siodelegate = new JSB_SocketIODelegate();
CCLOG("Calling native SocketIO.connect method");
SIOClient* ret = SocketIO::connect(url, *siodelegate);
2015-05-05 10:50:19 +08:00
jsval jsret;
do
{
if (ret)
{
2015-05-05 10:50:19 +08:00
// link the native object with the javascript object
js_proxy_t *p;
HASH_FIND_PTR(_native_js_global_ht, &ret, p);
if(!p)
{
2015-05-05 10:50:19 +08:00
//previous connection not found, create a new one
JS::RootedObject proto(cx, js_cocos2dx_socketio_prototype);
JSObject *obj = JS_NewObject(cx, js_cocos2dx_socketio_class, proto, JS::NullPtr());
2015-05-05 10:50:19 +08:00
p = jsb_new_proxy(ret, obj);
JS::RootedObject jsdelegate(cx, p->obj);
siodelegate->setJSDelegate(jsdelegate);
2015-05-05 10:50:19 +08:00
}
jsret = OBJECT_TO_JSVAL(p->obj);
}
else
{
2015-05-05 10:50:19 +08:00
jsret = JSVAL_NULL;
}
} while(0);
args.rval().set(jsret);
return true;
}
JS_ReportError(cx, "JSB SocketIO.connect: Wrong number of arguments");
return false;
}
bool js_cocos2dx_SocketIO_send(JSContext* cx, uint32_t argc, jsval* vp)
{
2015-05-05 10:50:19 +08:00
CCLOG("JSB SocketIO.send method called");
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
2015-05-05 10:50:19 +08:00
js_proxy_t *proxy = jsb_get_js_proxy(obj);
SIOClient* cobj = (SIOClient *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
if (argc == 1)
{
2015-05-05 10:50:19 +08:00
std::string payload;
do
{
2015-05-05 10:50:19 +08:00
bool ok = jsval_to_std_string(cx, args.get(0), &payload);
JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
} while (0);
CCLOG("JSB SocketIO send mesage: %s", payload.c_str());
cobj->send(payload);
return true;
}
JS_ReportError(cx, "Wrong number of arguments");
return false;
}
bool js_cocos2dx_SocketIO_emit(JSContext* cx, uint32_t argc, jsval* vp)
{
2015-05-05 10:50:19 +08:00
CCLOG("JSB SocketIO.emit method called");
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
2015-05-05 10:50:19 +08:00
js_proxy_t *proxy = jsb_get_js_proxy(obj);
SIOClient* cobj = (SIOClient *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
if (argc == 2)
{
2015-05-05 10:50:19 +08:00
std::string eventName;
do
{
2015-05-05 10:50:19 +08:00
bool ok = jsval_to_std_string(cx, args.get(0), &eventName);
JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
} while (0);
std::string payload;
do {
bool ok = jsval_to_std_string(cx, args.get(1), &payload);
JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
} while (0);
CCLOG("JSB SocketIO emit event '%s' with payload: %s", eventName.c_str(), payload.c_str());
cobj->emit(eventName, payload);
return true;
}
JS_ReportError(cx, "JSB SocketIO.emit: Wrong number of arguments");
return false;
}
bool js_cocos2dx_SocketIO_disconnect(JSContext* cx, uint32_t argc, jsval* vp)
{
2015-05-05 10:50:19 +08:00
CCLOG("JSB SocketIO.disconnect method called");
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
2015-05-05 10:50:19 +08:00
js_proxy_t *proxy = jsb_get_js_proxy(obj);
SIOClient* cobj = (SIOClient *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
if(argc == 0)
{
2015-05-05 10:50:19 +08:00
cobj->disconnect();
args.rval().setUndefined();
return true;
}
JS_ReportError(cx, "JSB SocketIO.disconnect: Wrong number of arguments");
return false;
}
bool js_cocos2dx_SocketIO_close(JSContext* cx, uint32_t argc, jsval* vp)
{
2015-05-05 10:50:19 +08:00
CCLOG("JSB SocketIO.close method called");
if(argc == 0)
{
2015-05-05 10:50:19 +08:00
//This method was previously implemented to take care of the HTTPClient instance not being destroyed properly
//SocketIO::close();
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
args.rval().setUndefined();
return true;
}
JS_ReportError(cx, "JSB SocketIO.close: Wrong number of arguments");
return false;
}
static bool _js_set_SIOClient_tag(JSContext* cx, uint32_t argc, jsval* vp)
{
CCLOG("JSB SocketIO.setTag method called");
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JSObject* jsobj = args.thisv().toObjectOrNull();
js_proxy_t *proxy = jsb_get_js_proxy(jsobj);
SIOClient* cobj = (SIOClient *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
if (cobj)
{
2015-05-05 10:50:19 +08:00
std::string out = "";
jsval_to_std_string(cx, args.get(0), &out);
cobj->setTag(out.c_str());
return true;
} else
{
2015-05-05 10:50:19 +08:00
JS_ReportError(cx, "Error: SocketIO instance is invalid.");
return false;
}
}
static bool _js_get_SIOClient_tag(JSContext* cx, uint32_t argc, jsval* vp)
{
CCLOG("JSB SocketIO.getTag method called");
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JSObject* jsobj = args.thisv().toObjectOrNull();
js_proxy_t *proxy = jsb_get_js_proxy(jsobj);
SIOClient* cobj = (SIOClient *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
if (cobj)
{
2015-05-05 10:50:19 +08:00
args.rval().set(std_string_to_jsval(cx, cobj->getTag()));
return true;
} else
{
2015-05-05 10:50:19 +08:00
JS_ReportError(cx, "Error: SocketIO instance is invalid.");
return false;
}
}
bool js_cocos2dx_SocketIO_on(JSContext* cx, uint32_t argc, jsval* vp)
{
2015-05-05 10:50:19 +08:00
CCLOG("JSB SocketIO.on method called");
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
2015-05-05 10:50:19 +08:00
js_proxy_t *proxy = jsb_get_js_proxy(obj);
SIOClient* cobj = (SIOClient *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "Invalid Native Object");
if(argc == 2)
{
2015-05-05 10:50:19 +08:00
std::string eventName;
do
{
2015-05-05 10:50:19 +08:00
bool ok = jsval_to_std_string(cx, args.get(0), &eventName);
JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
} while (0);
CCLOG("JSB SocketIO eventName to: '%s'", eventName.c_str());
std::shared_ptr<JSFunctionWrapper> callback(new JSFunctionWrapper(cx, obj, args.get(1)));
2015-05-05 10:50:19 +08:00
((JSB_SocketIODelegate *)cobj->getDelegate())->addEvent(eventName, callback);
2015-05-05 10:50:19 +08:00
args.rval().set(OBJECT_TO_JSVAL(proxy->obj));
JS_SetReservedSlot(proxy->obj, 0, args.get(1));
return true;
}
JS_ReportError(cx, "JSB SocketIO.close: Wrong number of arguments");
return false;
}
void register_jsb_socketio(JSContext *cx, JS::HandleObject global)
{
2015-05-05 10:50:19 +08:00
js_cocos2dx_socketio_class = (JSClass *)calloc(1, sizeof(JSClass));
js_cocos2dx_socketio_class->name = "SocketIO";
js_cocos2dx_socketio_class->addProperty = JS_PropertyStub;
js_cocos2dx_socketio_class->delProperty = JS_DeletePropertyStub;
js_cocos2dx_socketio_class->getProperty = JS_PropertyStub;
js_cocos2dx_socketio_class->setProperty = JS_StrictPropertyStub;
js_cocos2dx_socketio_class->enumerate = JS_EnumerateStub;
js_cocos2dx_socketio_class->resolve = JS_ResolveStub;
js_cocos2dx_socketio_class->convert = JS_ConvertStub;
js_cocos2dx_socketio_class->finalize = js_cocos2dx_SocketIO_finalize;
js_cocos2dx_socketio_class->flags = JSCLASS_HAS_RESERVED_SLOTS(2);
static JSPropertySpec properties[] =
{
2015-05-05 10:50:19 +08:00
JS_PSGS("tag", _js_get_SIOClient_tag, _js_set_SIOClient_tag, JSPROP_PERMANENT | JSPROP_ENUMERATE),
JS_PS_END
};
static JSFunctionSpec funcs[] =
{
2015-05-05 10:50:19 +08:00
JS_FN("send", js_cocos2dx_SocketIO_send, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE),
JS_FN("emit", js_cocos2dx_SocketIO_emit, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE),
JS_FN("disconnect", js_cocos2dx_SocketIO_disconnect, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
JS_FN("on", js_cocos2dx_SocketIO_on, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE),
JS_FS_END
};
static JSFunctionSpec st_funcs[] =
{
2015-05-05 10:50:19 +08:00
JS_FN("connect", js_cocos2dx_SocketIO_connect, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE),
JS_FN("close", js_cocos2dx_SocketIO_close, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE),
JS_FS_END
};
js_cocos2dx_socketio_prototype = JS_InitClass(
cx, global,
JS::NullPtr(),
js_cocos2dx_socketio_class,
js_cocos2dx_SocketIO_constructor, 0, // constructor
nullptr,
funcs,
properties,
2015-05-05 10:50:19 +08:00
st_funcs);
anonEvaluate(cx, global, "(function () { return SocketIO; })()").toObjectOrNull();
2015-05-05 10:50:19 +08:00
}