This article is about a PsycZilla feature. See also File Transfer for the generic Builtin webserver approach.
Contents |
Introduction
We plan to implement File Transfer via HTTP. A simple throttled httpd will be implemented in JavaScript and links will be shared via PSYC. Files can be dropped in a room to send a notice with the download url (It's not that this was the final plan for PSYC File Sharing, but since we're not living in a world dominated by PSYC clients, this is a start).
Initial Discussion
lynX suggests that we should implement an http server in the client for file sharing. Drop a file on the client window in order to offer the url to a file served locally.
- The interface that we implement to support this is: Components.interfaces.nsIServerSocket;
http://xulplanet.com/tutorials/mozsdk/serverpush.php
- <lynX> The second bullet in that document should read "The server uses the 'multipart/x-mixed-replace'" instead of "'The client uses the 'multipart/x-mixed-replace'". Server push is powerful, but MSIE never implemented it. But that doesn't matter, as we are not going to do that. We are going for the third option: Server Sockets!
Here's an example code snippet:
function http_server() { var serverSocket; this.start = function() { var listener = { onSocketAccepted : function(socket, transport) { try { var outputString = "HTTP/1.1 200 OK\n" + "Content-type: text/plain\n\n" + "Hello there " + transport.host + "\n"; var stream = transport.openOutputStream(0,0,0); stream.write(outputString,outputString.length); stream.close(); } catch(ex2){ dlux_debug(ex2); } }, onStopListening : function(socket, status){} }; try { serverSocket = Components.classes["@mozilla.org/network/server-socket;1"] .createInstance(Components.interfaces.nsIServerSocket); serverSocket.init(7055,false,-1); serverSocket.asyncListen(listener); } catch(ex){ dump(ex); } psyc_log("HTTP Server Started"); } this.stop = function() { if (serverSocket) serverSocket.close(); psyc_log("HTTP Server Stopped"); } } var httpd = new http_server();
Binary Streams in JavaScript
- XPCOM Interface: nsIBinaryInputStream
- An example of code that uses it.
- Thanks to ChatZilla developer Silver for pointing me in the right direction!
the Plain Old Webserver (POW)
Someone else made a web server extension for firefox: https://addons.mozilla.org/firefox/3002/
<lynX> It's neat. Let's integrate it..!?