javascript 中的端口监听
我正在尝试用 JavaScript 编写一些简单的聊天客户端,但我需要一种实时更新消息的方法。 虽然我可以使用 xmlhttprequest 函数,但我相信它会占用服务器上的 TCP/IP 端口,并且可能更重要的是,它在我当前的托管包中是不允许的。 无论如何,这似乎不是一个理想的解决方案,因为持续保持开放连接似乎有点麻烦,如果我可以只监听端口并获取数据,那就容易多了。 我在网上查了一下,发现很多关于彗星和连续轮询的参考,这些都不能令人满意,而且很多人说javascript不太适合它,我可以同意。 现在我实际上已经更多地了解了互联网的工作原理,但是,这似乎是可行的。到目前为止我不需要担心发送消息; 我可以处理这个问题,但是有什么方法可以监听 javascript 上的某个端口吗?
I'm trying to write some simple chat client in javascript, but I need a way to update messages in real time. While I could use the xmlhttprequest function, I believe it uses up a TCP/IP port on the server and possibly more importantly, is not allowed on my current hosting package. It doesn't seem like an ideal solution anyway as it seems a bit hacky to constantly have an open connection, and it would be a lot easier if I could just listen on the port and take the data as it comes. I looked on the internet and found lots of references to comet and continuous polling, which are unsatisfactory and lots of people say that javascript isn't really suited to it which I can agree with. Now I've actually learned a bit more about how the internet works however, it seems feasible.I don't need to worry about sending messages so far; I can deal with that, but is there any way to listen on a certain port on javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Javascript 中不可能侦听端口。
但是:
XmlHTTPRequest 在您的主机上是可能的,因为它是针对特殊站点(例如
chat.php?userid=12&action=poll&lasttime=31251
)的简单 HTTP 请求,服务器在其中打印所有新消息自从上次的结果以来。Listening on a port is not possible in Javascript.
But:
XmlHTTPRequest is possible on your host, as it is a simple HTTP request for a special site like
chat.php?userid=12&action=poll&lasttime=31251
where the server prints all new messages since lasttime as the result.您将需要一个可以加载到 Javascript 上下文中的第三方库。
Javascript 本身无法做到这一点,而且您不太可能(当然也不应该)在浏览器中执行此操作。
You are going to need a third-party library that you can load into the Javascript context.
Javascript itself has no way to do this and its unlikely you could (and certainly you should not) do this inside a browser.
看看 Comet
Take a look at Comet
您是否考虑过在 Flex 中构建您的应用程序? 您可以利用Flex的 XMLSocket 类来实现低延迟聊天客户端 - 几乎就是它的设计目的
Have you considered perhaps building your app in Flex ? You could make use of Flex's XMLSocket class to implement a low-latency chat client - pretty much the sort of thing it was designed to do