Esempio n. 1
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;
}
Esempio n. 3
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;

}
Esempio n. 4
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;
}