1 dump('entering util/socket.js\n');
7 Install netcat on a server and as root do: nc -l -p 5000
9 Then, in the staff client, load Admin -> For Developers -> Javascript Shell
13 JSAN.use('util.socket');
14 var s = new util.socket('server hostname or IP address here', 5000);
17 On the server, reply with world<enter>
19 Back in the javascript shell, use
25 if (typeof util == 'undefined') util = {};
26 util.socket = function (host,port,listener) {
30 throw('host = ' + host + ' port = ' + port);
36 this.listener = listener;
38 this._create_listener = true;
44 alert('error in util.socket constructor: ' + E);
51 util.socket.prototype = {
53 '_onStartRequest' : null,
54 'onStartRequest' : function(callback) {
55 this._onStartRequest = callback;
57 '_onDataAvailable' : null,
58 'onDataAvailable' : function(callback) {
59 this._onDataAvailable = callback;
61 '_onStopRequest' : null,
62 'onStopRequest' : function(callback) {
63 this._onStopRequest = callback;
65 '_reconnectOnStop' : false,
66 'dataCallback' : function(callback) {
67 this._dataCallback = callback;
70 const Cc = Components.classes;
71 const Ci = Components.interfaces;
72 const socket_Cc = "@mozilla.org/network/socket-transport-service;1";
73 var transportService = Cc[socket_Cc].getService(
74 Ci.nsISocketTransportService
76 this.socket = transportService.createTransport(
77 null,0,this.host,this.port,null);
78 this.outputStream = this.socket.openOutputStream(0,0,0);
79 this.rawInputStream = this.socket.openInputStream(0,0,0);
80 const istream_Cc = "@mozilla.org/scriptableinputstream;1";
81 this.inputStream = Cc[istream_Cc].createInstance(
82 Ci.nsIScriptableInputStream
87 if (this._create_listener) {
88 this.listener = this.generate_listener();
90 const pump_Cc = "@mozilla.org/network/input-stream-pump;1";
91 this.pump = Cc[pump_Cc].createInstance(Ci.nsIInputStreamPump);
92 this.pump.init(this.rawInputStream,-1,-1,0,0,true);
93 this.pump.asyncRead(this.listener,null);
95 'close' : function() {
96 dump('util.socket.close() on page ' + location.href + '\n');
98 this.pump.cancel(true);
99 this.socket.close(true);
101 dump('Error in util.socket.close(): ' + E + '\n');
104 'generate_listener' : function() {
106 Components.utils.import("resource://gre/modules/NetUtil.jsm");
108 onStartRequest : function(request,context) {
109 dump('util.socket.pump.onStartRequest on page ' + location.href + '\n');
110 if (obj._onStartRequest) {
111 obj._onStartRequest(request,context);
114 onDataAvailable : function(request,context,stream,offset,count) {
115 dump('util.socket.pump.onDataAvailable on page ' + location.href + '\n');
116 if (obj._onDataAvailable) {
117 dump('util.socket.pump.onDataAvailable using _onDataAvailable\n');
118 obj._onDataAvailable(request,context,stream,offset,count);
120 var data = NetUtil.readInputStreamToString(stream,count);
122 if (obj._dataCallback) {
123 dump('util.socket.pump.onDataAvailable using _dataCallback\n');
124 obj._dataCallback(data);
126 dump('util.socket.pump.onDataAvailable no _dataCallback to use\n');
130 onStopRequest : function(request,context,result) {
131 dump('util.socket.pump.onStopRequest on page ' + location.href + '\n');
132 if (!Components.isSuccessCode(result)) {
135 if (obj._onStopRequest) {
136 obj._onStopRequest(request,context,result);
137 } else if (obj._reconnectOnStop) {
138 // FIXME - cannot get this to work, but you
139 // can overwrite the socket with a new socket
140 // within _onStopRequest
145 'write' : function(s) {
146 this.outputStream.write(s,s.length);
148 'read' : function(peek) {
149 var data = this._data;
157 dump('exiting util/socket.js\n');