客户端如何处理来自服务器的多个响应的node.js [英] How client side Handle node.js with multiple response from the server

查看:153
本文介绍了客户端如何处理来自服务器的多个响应的node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器(节点)似乎具有类似于线程的行为,因为它处理同步请求,但是我们知道客户端的javascript不支持多线程.我只是问客户端(javascript)如何处理node.js,尤其是在服务器有多个响应的情况下? 例如,我想同时渲染两个对象.

It seems that the server(node) has a thread like behavior since it handles synchronous requests, but we know that javascript on the client side doesnt support multi-threading. Im just asking how the client side(javascript) handle node.js especially with multiple responses from the server? for an example i want to render two objects at the same time.

使用node.js制作快速分阶段的实时游戏应用程序是否合适?

Is it appropriate to make a fast phased realtime game app with node.js?

有没有办法解决这个问题?

Are there ways or patterns to handle this?

推荐答案

将节点视为指挥官,它可以无障碍地处理任务的监视.因此,如果您这样想,则节点不需要是多线程的,它无需任何等待时间即可执行任务,因此无需创建新线程,因为没有任何阻塞.

Think of node as a commander, it handles the monitoring of task without any blocking. So if you think of it like that, node does not need to be multi threaded, it executes tasks without any wait time, therefore there is no need to create a new thread as nothing is blocked.

您在评论中提到了套接字,请考虑一下,Node具有一个网络线程,该线程监视TCP/UDP连接以获取数据,当它收到一个数据包时,请向您的节点脚本询问您对该数据包的处理方式,您立即告诉网络线程将这个新数据包作为答复发送出去,然后网络线程继续执行此操作,然后调用下一个事件.

You mention about sockets in your comment, think of this, Node has a network thread that monitors the TCP/UDP connection for data, when it receives a packet it mealy ask's your node script what you would like done with this packet, you instantly tell the network thread to send this new packet out as a reply and then the network thread goes of to do that while then next event is called.

看看下面的图片和段落,这应该解释

Take a look at the following image and paragraph, this should explain

在node.js中,您不必担心后端会发生什么:只需在执行I/O时使用回调即可;并且可以确保您的代码永不中断,并且执行I/O不会阻塞其他请求,而不必承担每个请求的线程/进程成本(例如Apache中的内存开销).

In node.js, you aren’t supposed to worry about what happens in the backend: just use callbacks when you are doing I/O; and you are guaranteed that your code is never interrupted and that doing I/O will not block other requests without having to incur the costs of thread/process per request (e.g. memory overhead in Apache).

拥有异步I/O很好,因为I/O比大多数代码都昂贵,我们应该做的比等待I/O更好.

Having asynchronous I/O is good, because I/O is more expensive than most code and we should be doing something better than just waiting for I/O.

事件循环是处理和处理外部事件并将其转换为回调调用的实体".因此,I/O调用是Node.js从一个请求切换到另一个请求的关键点.在I/O调用中,您的代码将保存回调并将控制权返回给node.js运行时环境.当数据实际可用时,将在稍后调用该回调.

An event loop is "an entity that handles and processes external events and converts them into callback invocations". So I/O calls are the points at which Node.js can switch from one request to another. At an I/O call, your code saves the callback and returns control to the node.js runtime environment. The callback will be called later when the data actually is available.

来源: http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/

请务必阅读该文章,这是使我完全了解后端体系结构正在发生什么的第一篇文章.

Be sure to read that article it was the first article that made me fully understand what was going on with the back-end architecture.

这篇关于客户端如何处理来自服务器的多个响应的node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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