为什么 nsIScriptableInputStream 不起作用?
我正在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用套接字,您不仅需要可编写脚本的流,还需要:
1.nsiTransportService
2.nsIScriptableInputStream
3.nsIInputStreamPump
您可以使用此代码:
希望这对您有帮助。哦,顺便说一下,根据我的经验,代码的顺序很重要,但感觉
可以自由地证明我的能力:-)。
to work with sockets you need not only the scriptable stream but:
1.nsiTransportService
2.nsIScriptableInputStream
3.nsIInputStreamPump
you can use this code:
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 :-).