为什么nsIScriptableInputStream不工作? [英] Why nsIScriptableInputStream is not working?

查看:122
本文介绍了为什么nsIScriptableInputStream不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的java代码

  Socket clientSocket = new Socket(localhost,8888); 
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true);
字符串messageToServer =成功;
out.write(messageToServer);

这是我的javascript

 onInputStreamReady:function(input){
var sin = Components.classes [@ mozilla.org / (Ci.nsIScriptableInputStream);
sin.init(input);
var request ='';
尝试
{
while(true)
{
var chunk = sin.read(512);
alert(chunk.length);
if(chunk.length == 0)
break;
alert(chunk);
request = request + chunk;
}
alert(已收到+请求);

catch(e)
{
alert(Error:failed from stream:\\\
+ e +\\\
);


$ b var listener = {
onSocketAccepted:function(serverSocket,transport){
addSpan(Accepted connection on+ transport。主机+:+ 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);


$ b code
$ b

但Javascript没有收到任何东西。我在链接中看到相同的问题。如何做到这一点?

解决方案

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



1.nsiTransportService b
$ b 2.nsIScriptableInputStream
$ b $ 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.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){
//这里是建立连接的事件
},
onStopRequest:function(request,context,status){
//这里是事件,如果连接丢失
},
onDataAvailable:function(request,context,inputStream,offset,count ){
//这里是从服务器
recive输入的地方var response = scriptStream.read(count);
};

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

//写入流
outstream.write(data,data.length);
outstream.flush();

希望这对您有所帮助。哦,顺便说一句,从我的经验,代码的顺序是至关重要的,但感觉
免费证明我worng: - )。

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?

解决方案

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 :-).

这篇关于为什么nsIScriptableInputStream不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆