将数据推送到客户端 [英] Pushing data to a client

查看:126
本文介绍了将数据推送到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢将PHP和JavaScript与jQuery结合使用.

I like using PHP and JavaScript with jQuery.

我正在使用PHP从SQL数据库中显示项目列表.我有一个按钮,可以在新标签页中打开一个新页面,该标签页将向数据库添加其他项.

I am displaying a list of items out of a SQL database using PHP. I have a button that opens up a new page in a new tab that will add an additional item to the database.

如何在不刷新页面的情况下将数据库中新添加的项目添加到第一页当前显示的列表中?

How would I add the newly added item in the database to the currently displayed list on the first page without refreshing that page?

我可以强迫页面每隔几秒钟发出一个ajax请求,但我认为这不是很好的做法.

I can force the page to make an ajax request every few seconds but I don't think that is very good practice.

我一直在研究套接字-但是我不知道如何将这些知识应用于我的当前情况.

I have been looking into sockets - however I don't know how to apply that knowledge to my current situation.

推荐答案

如果您希望服务器将信息推送回客户端,请彗星可能就是您想要的.

If you want the server to push the information back to the client, Comet may be what are you looking for.

最近的浏览器支持 WebSockets ,它使服务器无需与客户端进行通信即可.客户提出常规的AJAX请求.较旧的浏览器不支持WebSocket,因此,如果需要支持它们,您可能会对允许使用更基本的技术(例如在这些浏览器上进行长时间轮询)的框架感兴趣.

Recent browsers support WebSockets, which enables the server to communicate with clients without the need for the client to make regular AJAX requests. Older browsers don't support WebSockets, so if you need to support them, you may be interested in frameworks which allow to fallback to more basic techniques, such as long polling on those browsers.

请注意,有些库可以使WebSockets或Comet真正容易地集成到您的Web应用程序中,所以不要重新发明轮子并坚持使用它您现在使用的PHP框架中提供了该功能.例如,如果是Laravel,一个简单的Google搜索就会带我到 BrainSocket 一个Laravel软件包,允许您使用WebSocket启动并运行实时事件驱动的PHP应用."

Note that there are libraries which make it really easy (well, some don't) to integrate WebSockets or Comet in your web application, so don't reinvent the wheel and stick with the one which is available in the PHP framework you use right now. For example, if it's Laravel, a simple Google search leads me to BrainSocket, "a Laravel package that allows you to get up and running with real-time event-driven PHP apps using WebSockets."

在所有情况下,都不希望您使用套接字(除非套接字,您的意思是WebSockets.)

In all cases, you're not expected to use sockets (unless by sockets, you mean WebSockets in your question).

这篇关于将数据推送到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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