Example #1
0
bool js_cocos2dx_SocketIO_send(JSContext* cx, uint32_t argc, jsval* vp){
    CCLOG("JSB SocketIO.send method called");

    jsval *argv = JS_ARGV(cx, vp);
    JSObject *obj = JS_THIS_OBJECT(cx, vp);
    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) {

        std::string payload;
        
        do {
            bool ok = jsval_to_std_string(cx, argv[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;

}
Example #2
0
void SIOClientImpl::handshakeResponse(HttpClient *sender, HttpResponse *response)
{
	log("SIOClientImpl::handshakeResponse() called");

	if (0 != strlen(response->getHttpRequest()->getTag())) 
    {
        log("%s completed", response->getHttpRequest()->getTag());
    }

	int statusCode = response->getResponseCode();
    char statusString[64] = {};
    sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
	log("response code: %d", statusCode);

	if (!response->isSucceed()) 
    {
        log("SIOClientImpl::handshake() failed");
        log("error buffer: %s", response->getErrorBuffer());

		DictElement* el = NULL;

		CCDICT_FOREACH(_clients, el) {

			SIOClient* c = static_cast<SIOClient*>(el->getObject());
			
			c->getDelegate()->onError(c, response->getErrorBuffer());

		}
bool js_cocos2dx_SocketIO_on(JSContext* cx, uint32_t argc, jsval* vp)
{
    CCLOG("JSB SocketIO.on method called");

    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
    JSObject *obj = JS_THIS_OBJECT(cx, vp);
    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)
    {
        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);

        CCLOG("JSB SocketIO eventName to: '%s'", eventName.c_str());
        
        std::shared_ptr<JSFunctionWrapper> callback(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(1)));

        ((JSB_SocketIODelegate *)cobj->getDelegate())->addEvent(eventName, callback);

        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;
}
bool js_cocos2dx_SocketIO_emit(JSContext* cx, uint32_t argc, jsval* vp)
{
    CCLOG("JSB SocketIO.emit method called");

    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
    JSObject *obj = JS_THIS_OBJECT(cx, vp);
    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)
    {
        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::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;
}
Example #5
0
static bool _js_get_SIOClient_tag(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp)
{
    CCLOG("JSB SocketIO.getTag method called");
    JSObject* jsobj = obj.get();
    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) {
        vp.set(std_string_to_jsval(cx, cobj->getTag()));
        return true;
    } else {
        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)
    {
        args.rval().set(std_string_to_jsval(cx, cobj->getTag()));
        return true;
    } else
    {
        JS_ReportError(cx, "Error: SocketIO instance is invalid.");
        return false;
    }
}
Example #7
0
bool js_cocos2dx_SocketIO_disconnect(JSContext* cx, uint32_t argc, jsval* vp){
    CCLOG("JSB SocketIO.disconnect method called");

    jsval *argv = JS_ARGV(cx, vp);
    JSObject *obj = JS_THIS_OBJECT(cx, vp);
    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) {

        cobj->disconnect();
        JS_SET_RVAL(cx, vp, JSVAL_NULL);
        return true;
    }

    JS_ReportError(cx, "JSB SocketIO.disconnect: Wrong number of arguments");
    return false;

}
Example #8
0
bool js_cocos2dx_SocketIO_on(JSContext* cx, uint32_t argc, jsval* vp){
    CCLOG("JSB SocketIO.on method called");

    jsval *argv = JS_ARGV(cx, vp);
    JSObject *obj = JS_THIS_OBJECT(cx, vp);
    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) {

        std::string eventName;
        
        do {
            bool ok = jsval_to_std_string(cx, argv[0], &eventName);
            JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
        } while (0);

        CCLOG("JSB SocketIO eventName to: '%s'", eventName.c_str());

        JSB_SIOEvent tmpCobj; 
        tmpCobj.setJSCallbackFunc(argv[1]);

        ((JSB_SocketIODelegate *)cobj->getDelegate())->addEvent(eventName, tmpCobj);

        JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(proxy->obj));

        JS_SetReservedSlot(proxy->obj, 0, argv[1]);
            
        return true;
    }

    JS_ReportError(cx, "JSB SocketIO.close: Wrong number of arguments");
    return false;

}
Example #9
0
void SIOClientImpl::onMessage(WebSocket* ws, const WebSocket::Data& data)
{
	log("SIOClientImpl::onMessage received: %s", data.bytes);

	int control = atoi(&data.bytes[0]);

	std::string payload, msgid, endpoint, s_data, eventname;
	payload = data.bytes;

	size_t pos, pos2;

	pos = payload.find(":");
	if(pos != std::string::npos ) {
		payload.erase(0, pos+1);
	}

	pos = payload.find(":");
	if(pos != std::string::npos ) {
		msgid = atoi(payload.substr(0, pos+1).c_str());	
	}
	payload.erase(0, pos+1);

	pos = payload.find(":");
	if(pos != std::string::npos)
    {
		endpoint = payload.substr(0, pos);
		payload.erase(0, pos+1);
	}
    else
    {
		endpoint = payload;
	}

	if (endpoint == "") endpoint = "/";


	s_data = payload;
	SIOClient *c = NULL;
	c = getClient(endpoint);
	if (c == NULL) log("SIOClientImpl::onMessage client lookup returned NULL");

	switch(control)
    {
		case 0: 
			log("Received Disconnect Signal for Endpoint: %s\n", endpoint.c_str());
			if(c) c->receivedDisconnect();
			disconnectFromEndpoint(endpoint);
			break;
		case 1: 
			log("Connected to endpoint: %s \n",endpoint.c_str());
			if(c) c->onConnect();
			break;
		case 2: 
			log("Heartbeat received\n");
			break;
		case 3:
			log("Message received: %s \n", s_data.c_str());
			if(c) c->getDelegate()->onMessage(c, s_data);
			break;
		case 4:
			log("JSON Message Received: %s \n", s_data.c_str());
			if(c) c->getDelegate()->onMessage(c, s_data);
			break;
		case 5:
			log("Event Received with data: %s \n", s_data.c_str());

			if(c)
            {
				eventname = "";
				pos = s_data.find(":");
				pos2 = s_data.find(",");
				if(pos2 > pos)
                {
					s_data = s_data.substr(pos+1, pos2-pos-1);
					std::remove_copy(s_data.begin(), s_data.end(),
						 std::back_inserter(eventname), '"');
				}

				c->fireEvent(eventname, payload);
			}
			
			break;
		case 6:
			log("Message Ack\n");
			break;
		case 7:
			log("Error\n");
			if(c) c->getDelegate()->onError(c, s_data);
			break;
		case 8:
			log("Noop\n");
			break;
	}

	return;
}
Example #10
0
int main(int argc, char* argv[])
{
	//create a c++ Poco logger to use and set its channel to the windows console
	//this is the same logger instance that the library will hook into
	Logger *logger = &(Logger::get("SIOClientLog"));

#ifdef _WIN64
   //define something for Windows (64-bit)
	logger->setChannel(new Poco::WindowsConsoleChannel());
#elif _WIN32
   //define something for Windows (32-bit)
	logger->setChannel(new Poco::WindowsConsoleChannel());
#elif __APPLE__
    #if TARGET_IPHONE_SIMULATOR
         // iOS Simulator
    #elif TARGET_OS_IPHONE
        // iOS device
    #elif TARGET_OS_MAC
        // Other kinds of Mac OS
    #else
        // Unsupported platform
    #endif
#elif __linux
    // linux
	logger->setChannel(new Poco::ConsoleChannel());
#elif __unix // all unices not caught above
    // Unix
	logger->setChannel(new Poco::ConsoleChannel());
#elif __posix
    // POSIX
	logger->setChannel(new Poco::ConsoleChannel());
#endif

	
	//Establish the socket.io connection
	//JS: var socket = io.connect("localhost:3000")
	SIOClient *sio = SIOClient::connect("http://localhost:3000");

	//Create a target and register object its method onUpdate for the Update event
	//JS: socket.on("Update", function(data) {...});
	TestTarget *target = new TestTarget();
	sio->on("Update", target, callback(&TestTarget::onUpdate));
	
	//setup is now complete, messages and events can be send and received
	logger->information("sio setup complete\n");

	//test the message sending
	logger->information("sending message\n");
	sio->send("Hello Socket.IO");

	//test the event emitter, this will return the same event so let's register a callback too
	sio->on("testevent", target, callback(&TestTarget::ontestevent));
	logger->information("emitting test event\n");
	sio->emit("testevent", "[{\"name\":\"myname\",\"type\":\"mytype\"}]");
	
	//test connecting to an endpoint 'testpoint'
	TestEndpointTarget *target2 = new TestEndpointTarget();
	SIOClient *testpoint = SIOClient::connect("http://localhost:3000/testpoint");
	testpoint->send("Hello Socket.IO Testpoint");
	testpoint->on("Update", target2, callback(&TestEndpointTarget::onUpdate));
	testpoint->on("testevent", target2, callback(&TestEndpointTarget::ontestevent));
	testpoint->emit("testevent", "[{\"name\":\"myname\",\"type\":\"mytype\"}]");

	//wait for user input to move to next section of code
	//socket receiving occurs in another thread and will not be halted
	logger->information("Press ENTER to continue...");
	std::cin.get();

	//test disconnecting a single endpoint, other endpoints stay connected
	testpoint->disconnect();

	//disconnecting the default socket with no endpoint will also disconnect all endpoints
	sio->disconnect();

	logger->information("Press ENTER to quit...");
	std::cin.get();

	return 0;
}