PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/emitter/dji/dji.js

http://github.com/square/cube
JavaScript | 40 lines | 35 code | 3 blank | 2 comment | 1 complexity | a57069d6e496eb5432d926bee1a7310f MD5 | raw file
Possible License(s): Apache-2.0
  1. var util = require("util"),
  2. emitter = require("../../../lib/cube/server/emitter"),
  3. options = require("./dji-config");
  4. // Connect to websocket.
  5. util.log("starting websocket client");
  6. var client = emitter().open(options["http-host"], options["http-port"]);
  7. // Emit stock data.
  8. readline(function(line, i) {
  9. if (i) {
  10. var fields = line.split(",");
  11. client.send({
  12. type: "stock",
  13. time: new Date(fields[0]),
  14. data: {
  15. open: +fields[1],
  16. high: +fields[2],
  17. low: +fields[3],
  18. close: +fields[4],
  19. volume: +fields[5]
  20. }
  21. });
  22. }
  23. });
  24. function readline(callback) {
  25. var stdin = process.openStdin(), line = "", i = -1;
  26. stdin.setEncoding("utf8");
  27. stdin.on("data", function(string) {
  28. var lines = string.split("\n");
  29. lines[0] = line + lines[0];
  30. line = lines.pop();
  31. lines.forEach(function(line) { callback(line, ++i); });
  32. });
  33. stdin.on("end", function() {
  34. util.log("stopping websocket client");
  35. client.close();
  36. });
  37. }