Nodejs 网络 - 实时通信

发布于 2024-09-26 15:04:22 字数 308 浏览 1 评论 0原文

我是 node.js 的新手,我想问一个关于它如何工作的简单问题。

我过去曾使用 FM 进行客户端到客户端的通信和实时应用程序。例如,为了创建协作应用程序,您需要查看其他用户正在做什么。我想使用 NodeJS 来探索这一点。

我有几个问题:

1)NodeJs 如何处理服务器到客户端的通信?有什么办法可以将信息推送给客户吗?或者客户端需要不断向服务器发出请求以查看是否有任何更改?

2)服务器和客户端之间是否存在永久连接之类的东西?

3)如何处理客户端到客户端的通信(当然是通过服务器)?

提前致谢。

I'm new to node.js and I want to ask a simple question about how it works.

I have used FMs in the past for client to client communication and real time applications. For example, for creating a collaborative application where you need to see what other users are doing. I want to explore that using NodeJS.

I have couple of questions:

1) How does NodeJs handle server-to-client communication? Is thee any way to push information to the client? or the client needs to be making requests constantly to the server to see if anything has changed?

2) Is there such thing like permanent connections between the server and the clients?

3) How Can be handle client-to-client communication (of course thru the server)?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉鱼一梦 2024-10-03 15:04:22

3) 如何处理客户端到客户端
沟通(当然是通过
服务器)?

一个简单的解决方案是在服务器和每个客户端之间打开一个 websocket :

[Client A] <==websocket==> [服务器] <==websocket==> [客户端B]

例如,如果您使用Socket.IO,那么通过这种方式进行客户端到客户端的通信非常容易。

当服务器收到来自一个客户端的消息时,您只需将其广播到所有客户端或将其发送到一个特定客户端,具体取决于您的用例。

使用 Socket.IO 的一些示例代码:

var socket = io.listen(server);
socket.on('connection', function(client) {
  client.on('message', function(msg) {
    broadcast(msg); // broadcast message to all clients
    // OR
    socket.clients[session_id].send(msg); // send message to one client
  });

  client.on('disconnect', function( ) {
    console.log('Client Disconnected.');
  });
});

3) How Can be handle client-to-client
communication (of course thru the
server)?

A simple solution is to open a websocket between the server and each client :

[Client A] <==websocket==> [Server] <==websocket==> [Client B]

If you go with Socket.IO for example, it is very easy to do client-to-client communication this way.

When the server receives a message from one client, you just broadcast it to all clients or send it to one specific client depending on your use case.

Some example code using Socket.IO :

var socket = io.listen(server);
socket.on('connection', function(client) {
  client.on('message', function(msg) {
    broadcast(msg); // broadcast message to all clients
    // OR
    socket.clients[session_id].send(msg); // send message to one client
  });

  client.on('disconnect', function( ) {
    console.log('Client Disconnected.');
  });
});
只怪假的太真实 2024-10-03 15:04:22

最近您提出了很多 Node.js 问题 ;)

  1. 正如 Toby 所说,Node 可以处理 HTTP、TCP/UDP 和 Unix Sockets。当您建立永久连接时,您当然可以将数据推送到客户端。

  2. 由于您正在谈论基于浏览器的客户端,因此有多种方法可以实现此目的。例如,您可以将 WebSocket 与 Flash 后备一起使用。如果您对底层细节不感兴趣并且想要完整的包,请查看 Socket.IO< /a>.

  3. 据我所知,WebSockets 无法做到这一点,Flash 也无法做到这一点。因此,除非您想进入 Java/Silverlight 领域,否则您需要通过服务器路由请求。

Quite a lot of Node.js questions from you recently ;)

  1. As Toby already said, Node can do HTTP, TCP/UDP and Unix Sockets. When you establish a permanent connection, you can of course push data to the clients.

  2. Since you are talking about Browser based clients, there a numerous ways to achieve this. You could for example use WebSockets with a Flash fallback. In case you are not interested in the low level details and want a complete package, take a look at Socket.IO.

  3. WebSockets can't do this, Flash can't do it either as far as I know. So unless you want to enter Java/Silverlight land, you'll need to route the requests through your server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文