使用JavaScript的AJAX和客户端 - 服务器架构

时间:2022-08-21 23:54:51

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

我必须对网站进行编程,但我宁愿不喜欢静态HTML性质。我更喜欢客户端 - 服务器架构。

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.

现在我已经想到,使用XMLhttp,您基本上可以动态更新页面并向/从服务器发送/请求信息/操作。所以这基本上涵盖了客户区域。

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.

有没有办法,例如聊天服务器,将收到的消息发送回所有客户端(客户端使用Web浏览器),而客户端不必在固定的时间间隔内查询?我想实现一个人在你输入内容时可以看到的内容。

5 个解决方案

#1


13  

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. Polling

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.

经常向服务器发出请求以检查新信息。这是最糟糕的方式,但可能是最简单的方法。如果您的网站用户数量较少,则可能需要这样做。

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.

这可以通过在javascript中使用setInterval(myFunction,n)来每隔n毫秒将XMLHttpRequests发送到服务器来完成。然后,在服务器上,您可以使用新信息(如果有)或某些不显示新信息的消息对此进行响应。

2. Long Polling

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.

加载页面时,它向服务器发出新信息请求。服务器保持连接打开,直到有东西要发回。此方法可减少使用的网络流量,但会增加服务器上使用的资源。您可以将此用于少数用户,但它不能很好地扩展。

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.

最简单的方法是让处理AJAX请求的页面等待新信息可用,然后响应。这可能会占用您服务器上的大量连接。所以,要小心使用。

3. 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.

COMET基本上是长轮询,但服务器设置正确。它知道这些连接不是“真实的”,并且它使用较少的资源来处理它们。这是解决此问题的绝佳解决方案,但它要求为此目的明确设置服务器。有其他流行服务器的COMET服务器和COMET插件,但它需要一些设置,有时需要一些钱。

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.

在.NET上实现这一点并不是世界上最简单的事情。您可以为解决方案付费,尝试找到其他类似代码的代码,或者尝试自己编写代码。我没有找到任何体面的免费解决方案。如果其他人有,请发表评论。

4. RIA

4. RIA

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.

另一种解决方案是在您的页面上包含Flash,Silverlight或Java Applet。人们通常使用1x1对象来执行此操作,以便他们可以使用Flash或Silverlight与服务器通信。如果您不介意添加依赖项,这是一个不错的解决方案。如果您已经了解Silverlight或Flash,则实现起来可能相对简单。

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

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

5. Web Sockets

5. Web套接字

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.

如果您处于最前沿,可以查看Web套接字。它仅适用于最新版本的现代浏览器。它是HTML5的一部分,但它现在可能是它自己的规范。无论如何,这意味着旧版浏览器将无法处理它。但是,如果你不介意将自己限制在最新的浏览器中,你可以使用这个神奇的功能。

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.

我相信Chromium是目前唯一支持它的浏览器。但是,有一些工作要在Firefox和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.

我会免除你的争议,只是说这完全符合你的要求。规范的摘要说明了一切。

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

此规范定义了一个API,使Web页面能够使用Web套接字协议与远程主机进行双向通信。

Special Mention

特别提及

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.

如果您对Node JS的世界感兴趣,那么Socket IO就不会出错。它将实现浏览器可用的最佳技术。

Conclusion

结论

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.

最好的选择是Node JS上的Socket.IO。但是,对于ASP.Net解决方案,如果可以,请选择COMET或Web套接字。否则,使用Flash / Silverlight并不可怕。最后,民意调查和长期民意调查应该是最后的度假胜地。您可以随时支持其中一个,如果在客户端的浏览器中不支持它,则可以回退到另一个。

#2


2  

Yes, you can use COMET.

是的,您可以使用COMET。

#3


1  

The client has to tell the server when the client-user begins typing. You've got a couple options here.

客户端必须在客户端用户开始键入时告诉服务器。你有几个选择。

  1. Frequent requests from the server for the latest activity. This would be taking place for each user involved in the chat. The same request could be used to send user-specific activity to the server as well: "Jonathan is typing..."

    来自服务器的频繁请求以获取最新活动。这将发生在聊天中涉及的每个用户。可以使用相同的请求将用户特定的活动发送到服务器:“Jonathan正在输入...”

  2. Long-polling. This essentially requests information from the server, and the server keeps the connection opened until it has something to send back. So your requests are minimized, but your connections stay opened longer.

    长轮询。这本质上是从服务器请求信息,服务器保持连接打开,直到有东西要发回。因此,您的请求被最小化,但您的连接保持打开时间更长。

Depending on your traffic, type of data being transmitted, server-environment, and many other factors, one of these options may shine more than the other.

根据您的流量,传输的数据类型,服务器环境以及许多其他因素,这些选项中的一个可能比另一个更闪耀。

#4


0  

You can use Silverlight for push notifications. Look at PollingDuplexHttpBinding. Since you are using ASP.Net MVC, adding Silverlight will be easy.

您可以使用Silverlight进行推送通知。看看PollingDuplexHttpBinding。由于您使用的是ASP.Net MVC,因此添加Silverlight非常简单。

Look at this page for more information.

查看此页面以获取更多信息。

#5


0  

Based upon the REST architecture the html system is based upon, the servers role is to simply act as a resource for the client to pull from. I am generalizing but there are tools to implement this type of action on the client side, rather than on the server side.

基于html系统所基于的REST架构,服务器角色只是充当客户端的资源。我正在推广,但有一些工具可以在客户端实现这种类型的操作,而不是在服务器端。

You are better off writing/using a library that can request updates from the server periodically. You can encapsulate these types of requests in a javascript object that can fire events. This way your client side script can act like it's getting notified from the server. Review some common stuff with COMET you can probably find some tools to help you client side code.

您最好编写/使用可以定期从服务器请求更新的库。您可以将这些类型的请求封装在可以触发事件的javascript对象中。这样,您的客户端脚本可以像从服务器获得通知一样。查看COMET的一些常见内容,您可以找到一些工具来帮助您使用客户端代码。

HTML 5 has some tentative attempts at this type of functionality, but if you want your app to work on older browsers, your better off using more stable methods, like AJAX requested updates.

HTML 5对此类功能有一些尝试性尝试,但如果您希望您的应用程序在旧版浏览器上运行,最好使用更稳定的方法,例如AJAX请求的更新。

#1


13  

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. Polling

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.

经常向服务器发出请求以检查新信息。这是最糟糕的方式,但可能是最简单的方法。如果您的网站用户数量较少,则可能需要这样做。

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.

这可以通过在javascript中使用setInterval(myFunction,n)来每隔n毫秒将XMLHttpRequests发送到服务器来完成。然后,在服务器上,您可以使用新信息(如果有)或某些不显示新信息的消息对此进行响应。

2. Long Polling

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.

加载页面时,它向服务器发出新信息请求。服务器保持连接打开,直到有东西要发回。此方法可减少使用的网络流量,但会增加服务器上使用的资源。您可以将此用于少数用户,但它不能很好地扩展。

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.

最简单的方法是让处理AJAX请求的页面等待新信息可用,然后响应。这可能会占用您服务器上的大量连接。所以,要小心使用。

3. 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.

COMET基本上是长轮询,但服务器设置正确。它知道这些连接不是“真实的”,并且它使用较少的资源来处理它们。这是解决此问题的绝佳解决方案,但它要求为此目的明确设置服务器。有其他流行服务器的COMET服务器和COMET插件,但它需要一些设置,有时需要一些钱。

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.

在.NET上实现这一点并不是世界上最简单的事情。您可以为解决方案付费,尝试找到其他类似代码的代码,或者尝试自己编写代码。我没有找到任何体面的免费解决方案。如果其他人有,请发表评论。

4. RIA

4. RIA

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.

另一种解决方案是在您的页面上包含Flash,Silverlight或Java Applet。人们通常使用1x1对象来执行此操作,以便他们可以使用Flash或Silverlight与服务器通信。如果您不介意添加依赖项,这是一个不错的解决方案。如果您已经了解Silverlight或Flash,则实现起来可能相对简单。

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

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

5. Web Sockets

5. Web套接字

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.

如果您处于最前沿,可以查看Web套接字。它仅适用于最新版本的现代浏览器。它是HTML5的一部分,但它现在可能是它自己的规范。无论如何,这意味着旧版浏览器将无法处理它。但是,如果你不介意将自己限制在最新的浏览器中,你可以使用这个神奇的功能。

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.

我相信Chromium是目前唯一支持它的浏览器。但是,有一些工作要在Firefox和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.

我会免除你的争议,只是说这完全符合你的要求。规范的摘要说明了一切。

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

此规范定义了一个API,使Web页面能够使用Web套接字协议与远程主机进行双向通信。

Special Mention

特别提及

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.

如果您对Node JS的世界感兴趣,那么Socket IO就不会出错。它将实现浏览器可用的最佳技术。

Conclusion

结论

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.

最好的选择是Node JS上的Socket.IO。但是,对于ASP.Net解决方案,如果可以,请选择COMET或Web套接字。否则,使用Flash / Silverlight并不可怕。最后,民意调查和长期民意调查应该是最后的度假胜地。您可以随时支持其中一个,如果在客户端的浏览器中不支持它,则可以回退到另一个。

#2


2  

Yes, you can use COMET.

是的,您可以使用COMET。

#3


1  

The client has to tell the server when the client-user begins typing. You've got a couple options here.

客户端必须在客户端用户开始键入时告诉服务器。你有几个选择。

  1. Frequent requests from the server for the latest activity. This would be taking place for each user involved in the chat. The same request could be used to send user-specific activity to the server as well: "Jonathan is typing..."

    来自服务器的频繁请求以获取最新活动。这将发生在聊天中涉及的每个用户。可以使用相同的请求将用户特定的活动发送到服务器:“Jonathan正在输入...”

  2. Long-polling. This essentially requests information from the server, and the server keeps the connection opened until it has something to send back. So your requests are minimized, but your connections stay opened longer.

    长轮询。这本质上是从服务器请求信息,服务器保持连接打开,直到有东西要发回。因此,您的请求被最小化,但您的连接保持打开时间更长。

Depending on your traffic, type of data being transmitted, server-environment, and many other factors, one of these options may shine more than the other.

根据您的流量,传输的数据类型,服务器环境以及许多其他因素,这些选项中的一个可能比另一个更闪耀。

#4


0  

You can use Silverlight for push notifications. Look at PollingDuplexHttpBinding. Since you are using ASP.Net MVC, adding Silverlight will be easy.

您可以使用Silverlight进行推送通知。看看PollingDuplexHttpBinding。由于您使用的是ASP.Net MVC,因此添加Silverlight非常简单。

Look at this page for more information.

查看此页面以获取更多信息。

#5


0  

Based upon the REST architecture the html system is based upon, the servers role is to simply act as a resource for the client to pull from. I am generalizing but there are tools to implement this type of action on the client side, rather than on the server side.

基于html系统所基于的REST架构,服务器角色只是充当客户端的资源。我正在推广,但有一些工具可以在客户端实现这种类型的操作,而不是在服务器端。

You are better off writing/using a library that can request updates from the server periodically. You can encapsulate these types of requests in a javascript object that can fire events. This way your client side script can act like it's getting notified from the server. Review some common stuff with COMET you can probably find some tools to help you client side code.

您最好编写/使用可以定期从服务器请求更新的库。您可以将这些类型的请求封装在可以触发事件的javascript对象中。这样,您的客户端脚本可以像从服务器获得通知一样。查看COMET的一些常见内容,您可以找到一些工具来帮助您使用客户端代码。

HTML 5 has some tentative attempts at this type of functionality, but if you want your app to work on older browsers, your better off using more stable methods, like AJAX requested updates.

HTML 5对此类功能有一些尝试性尝试,但如果您希望您的应用程序在旧版浏览器上运行,最好使用更稳定的方法,例如AJAX请求的更新。