简体   繁体   English

如何实施实时表单编辑,就像Google Docs一样

[英]How to Implement Real-time Form Editing, Like Google Docs

I am trying to add the feature that Google Docs has, which is real time collaboration, to an editable textarea in HTML. 我正在尝试将Google Docs所具有的功能(即实时协作)添加到HTML中的可编辑文本区域。 For example, this would allow 2 or 3 users can edit the same textarea collaboratively. 例如,这将允许2或3个用户可以协作编辑相同的textarea。 How would one go about approaching this problem, or is there is a JavaScript library that can be used? 如何处理这个问题,或者是否有可以使用的JavaScript库? (I use PHP, mySQL, and JavaScript/AJAX/jQuery). (我使用PHP,mySQL和JavaScript / AJAX / jQuery)。

In order to facilitate real-time updates between more than one Web client, you'll need to use a technology that either capitalizes on the request/response cycle of the Web by using a Comet or Websockets solution. 为了促进多个Web客户端之间的实时更新,您需要使用一种技术,该技术可以使用Comet或Websockets解决方案利用Web的请求/响应周期。

To keep the textarea updated, you'd need to establish a long-lived HTTP connection or a Websocket connection to your server from all 3 clients. 要保持textarea的更新,您需要从所有3个客户端建立与服务器的长期HTTP连接或Websocket连接。 Each textarea would need a keyup or keypress handler that, when invoked, sends the character across the stream to the server. 每个textarea都需要一个keyup或keypress处理程序,在调用时,它将流中的字符发送到服务器。 When your server retrieves this data, it would then need to return a response to the other 2 connected clients. 当您的服务器检索此数据时,它将需要将响应返回给其他2个连接的客户端。

The response would need to then be handled by updating the value property of the textarea with the most recent data. 然后需要通过使用最新数据更新textarea的value属性来处理响应。

I see you're using PHP, which I do know supports comet. 我看到你正在使用PHP,我知道它支持彗星。 You'll need to setup comet (or Websockets) in order to implement such a solution. 您需要设置comet(或Websockets)才能实现此类解决方案。

With that said, a more rudimentary alternative would be to use polling to achieve the desired effect. 话虽如此,一个更基本的选择是使用轮询来达到预期的效果。 This would involve all 3 clients making requests to the server periodically to retrieve updates. 这将涉及所有3个客户端定期向服务器发出请求以检索更新。 As you can imagine, the faster the polling rate, the more real time the application would feel. 可以想象,轮询率越快,应用程序的实际时间就越多。 However, with a faster the polling rate, your application would consume more bandwidth and resources. 但是,轮询速率越快,您的应用程序将消耗更多带宽和资源。

For 3 clients, this may be feasible, but for any serious application that involved heavy usage, you would definitely want to look into Websockets or Comet. 对于3个客户端,这可能是可行的,但对于涉及大量使用的任何严重应用程序,您肯定想要查看Websockets或Comet。

To answer your question of JavaScript libraries, check out the Dojo Cometd library for a Comet solution on the client-side. 要回答您的JavaScript库问题,请在客户端查看Dojo Cometd库以获取Comet解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM