AJAX使用JavaScript客户端 - 服务器架构 [英] AJAX and Client-Server Architecture with JavaScript

查看:176
本文介绍了AJAX使用JavaScript客户端 - 服务器架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要计划的网站,但我宁愿不喜欢静态的HTML性质。我preFER更多的客户端 - 服务器架构。

I have to program websites, but I rather don't like the static HTML nature. I prefer more of a client-server architecture.

现在我已经想通,与XMLHTTP,你基本上可以动态更新您的网页和发送/要求信息/动作/从服务器。因此,这将基本覆盖了客户区。

Now I've figured, that with XMLhttp, you can basically dynamically update your page and send/request for information/action to/from a server. So this would basically cover the client area.

但是,为了完成一个客户端 - 服务器体系结构,它是必要的服务器发送/请求信息,也没有被查询。

But to complete a client-server architecture, it is necessary for the server to send/request information, too, without being queried.

有什么办法,例如用于聊天服务器,发送回一个收到的消息给所有客户(客户端使用一个网络浏览器),而该客户端必须在固定的时间来查询?我想要实现一个可以看到,而你输入的东西

Is there any way, for example for a chat server, to send back a received message to all clients (the clients use a web browser) without that the clients have to query in a fixed interval? I want to implement that one can see while you type something in.

推荐答案

有几种不同的方式来做到这一点。他们中的一些已经回答了这里,但我想有几个和我的想法在他们身上。

There are several different ways to accomplish this. Some of them are already answered here, but I wanted to include a few more as well as my thoughts on them.

1。轮询

频繁请求进行到服务器,以检查是否有新信息。这是这样做的最糟糕的方式,但可能是最容易。如果您的网站将拥有的用户数量较少,它可能是值得做这种方式。

Frequent requests are made to the server to check for new info. This is the worst way to do this, but probably the easiest. If your site will have a low number of users, it might be worth doing it this way.

这可以通过使用来实现的setInterval(myFunction的,N)中的JavaScript发送 XMLHtt prequests 到服务器的每 N 毫秒。然后,在服务器上,你对此作出回应和你的新的信息,当你拥有了它,或者一些消息,意味着没有新的信息。

This can be accomplished by using setInterval(myFunction, n) in javascript to send XMLHttpRequests to the server every n milliseconds. Then, on the server, you respond to this with your new info, when you have it, or some message that implies no new info.

2。长轮询

在加载页面时,它发出请求到服务器进行新的信息。服务器保持连接打开,直到有东西送回去。这种方法降低了使用的网络通信量,但会增加服务器上使用的资源。可以使用此一小数量的用户,但它并不规模非常好

When the page is loaded, it makes a request to the server for new info. The server holds the connection open until there is something to send back. This method reduces the amount of network traffic used, but increases the resources used on the server. You can use this for a small number of users, but it doesn't scale very well.

要做到这一点,最简单的方法是有一个处理AJAX请求只需等待新的信息可用,那么响应页面。这会占用大量的连接在服务器上。因此,使用时要小心。

The easiest way to do this is to have the page that handles the AJAX request simply wait for new information to be available, then respond. This can tie up a lot connections on your server. So, use with care.

3。 COMET

COMET 基本上是长轮询,但服务器是正确安装它。据了解,这些连接是不是真正的,它使用更少的资源来处理它们。这是这个问题的一个很好的解决方案,但它需要服务器是明确设置用于此目的。有COMET服务器和COMET加载项的其他流行的服务器,但它需要一些设置,有时一些钱。

COMET is basically long polling, but the server is setup properly for it. It knows that these connections aren't "real" and it uses less resources to handle them. This is a great solution for this problem, but it requires that the server is explicitly setup for this purpose. There are COMET servers and COMET addins for other popular servers, but it will require some setup and sometimes some money.

在.NET实现这还不是最容易的事在世界上。你可以支付解决方案,试图找到别人的code,做类似的东西,或者尝试自己写。我还没有发现任何像样的免费解决方案这一点。如果别人有,请评论。

Implementing this on .NET isn't the easiest thing in the world. You can pay for solutions, try to find someone else's code that does something similar, or try to write it yourself. I've not found any decent free solutions for this. If someone else has, please comment.

4。 RIA

另一种解决方案是包括闪存,Silverlight或Java小程序在页面上。人们通常方法是使用一个1x1的对象,以便他们可以使用Flash或Silverlight交谈的服务器。如果你不介意添加依赖,这是一个不错的解决方案。如果你已经知道的Silverlight或Flash,也可能是相对容易实现。

Another solution would be to include Flash, Silverlight, or Java Applet on your page. People often do this by using a 1x1 object so that they can use Flash or Silverlight to talk to the server. If you don't mind adding the dependency, this is a decent solution. If you already know Silverlight or Flash, it could be relatively simple to implement.

您可以在互联网上找到的教程每个选项。

You can find tutorials on the internet for each of these options.

5。网络套接字

如果你是在刀刃上,你可以看看网络套接字的。这是只适用于最新建立的现代浏览器。它是HTML5的一部分,但也可能是它自己的规范现在。无论如何,这意味着旧的浏览器将无法处理它。但是,如果你不介意限制自己最新的浏览器,你可以用这个神奇的功能。

If you are on the cutting edge, you can look into Web Sockets. It's only available in the latest builds of modern browsers. It was part of HTML5, but it might be its own spec now. Regardless, it means that older browsers won't be able to handle it. But, if you don't mind limiting yourself to the latest of browsers, you can use this amazing feature.

我相信是目前支持它的浏览器。不过,目前正在进行的工作在Firefox和WebKit来实现这一点。

I believe that Chromium is the only browser that currently supports it. However, there is work being done to implement this in Firefox and WebKit.

我就不告诉你的争议,简单地说,这不正是你想要的。该规范的摘要说明了一切。

I'll spare you the controversy and simply say that this does exactly what you want it to. The Abstract of the spec says it all.

该规范定义了一个API,使网页使用双向通信网络套接字协议与远程主机。

This specification defines an API that enables Web pages to use the Web Sockets protocol for two-way communication with a remote host.

特别表扬

如果您有兴趣节点JS的世界里,你不能去错插槽IO 。会执行最好的任何技术可用于浏览器。

If you are interested in the world of Node JS, you can't go wrong with Socket IO. It will implement the best of whichever technology is available to the browser.

结论

最好的办法是Socket.IO节点上的JS。然而,对于一个ASP.Net解决方案,去彗星或网络套接字,如果你能。否则,使用Flash / Silverlight是不可怕的。最后,轮询和长轮询应该是最后的解决办法。你总是可以支持其中的一个,然后回落到另一种,如果没有它支持在客户端的浏览器。

The best option is Socket.IO on Node JS. However, for an ASP.Net solution, go for COMET or Web Sockets, if you can. Otherwise, using Flash/Silverlight isn't terrible. Finally, polling and long polling should be last resorts. You could always support one of these, then fall back to another if there isn't support for it in the client's browser.

这篇关于AJAX使用JavaScript客户端 - 服务器架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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