nodejs:Ajax 与 Socket.IO,优缺点 [英] nodejs: Ajax vs Socket.IO, pros and cons
问题描述
我想摆脱所有客户端 Ajax 调用 (jQuery),而是使用永久套接字连接 (Socket.IO).
I thought about getting rid of all client-side Ajax calls (jQuery) and instead use a permanent socket connection (Socket.IO).
因此,我会在客户端和服务器端使用事件侦听器/发射器.
Therefore I would use event listeners/emitters client-side and server-side.
例如.单击事件由用户在浏览器中触发,客户端发射器通过套接字连接将事件推送到服务器.服务器端侦听器对传入事件做出反应,并将完成"事件推送回客户端.客户端的侦听器通过淡入 DIV 元素对传入事件做出反应.
Ex. a click event is triggered by user in the browser, client-side emitter pushes the event through socket connection to server. Server-side listener reacts on incoming event, and pushes "done" event back to client. Client's listener reacts on incoming event by fading in DIV element.
这有意义吗?优点 &缺点?
Does that make sense at all? Pros & cons?
推荐答案
发送单向消息并调用它们的回调会变得非常混乱.
Sending one way messages and invoking callbacks to them can get very messy.
$.get('/api', sendData, returnFunction);
比socket.emit('sendApi', sendData);
socket.on('receiveApi', returnFunction);
这就是为什么在 socket.io 之上构建 dnode 和 nowjs 以使事情易于管理的原因.仍然是事件驱动但不放弃回调.
Which is why dnode and nowjs were built on top of socket.io to make things manageable. Still event driven but without giving up callbacks.
这篇关于nodejs:Ajax 与 Socket.IO,优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!