简体   繁体   English

如何使用Javascript通过Firefox XPCOM获取可靠的HTTP消息

[英]How to get reliable HTTP messages via Firefox XPCOM in Javascript

I am trying to program a small server+client in Javascript on Firefox, using XPCOM. 我正在尝试使用XPCOM在Firefox上的Javascript中编写小型服务器+客户端程序。

To get the HTTP message in Javascript, I am using the nsIScriptableInputStream interface. 为了使用Javascript获得HTTP消息,我正在使用nsIScriptableInputStream接口。 This f**ing component through the read() method randomly cut the message and I cannot make it reliable. 通过read()方法创建的组件会随机剪切消息,我无法使其可靠。

Is anybody know a solution to get reliably the information? 有人知道一种可靠获取信息的解决方案吗? (I already tried a binary stream, same failure.) (我已经尝试过二进制流,同样失败。)

J. J.

I had the same problem with unreliability... I ended up using XMLHTTPRequest, which when used from the XPCOM component can do cross site requests. 我在可靠性方面也遇到了同样的问题...我最终使用了XMLHTTPRequest,当从XPCOM组件中使用XMLHTTPRequest时,它可以执行跨站点请求。 The second part of the docs detail how to instantiate the XPCOM version. 文档的第二部分详细介绍了如何实例化XPCOM版本。

If you're looking to serve HTTP request I'd take a look at the POW source code and the use of server sockets , which implements a basic HTTP server in JavaScript. 如果您要处理HTTP请求,那么我将看一下POW源代码和服务器套接字的使用,该服务器套接字在JavaScript中实现了基本的HTTP服务器。 Also check out httpd.js 还要查看httpd.js

If you control the protocol (that is, both the client and server) I would highly recommend using Javascript/JSON for your server-to-client messages. 如果您控制协议(即客户端和服务器),则强烈建议对服务器到客户端消息使用Javascript / JSON。 The client can open a stream either via dynamically adding a <script> tag to the DOM. 客户端可以通过向DOM动态添加<script>标记来打开流。 The server can then send a stream of Javascript commands like: 然后,服务器可以发送Javascript命令流,例如:

receiveMsg({type:"text", content:"this is my message"});

Then the client just needs to define a receiveMsg function. 然后,客户端只需要定义一个receiveMsg函数。 This allows you to rely on fast browser code to parse the message and determine where the end of each message is, at which point it will call your handler for you. 这使您可以依靠快速的浏览器代码来解析消息并确定每条消息的末尾,这时它将为您调用处理程序。

Even if you're working with an existing HTTP protocol and can't use JSON, is there some reason you can't use XMLHttpRequest? 即使您正在使用现有的HTTP协议并且不能使用JSON,还是有某些原因不能使用XMLHttpRequest? I would expect it to be more stable than some poorly documented Firefox-specific XPCOM interface. 我希望它比某些文献记载不完整的Firefox XPCOM界面更稳定。

--Chouser -克雷尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM