dongru2019 2015-11-26 16:01
浏览 131
已采纳

使用Angular的实时数据

i want to create a web page with data, this data can be edited in Real Time.

Users will see data in Real Time and can edit it, something like "Google Sheets" where everyone can edit the same file and see others changes in real time.

I will be using PHP, MYSQL, AngularJS.

I want to consult you on how to do it in the best way, this is some points that i thought of:

  1. Use angular polling every X seconds to update page data in real time, but if user editing one of the fields, how can i prevent from this specific field to be updated by polling?

  2. How can i LOCK specific field that user is editing, to prevent 2 users to edit the same field in real time

  3. There is any better way to pull data in real time than angular polling?

  4. When user editing text field i want to update it in database without "submit" or save button, i thought to save the data after 3 seconds, any better ideas?

Thank you,

  • 写回答

1条回答 默认 最新

  • dreamice2013 2015-11-26 20:56
    关注

    1: I'd suggest for you to have an array of objects or a datastructure similar to that, which contains the fields, in your AngularJS controller. When a user starts editing a certain field, you could set isEditing to true in the field object in your datastructure. Whenever an update comes in, you loop through your datastructure and only update fields of which isEditing isn't set / isn't true.

    For making it more realtime, instead of polling, setup WebSockets and let the server broadcast the newest values of a field to all editors whenever it gets changed.

    2: For locking a field that a certain user is working on, you could add a locked column to the database table containing the fields. Whenever a user wants to start editing, the following would occur:

    • User requests to edit a field
    • Server checks if the field is locked
    • If the field isn't locked, the user is permitted to edit the field and the server sets the locked column to true or to the username, depending on your needs.
    • If the field is locked, the user isn't permitted to edit the field

    When a user saves a field after editing it, you should set the locked column to false. You should probably also set the locked column to false whenever the editing user gets disconnected.

    3: PHP on it's own is not able to send data to the client without the client making a request. You'd need to add WebSocket support to PHP (for example http://socketo.me/, haven't tried that out though).

    If you are interested in a server platform that is able to do this out of the box, you could take a look at http://nodejs.org. When you plan on using Node.js, I suggest using http://socket.io/ for maximum browser compatibility. (it includes fallbacks for whenever WebSockets aren't supported by the users browser)

    4: You could save the current value every x seconds if the value is different from the previous save. This would be more efficient than always saving the value. You'd need to save the previously saved value in a variable for this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作