Monday 27 August 2012

jsonSpew

Recently I've been throwing a lot of JSON down websockets for display using processing.js. To help in this I wrote a little Go script.  The script serves files over http, as well as relaying any JSON recevied down a websocket. The script can monitor data files and serves tcp to receive JSON updates. All the files are here. It was a bit of a stream of consciousness, so the structure could be better, and half the code should really be put in seperate packages, but hey.

go run jsonSpew.go -httpPort=8081 -fileList="/test.html:/processing.js:/jquery.min.js" -watchList="test.json" -tcpPort=12345

2012/08/27 12:02:39.482810 jsonSpew 1.0.a
2012/08/27 12:02:39.482989 Registered /test.html
2012/08/27 12:02:39.483003 Registered /processing.js
2012/08/27 12:02:39.483014 Registered /jquery.min.js
2012/08/27 12:02:39.483037 Watching:test.json
2012/08/27 12:02:39.484011 Serving tcp on [127.0.1.1] 12345
2012/08/27 12:02:39.484050 Serving http on [127.0.1.1]:8081

So now we have a process sitting on 8081 serving up test.html,processing.js, and jquery.min.js. The process is also serving up tcp connections on 12345, and watching test.json.

Now, hitting 127.0.1.1:8081/test.html will give us a a view onto the JSON data available. Test.html simply iterates over the JSON and display the values using processing.js's draw loop.


Now we can send some updates.

netcat 127.0.1.1 12345 < test2.json



netcat 127.0.1.1 12345 < test3.json


touch test.json


The flexibility of Go's standard library meant that I could knock this useful little utility up in an afternoon.