Interacting with NMControl from Javascript?

Post Reply
biolizard89
Posts: 2001
Joined: Tue Jun 05, 2012 6:25 am
os: linux

Interacting with NMControl from Javascript?

Post by biolizard89 »

I'm trying to interact with NMControl's RPC server from Javascript, but I'm not having much luck. Based on code found via Google (i.e. probably crappy code), I have the following:

Code: Select all

	var http = new XMLHttpRequest();
	var url = "http://127.0.0.1:9000/";
	
	var params = {method: "data", params: ["getValue", "d/dot-bit"], id: 1};
	
	http.open("POST", url, false);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "text/x-json");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.send(JSON.stringify(params));
Unfortunately, when I execute this code, the XMLHttpRequest never returns from send(), and NMControl says this:

Code: Select all

ValueError: No JSON object could be decoded
Using Wireshark, I don't see any HTTP headers when NMControl talks to itself, whereas my Javascript code is sending HTTP headers. I assume this is related to the issue. Does NMControl not use HTTP requests? Is there a preferred way to speak this protocol in Javascript?

Thanks.
Jeremy Rand, Lead Namecoin Application Engineer
NameID: id/jeremy
DyName: Dynamic DNS update client for .bit domains.

Donations: BTC 1EcUWRa9H6ZuWPkF3BDj6k4k1vCgv41ab8 ; NMC NFqbaS7ReiQ9MBmsowwcDSmp4iDznjmEh5

domob
Posts: 1129
Joined: Mon Jun 24, 2013 11:27 am
Contact:

Re: Interacting with NMControl from Javascript?

Post by domob »

biolizard89 wrote: Using Wireshark, I don't see any HTTP headers when NMControl talks to itself, whereas my Javascript code is sending HTTP headers. I assume this is related to the issue. Does NMControl not use HTTP requests? Is there a preferred way to speak this protocol in Javascript?
Just found this question by chance, is it still of interest to you? I presume you were interested in it because of your work on the TLS integration (which seems solved already)? But to answer your question nevertheless, NMControl does indeed not communicate with HTTP but instead a raw socket connection. I'm not sure whether you can even do that with (standard and sand-boxed) JavaScript (maybe some HTML5 API provides socket communication - Websockets?), but from the context of a Mozilla add-on, I'm using NetUtils.jsm to do that nicely.
BTC: 1domobKsPZ5cWk2kXssD8p8ES1qffGUCm | NMC: NCdomobcmcmVdxC5yxMitojQ4tvAtv99pY
BM-GtQnWM3vcdorfqpKXsmfHQ4rVYPG5pKS
Use your Namecoin identity as OpenID: https://nameid.org/

biolizard89
Posts: 2001
Joined: Tue Jun 05, 2012 6:25 am
os: linux

Re: Interacting with NMControl from Javascript?

Post by biolizard89 »

domob wrote:
biolizard89 wrote: Using Wireshark, I don't see any HTTP headers when NMControl talks to itself, whereas my Javascript code is sending HTTP headers. I assume this is related to the issue. Does NMControl not use HTTP requests? Is there a preferred way to speak this protocol in Javascript?
Just found this question by chance, is it still of interest to you? I presume you were interested in it because of your work on the TLS integration (which seems solved already)? But to answer your question nevertheless, NMControl does indeed not communicate with HTTP but instead a raw socket connection. I'm not sure whether you can even do that with (standard and sand-boxed) JavaScript (maybe some HTML5 API provides socket communication - Websockets?), but from the context of a Mozilla add-on, I'm using NetUtils.jsm to do that nicely.
I did indeed figure it out, the code I'm using now is on GitHub.
Jeremy Rand, Lead Namecoin Application Engineer
NameID: id/jeremy
DyName: Dynamic DNS update client for .bit domains.

Donations: BTC 1EcUWRa9H6ZuWPkF3BDj6k4k1vCgv41ab8 ; NMC NFqbaS7ReiQ9MBmsowwcDSmp4iDznjmEh5

Post Reply