1

我正在尝试使用 XPCOM 在 Firefox 上的 Javascript 中编写一个小型服务器+客户端。

为了在 Javascript 中获取 HTTP 消息,我使用了 nsIScriptableInputStream 接口。这个 f**ing 组件通过 read() 方法随机剪切消息,我无法使其可靠。

有人知道可靠地获取信息的解决方案吗?(我已经尝试过二进制流,同样失败。)

J。

4

2 回答 2

1

我在不可靠性方面遇到了同样的问题......我最终使用了 XMLHTTPRequest,当从 XPCOM 组件中使用它时,它可以进行跨站点请求。文档的第二部分详细介绍了如何实例化 XPCOM 版本。

如果您正在寻找服务 HTTP 请求,我会看一下POW源代码和服务器套接字的使用,它在 JavaScript 中实现了一个基本的 HTTP 服务器。另请查看httpd.js

于 2008-09-16T19:08:17.577 回答
0

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. The client can open a stream either via dynamically adding a <script> tag to the DOM. The server can then send a stream of Javascript commands like:

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

Then the client just needs to define a receiveMsg function. 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? I would expect it to be more stable than some poorly documented Firefox-specific XPCOM interface.

--Chouser

于 2008-09-16T16:00:47.013 回答