PageRenderTime 103ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/javascript/hi/firefox_socket.js

https://github.com/gutomaia/hi_firefox
JavaScript | 52 lines | 43 code | 9 blank | 0 comment | 0 complexity | d98cf3ec11e1d76a89ed53c20a85d779 MD5 | raw file
  1. function FireFoxSocketConnection (msn) {
  2. this.msn = msn;
  3. this.transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService(Components.interfaces.nsISocketTransportService);
  4. }
  5. FireFoxSocketConnection.prototype.getSocket = function() {}
  6. FireFoxSocketConnection.prototype.connect = function(host, port){
  7. this.transport = this.transportService.createTransport(null, 0, host, port, null);
  8. this.outputStream = this.transport.openOutputStream(0, 0, 0);
  9. this.stream = this.transport.openInputStream(0, 0, 0);
  10. this.inputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
  11. this.inputStream.init(this.stream);
  12. var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance(Components.interfaces.nsIInputStreamPump);
  13. pump.init(this.stream, -1, -1, 0, 0, false);
  14. pump.asyncRead(this, null);
  15. }
  16. FireFoxSocketConnection.prototype.disconnect = function(){
  17. }
  18. FireFoxSocketConnection.prototype.send = function (cmd) {
  19. var suc = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
  20. suc.charset = "utf-8";
  21. var command = suc.ConvertFromUnicode(cmd);
  22. this.outputStream.write(command, command.length);
  23. }
  24. FireFoxSocketConnection.prototype.nextCommand = function () {
  25. return null;
  26. }
  27. FireFoxSocketConnection.prototype.onStartRequest = function (request, context) {
  28. }
  29. FireFoxSocketConnection.prototype.onStopRequest = function (request, context, result) {
  30. }
  31. FireFoxSocketConnection.prototype.onDataAvailable = function (request, context, inputStream, offset, count){
  32. var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance (Components. interfaces. nsIScriptableInputStream);
  33. sis.init(inputStream);
  34. var str = sis.read(count);
  35. var suc = Components. classes ["@mozilla.org/intl/scriptableunicodeconverter"].createInstance (Components. interfaces. nsIScriptableUnicodeConverter);
  36. suc.charset = "utf-8";
  37. var product = suc.ConvertToUnicode(str);
  38. logger.log("<<<" + product);
  39. this.msn.execute(product);
  40. }
  41. FireFoxSocketConnection.prototype.hasMoreCommands = function() {
  42. return false;
  43. }