为什么 nsIScriptableInputStream 不起作用?

发布于 2024-12-10 16:26:25 字数 1894 浏览 0 评论 0原文

我正在开发 firefox 扩展,它将通过 Socket 与 java 进行通信。

这是我的 java 代码

      Socket clientSocket = new Socket("localhost", 8888);
      PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
      String messageToServer="Success" ;
      out.write(messageToServer);

这是我的 javascript

var reader = {
            onInputStreamReady : function(input) {
                var sin = Components.classes["@mozilla.org/scriptableinputstream;1"]
                            .createInstance(Ci.nsIScriptableInputStream);
                sin.init(input);
                var request = '';
                try
                {
                  while (true)
                  {
                    var chunk = sin.read(512);
                    alert(chunk.length);
                    if (chunk.length == 0)
                      break;
                    alert(chunk);
                    request=request+chunk;
                  }
                  alert("Received"+request);
                }
                catch (e)
                {
                  alert("Error: failed reading from stream:\n" + e + "\n");
                }
            } 
        } 
    var listener = {
        onSocketAccepted: function(serverSocket, transport) {
            addSpan("Accepted connection on " + transport.host + ":" + transport.port);
            var input = transport.openInputStream(0, 0, 0).QueryInterface(Ci.nsIAsyncInputStream);
            var output = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
            var tm = Cc["@mozilla.org/thread-manager;1"].getService();
            input.asyncWait(reader,0,0,tm.mainThread);

        }
    }

但 Javascript 没有收到任何内容。我在链接中看到了同样的问题。要做什么才能使这项工作有效?

I'm working on firefox extension which will communicate with java through Socket.

Here is my java code

      Socket clientSocket = new Socket("localhost", 8888);
      PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
      String messageToServer="Success" ;
      out.write(messageToServer);

Here is my javascript

var reader = {
            onInputStreamReady : function(input) {
                var sin = Components.classes["@mozilla.org/scriptableinputstream;1"]
                            .createInstance(Ci.nsIScriptableInputStream);
                sin.init(input);
                var request = '';
                try
                {
                  while (true)
                  {
                    var chunk = sin.read(512);
                    alert(chunk.length);
                    if (chunk.length == 0)
                      break;
                    alert(chunk);
                    request=request+chunk;
                  }
                  alert("Received"+request);
                }
                catch (e)
                {
                  alert("Error: failed reading from stream:\n" + e + "\n");
                }
            } 
        } 
    var listener = {
        onSocketAccepted: function(serverSocket, transport) {
            addSpan("Accepted connection on " + transport.host + ":" + transport.port);
            var input = transport.openInputStream(0, 0, 0).QueryInterface(Ci.nsIAsyncInputStream);
            var output = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
            var tm = Cc["@mozilla.org/thread-manager;1"].getService();
            input.asyncWait(reader,0,0,tm.mainThread);

        }
    }

But the Javascript doesn't receive anything. I see the same problem in the link. What to do to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒浓于脸红 2024-12-17 16:26:25

要使用套接字,您不仅需要可编写脚本的流,还需要:

1.nsiTransportService

2.nsIScriptableInputStream

3.nsIInputStreamPump

您可以使用此代码:

this.transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].
                                        getService(Components.interfaces.nsISocketTransportService);
    this.scriptablestream = Components.classes["@mozilla.org/scriptableinputstream;1"].
                                            createInstance(Components.interfaces.nsIScriptableInputStream);
    this.pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].
                                                createInstance(Components.interfaces.nsIInputStreamPump);
this.transport = this.transportService.createTransport(null, 0, server, port, null);
this.outstream = this.transport.openOutputStream(1, 0, 0);
this.outputData = "";
  //this is where the connection is actually opens.
            this.outstream.write(this.outputData, this.outputData.length);
            this.outstream.flush();
this.stream = this.transport.openInputStream(0, 0, 0);
this.scriptablestream.init(this.stream);
var dataListener = {
            data: "",
            onStartRequest: function (request, context) {
    //here is the event for connection established
                                },
            onStopRequest: function (request, context, status) {
    // here is the event if connection lost
            },
            onDataAvailable: function (request, context, inputStream, offset, count) {
    // here is where you recive input from server
                var response = scriptStream.read(count);
                            };

this.pump.init(this.stream, -1, -1, 0, 0, false);
        this.pump.asyncRead(dataListener, null);

//to write to stream
  outstream.write(data, data.length);
   outstream.flush();

希望这对您有帮助。哦,顺便说一下,根据我的经验,代码的顺序很重要,但感觉
可以自由地证明我的能力:-)。

to work with sockets you need not only the scriptable stream but:

1.nsiTransportService

2.nsIScriptableInputStream

3.nsIInputStreamPump

you can use this code:

this.transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].
                                        getService(Components.interfaces.nsISocketTransportService);
    this.scriptablestream = Components.classes["@mozilla.org/scriptableinputstream;1"].
                                            createInstance(Components.interfaces.nsIScriptableInputStream);
    this.pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].
                                                createInstance(Components.interfaces.nsIInputStreamPump);
this.transport = this.transportService.createTransport(null, 0, server, port, null);
this.outstream = this.transport.openOutputStream(1, 0, 0);
this.outputData = "";
  //this is where the connection is actually opens.
            this.outstream.write(this.outputData, this.outputData.length);
            this.outstream.flush();
this.stream = this.transport.openInputStream(0, 0, 0);
this.scriptablestream.init(this.stream);
var dataListener = {
            data: "",
            onStartRequest: function (request, context) {
    //here is the event for connection established
                                },
            onStopRequest: function (request, context, status) {
    // here is the event if connection lost
            },
            onDataAvailable: function (request, context, inputStream, offset, count) {
    // here is where you recive input from server
                var response = scriptStream.read(count);
                            };

this.pump.init(this.stream, -1, -1, 0, 0, false);
        this.pump.asyncRead(dataListener, null);

//to write to stream
  outstream.write(data, data.length);
   outstream.flush();

hope this helps you. oh and by the way from my experience the order of the code is critical but feel
free to prove me worng :-).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文