ofxJSONRPC - cannot load images from JSONRPC registered method

I have js app with thumbnails from 0 to 9. When I press thumbnail it sends integer from 0 to 9 to oF app.

Server registers method which loads a image based on that integer:

(...)

server->registerMethod("set-pic-id",
		"Set a picID",
		this,
		&ofApp::setPicture);

(...)

this works fine:

void ofApp::keyPressed(int key)
{
	loadPicture(ofToString(key-48));
}

this doesn’t (image is filled with white):

void ofApp::setPicture(ofx::JSONRPC::MethodArgs& args)
{
	loadPicture(args.params.asString());
	ofLogVerbose("ofApp::setVideo") << args.params.asString();
}

this loads image:

void ofApp::loadPicture(string input)
{
	string bgPath;
	bgPath="images/bg/"+ input +".jpg";
	bg.loadImage(bgPath);
	cout << "image loaded..."<< endl;
}

main.js:

(...)   
 $('.thumb').on('click', function() {
        var $this = $(this);
        JSONRPCClient.call('set-pic-id',
            $(this).attr('id'),
            function(result) {},
            function(error) {
                addError(error);
            });
    });
(...)

html:

(...)
<img class=thumb id=1 />
<img class=thumb id=2 />
(...)

any suggestions?

1 Like

It is likely because the callback from jsonrpc is happening outside the main thread. Opengl can’t upload textures outside the main thread. I would load the image pixels without the texture then call update on the image in the ofApp update method.

Thanks, I’ll keep it in mind.
For now it’s a workaround:

void ofApp::update()
{
	if (currentVideoID != selectedVideoID)
	{
		loadPicture(selectedVideoID);
		currentVideoID = selectedVideoID;
	}
}

registered method:

void ofApp::setVideo(ofx::JSONRPC::MethodArgs& args)
{
	// Set the user text.
	// loadPicture(args.params.asString());
	selectedVideoID = args.params.asString();
	ofLogVerbose("ofApp::setVideo") << args.params.asString();
}
1 Like

Yep, that should totally work. Nice one!