PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/desktop_extensions/content/mobibar_working.js

https://bitbucket.org/ykim320/kayo
JavaScript | 90 lines | 39 code | 5 blank | 46 comment | 0 complexity | fd1be43ee431d6bd67ad0d0a0f93b137 MD5 | raw file
  1. function MobiBarSearch(event){
  2. var query = document.getElementById("MobiBarQuery").value;
  3. var server = 'localhost';
  4. var port = 12345;
  5. var data = query;
  6. this.transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].
  7. getService(Components.interfaces.nsISocketTransportService);
  8. this.scriptablestream = Components.classes["@mozilla.org/scriptableinputstream;1"].
  9. createInstance(Components.interfaces.nsIScriptableInputStream);
  10. this.pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].
  11. createInstance(Components.interfaces.nsIInputStreamPump);
  12. this.transport = this.transportService.createTransport(null, 0, server, port, null);
  13. this.outstream = this.transport.openOutputStream(1, 0, 0);
  14. this.outputData = query;
  15. //this is where the connection is actually opens.
  16. this.outstream.write(this.outputData, this.outputData.length);
  17. this.outstream.flush();
  18. this.stream = this.transport.openInputStream(0, 0, 0);
  19. this.scriptablestream.init(this.stream);
  20. var dataListener = {
  21. data: "testdata",
  22. onStartRequest: function (request, context) {
  23. //here is the event for connection established
  24. alert("Started");
  25. },
  26. onStopRequest: function (request, context, status) {
  27. // here is the event if connection lost
  28. alert("Stop");
  29. },
  30. onDataAvailable: function (request, context, inputStream, offset, count) {
  31. // here is where you recieve input from server
  32. var response = scriptStream.read(count);
  33. alert("Data available");
  34. },
  35. };
  36. outstream.write(data, data.length);
  37. outstream.flush();
  38. this.pump.init(this.stream, -1, -1, 0, 0, false);
  39. this.pump.asyncRead(dataListener, null);
  40. //to write to stream
  41. outstream.write(data, data.length);
  42. outstream.flush();
  43. window._content.document.location = encodeURI(query);
  44. }
  45. //var serverSocket;
  46. //serverSocket = io.connect('127.0.0.1:12345');
  47. //serverSocket.emit('clientMessage', 'Yo server');
  48. //function getLocalPlaya(whichOne){
  49. //var filePa = emfile.path+'\\chrome\\content\\players\\'+whichOne;
  50. //var gunsNRoses = filePa.replace('\\', '\\\\');
  51. //var bFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  52. //bFile.initWithPath(gunsNRoses);
  53. //var istream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
  54. //istream.init(bFile, -1, -1, false);
  55. //var bstream = Components.classes["@mozilla.org/binaryinputstream;1"].createInstance(Components.interfaces.nsIBinaryInputStream);
  56. //bstream.setInputStream(istream);
  57. //var bytes = bstream.readBytes(bstream.available());
  58. /*
  59. var serverSocket;
  60. var listener =
  61. {
  62. onSocketAccepted : function(socket, transport)
  63. {
  64. try {
  65. /*var outputString = 'HTTP/1.1 200 OK\n' +
  66. 'Content-Length: '+bytes.length+'\r\n'+
  67. 'Content-Type: application/x-shockwave-flash\r\n' +
  68. 'content-disposition: attachment; filename="'+whichOne+'"\r\n'+
  69. 'Accept-Ranges: bytes\r\n'+
  70. bytes;
  71. var outputString = query;
  72. var stream = transport.openOutputStream(0,0,0);
  73. stream.write(outputString,outputString.length);
  74. stream.close();
  75. } catch(ex2){ LOG("::"+ex2); }
  76. },
  77. onStopListening : function(socket, status){}
  78. };
  79. try {
  80. serverSocket = Components.classes["@mozilla.org/network/server-socket;1"]
  81. .createInstance(Components.interfaces.nsIServerSocket);
  82. serverSocket.init(12345,true,-1);
  83. serverSocket.asyncListen(listener);
  84. } catch(ex){ }
  85. */