gpt4 book ai didi

node.js - 在为 NodeJS 服务器开发客户端应用程序时,Socket.IO 是必须的吗?

转载 作者:搜寻专家 更新时间:2023-11-01 00:15:45 26 4
gpt4 key购买 nike

我是 NodeJS 的新手,发现它非常有趣。我在使用 NodeJS 构建服务器时没有遇到太多问题。但是当我去开发一个网络浏览器客户端时,我开始了解 Socket.IO。但我对此很困惑。我们可以在不使用 NodeJS 服务器的 Socket.IOS 的情况下开发 Web 客户端应用程序吗?

在没有 Socket.IO 的情况下,NodeJS 是否支持 websockets?

最佳答案

这个问题很老了,但供以后引用。

Socket.IO 很有名,用于 Node 流https://github.com/substack/stream-handbook你应该会感兴趣。

我个人在自己的项目中这样做:

http://kenokabe.github.io/MarkdownLive/

我用 Markdown 写东西,需要流媒体预览,所以我自己创建了。预览画面是浏览器HTML页面,HTML内容以流方式递增渲染和更新。

这种技术一般称为RPC(remote procedure call)。 Socket.IO 对很多人来说都很熟悉,你可以在网络上找到丰富的资源,这是一个很好的开始。

但是,我个人不再使用它,因为它是一个巨大的库,仅用于在 Node 和浏览器之间执行 RPC,尤其是当您知道除了 websocket 之外不需要任何回退时。

这里进行 RPC 的更简洁和复杂(我认为)和无缝的方式是使用

dnoderpc-streamBrowserify .

证明:下面是我自己项目的客户端(浏览器)的代码:http://kenokabe.github.io/MarkdownLive/

https://github.com/kenokabe/MarkdownLive/blob/master/www/js/index.js

(function() //-----
{
"use strict";

$(document)
.ready(function()
{ //--------------------------
$('#streamDIV')
.html('<h1>MarkdownLive</h1><h3>Markdown Streaming Live View for SublimeText3</h3><br><h4><strong>Open</strong> <br><br>.md<br>.markdown<br>.mdown<br>.mkdn<br>.mkd<br>.mdwn<br>.mdtxt<br>.mdtext<br>.text<br>.txt</h4>');
var through = require('through');

var stream = require('shoe')('/stream')
.pipe(through(function(data)
{
$('#streamDIV')
.html(data);
}));
//-------------------------
});

}());

正确使用node.js stream,可以用简洁的Declarative编程或FRP方式写出server-client RPC代码。

node.js中没有webSocket的原生实现 Web Sockets server side implementation for NodeJS和Socket.IO 基于 ws .

WebSocket 层

wssock-js或其他人。

流层

https://github.com/maxogden/websocket-stream对于 ws

https://github.com/NodeGuy/WebSocketStream对于 ws

https://github.com/kenokabe/WebSocketStreamPlus (我的工作基于 NodeGuy 的)

https://github.com/substack/shoe对于 sock-js

RPC层

https://github.com/substack/dnode

https://github.com/dominictarr/rpc-stream

关于node.js - 在为 NodeJS 服务器开发客户端应用程序时,Socket.IO 是必须的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12832402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com