为 NodeJS 服务器开发客户端应用程序时必须使用 Socket.IO 吗? [英] Is Socket.IO a must in developing client apps for NodeJS servers?
问题描述
我是 NodeJS 的新手,发现它非常有趣.我在使用 NodeJS 构建服务器时没有遇到太多问题.但是当我去开发一个 Web 浏览器客户端时,我开始了解 Socket.IO.但我对此非常困惑.我们可以在不使用 NodeJS 服务器的 Socket.IOS 的情况下开发 Web 客户端应用程序吗?
在没有 Socket.IO 的 NodeJS 中是否支持 websockets?
这个问题很老了,但供以后参考.
Socket.IO 很有名,对于节点流 https://github.com/substack/stream-手册对你来说应该很有趣.
我个人在我自己的项目中这样做:
http://kenokabe.github.io/MarkdownLive/
我使用 Markdown 编写内容并且需要流式预览,所以我自己创建了.预览画面为浏览器 HTML 页面,HTML 内容以流式方式增量渲染和更新.
这种技术一般称为RPC(remote procedure call)
.Socket.IO 很多人都很熟悉,你可以在网上找到丰富的资源,这是一个很好的开始.
但是,我个人不再使用它,因为它是一个巨大的库,只是为了在节点和浏览器之间进行 RPC,尤其是当您知道除了 websocket 之外不需要任何回退时.
在这里做 RPC 的更简洁、更复杂(我认为)和无缝的方法是使用
dnode 或 rpc-stream使用 Browserify.
证明:以下是我自己项目的客户端(浏览器)代码:http://kenokabe.github.io/MarkdownLive/
https://github.com/kenokabe/MarkdownLive/blob/master/www/js/index.js
(function()//-----{严格使用";$(文件).ready(函数(){//---------------------------$('#streamDIV').html('<h1>MarkdownLive</h1><h3>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.txt/h4>');var through = require('through');var stream = require('shoe')('/stream').pipe(通过(功能(数据){$('#streamDIV').html(数据);}));//-------------------------});}());
正确使用 node.js 流,可以用简洁的声明式编程或 FRP 方式编写服务器-客户端 RPC 代码.
node.js 中没有 webSocket 的原生实现NodeJS 的 Web Sockets 服务器端实现和Socket.IO 基于 ws.
WebSocket 层
ws
或 sock-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
I'm new to NodeJS and found it very interesting. I'm not having much problems with building servers with NodeJS. But when I went to develop a web browser-client, I came to know about Socket.IO. But I'm very much confused about this. Can we develop web client applications without using Socket.IOS for NodeJS servers?
Is there support for websockets in NodeJS without Socket.IO?
This question is old, but for future references.
Socket.IO is famous, and for node stream https://github.com/substack/stream-handbook should be interesting to you.
I personally do this on my own project:
http://kenokabe.github.io/MarkdownLive/
I write stuff using Markdown and needed a streaming preview, so I created by myself. The preview screen is a Browser HTML page, and the HTML contents rendered and updated incrementally in streaming manner.
This technology is generally called RPC(remote procedure call)
. Socket.IO is familiar to many people and you can find abundant resource on the web, so good thing to start.
However, I personally do not use it anymore since it's a huge library just to do RPC between node and browser, especially when you do know you don't need any fallbacks to other than websocket.
The more concise and sophisticated (I think) and seamless way to do RPC here is to use
dnode or rpc-stream with Browserify.
Proof: the below is the code on the client side (browser) of my own project: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);
}));
//-------------------------
});
}());
Using node.js stream properly, it's possible to write a server-client RPC code in concise Declarative programming or FRP manner.
There is no native implementation of webSocket in node.js Web Sockets server side implementation for NodeJS and Socket.IO is based on ws.
WebSocket layer
ws
or sock-js or the others.
Stream layer
https://github.com/maxogden/websocket-stream for ws
https://github.com/NodeGuy/WebSocketStream for ws
https://github.com/kenokabe/WebSocketStreamPlus (my work based on NodeGuy's one)
https://github.com/substack/shoe for sock-js
RPC layer
https://github.com/substack/dnode
https://github.com/dominictarr/rpc-stream
这篇关于为 NodeJS 服务器开发客户端应用程序时必须使用 Socket.IO 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!