websockets

WebSockets are an alternative to HTTP communication in Web Applications.

WebSocket是Web应用程序中HTTP通信的替代方法。

They offer a long lived, bidirectional communication channel between client and server.

它们在客户端和服务器之间提供了长期的双向通信通道。

Once established, the channel is kept open, offering a very fast connection with low latency and overhead.

建立通道后,通道将保持打开状态,从而提供非常快速的连接,并具有较低的延迟和开销。

浏览器对WebSocket的支持 (Browser support for WebSockets)

WebSockets are supported by all modern browsers.

所有现代浏览器都支持WebSocket。

WebSockets与HTTP有何不同 (How WebSockets differ from HTTP)

HTTP is a very different protocol, and also a different way of communicate.

HTTP是一个非常不同的协议,也是一种不同的通信方式。

HTTP is a request/response protocol: the server returns some data when the client requests it.

HTTP是一个请求/响应协议:服务器在客户端请求时返回一些数据。

With WebSockets:

使用WebSockets:

  • the server can send a message to the client without the client explicitly requesting something

    服务器可以向客户端发送消息,而无需客户端明确请求

  • the client and the server can talk to each other simultaneously

    客户端和服务器可以同时对话

  • very little data overhead needs to be exchanged to send messages. This means a low latency communication.

    发送消息所需的数据开销很少 。 这意味着低延迟的通信

WebSockets are great for real-time and long-lived communications.

WebSockets非常适合实时长期通信。

HTTP is great for occasional data exchange and interactions initiated by the client.

HTTP非常适合客户端偶尔进行的数据交换和交互。

HTTP is much simpler to implement, while WebSockets require a bit more overhead.

HTTP的实现要简单得多 ,而WebSockets需要更多的开销。

安全的WebSocket (Secured WebSockets)

Always use the secure, encrypted protocol for WebSockets, wss://.

始终对WebSocket使用安全的加密协议wss://

ws:// refers to the unsafe WebSockets version (the http:// of WebSockets), and should be avoided for obvious reasons.

ws://是指不安全的WebSockets版本(WebSockets的http:// ),出于明显的原因应避免使用。

创建一个新的WebSockets连接 (Create a new WebSockets connection)

const url = 'wss://myserver.com/something'
const connection = new WebSocket(url)

connection is a WebSocket object.

connection是一个WebSocket对象。

When the connection is successfully established, the open event is fired.

成功建立连接后,将触发open事件。

Listen for it by assigning a callback function to the onopen property of the connection object:

通过为connection对象的onopen属性分配一个回调函数来监听它:

connection.onopen = () => {//...
}

If there’s any error, the onerror function callback is fired:

如果有任何错误,则会触发onerror函数回调:

connection.onerror = error => {console.log(`WebSocket error: ${error}`)
}

使用WebSockets将数据发送到服务器 (Sending data to the server using WebSockets)

Once the connection is open, you can send data to the server.

打开连接后,您可以将数据发送到服务器。

You can do so conveniently inside the onopen callback function:

您可以在onopen回调函数中方便地执行此操作:

connection.onopen = () => {connection.send('hey')
}

使用WebSocket从服务器接收数据 (Receiving data from the server using WebSockets)

Listen with a callback function on onmessage, which is called when the message event is received:

使用onmessage上的回调函数进行侦听,该函数在onmessage message事件时调用:

connection.onmessage = e => {console.log(e.data)
}

在Node.js中实现WebSockets服务器 (Implement a WebSockets server in Node.js)

ws is a popular WebSockets library for Node.js.

ws是Node.js的流行WebSockets库。

We’ll use it to build a WebSockets server. It can also be used to implement a client, and use WebSockets to communicate between two backend services.

我们将使用它来构建WebSockets服务器。 它还可以用于实现客户端,并使用WebSocket在两个后端服务之间进行通信。

Easily install it using

使用轻松安装

yarn init
yarn add ws

The code you need to write is very little:

您需要编写的代码很少:

const WebSocket = require('ws')const wss = new WebSocket.Server({ port: 8080 })wss.on('connection', ws => {ws.on('message', message => {console.log(`Received message => ${message}`)})ws.send('ho!')
})

This code creates a new server on port 8080 (the default port for WebSockets), and adds a callback function when a connection is established, sending ho! to the client, and logging the messages it receives.

此代码在端口8080(WebSocket的默认端口)上创建一个新服务器,并在建立连接时添加回调函数,发送消息ho! 发送给客户端,并记录其收到的消息。

查看有关Glitch的实时示例 (See a live example on Glitch)

Here is a live example of a WebSockets server: https://glitch.com/edit/#!/flavio-websockets-server-example

这是一个WebSockets服务器的实时示例: https ://glitch.com/edit/#!/flavio-websockets-server-example

Here is a WebSockets client that interacts with the server: https://glitch.com/edit/#!/flavio-websockets-client-example

这是一个与服务器交互的WebSockets客户端: https ://glitch.com/edit/#!/flavio-websockets-client- example

翻译自: https://flaviocopes.com/node-websockets/

websockets

websockets_将WebSockets与Node.js结合使用相关推荐

  1. node.js使用手册_权威的Node.js手册

    node.js使用手册 Note: you can get a PDF, ePub, or Mobi version of this handbook for easier reference, or ...

  2. Node.js 究竟是什么?

    2019独角兽企业重金招聘Python工程师标准>>> 简介 如果您听说过 Node,或者阅读过一些文章,宣称 Node 是多么多么的棒,那么您可能会想:"Node 究竟是 ...

  3. [转]为什么我要用 Node.js? 案例逐一介绍

    原文地址:http://blog.jobbole.com/53736/ 介绍 JavaScript 高涨的人气带来了很多变化,以至于如今使用其进行网络开发的形式也变得截然不同了.就如同在浏览器中一样, ...

  4. 如何创建高性能、可扩展的Node.js应用?

    作者|Virgafox 译者|姚佳灵 出处丨前端之巅 说明:本文根据原文作者的系列文章编辑而成,略有删改. 在这篇文章中,我们将介绍关于开发 Node.js web 应用程序的一些最佳实践,重点关注效 ...

  5. 哪个websocket库与Node.js一起使用? [关闭]

    本文翻译自:Which websocket library to use with Node.js? [closed] Currently there is a plethora of websock ...

  6. node.js学习总结

    NodeJS介绍 1.概述: Node.js是基于Chrome JavaScript运行时建立的一个平台,实际上它是对Google Chrome V8引擎 进行了封装,它主要用于创建快速的.可扩展的网 ...

  7. Node.js模块之Buffer

    简言 在没有出现Node.js之前,JavaScript还是运行在浏览器端,对于处理Unicode编码的字符串数据很容易,但是对于处理二进制以及非Unicode编码的数据无能为力,但是对于Server ...

  8. 2021年Node.js开发人员学习路线图

    Node.js 自发布以来,已成为业界重要破局者之一.Uber.Medium.PayPal 和沃尔玛等大型企业,纷纷将技术栈转向 Node.js.Node.js 支持开发功能强大的应用,例如实时追踪 ...

  9. Node.js 指南(入门指南)

    入门指南 安装Node之后,让我们尝试构建我们的第一个Web服务器,创建名为"app.js"的文件,并粘贴以下代码: const http = require('http');co ...

  10. Node.js 应该用在什么地方

    Node.js 应该用在什么地方 聊天 聊天是最典型的多用户实时交互的应用.从IRC开始,有许多开源或者不开源的协议都运行在非标准端口上,而现在,使用 Node.js 则可以解决这些问题--在标准的8 ...

最新文章

  1. jwt 私钥_JSON Web Token (JWT)生成Token及解密实战。
  2. 作弊翻车!Kaggle 大赛第一团队获最严处分
  3. 【设置Oracle 11Gr2 RAC的归档模式】
  4. xshell与xftp使用注意
  5. [BZOJ 4819] [SDOI 2017] 新生舞会
  6. 功能性农业实用技术 谋定·农业大健康-李喜贵:粤黔东西协作
  7. 找中位数,找第k小,还存在问题
  8. 前端学习(1832):前端面试题之跨域
  9. mapreduce原理_Hbase Bulkload 原理面试必备
  10. win7家庭版安装oracle,win7 home 版安装 Oracle 10g
  11. hive 增加表字段语录_Hive改表结构的两个坑|避坑指南
  12. 使用ftp命令之后,如何退出
  13. Memcached启动提示:cann't run as root without the -u switch
  14. 计算机房电源解决方案,机房UPS电源解决方案
  15. SOP:Ubuntu20安装微信
  16. androoid_4_4 yuga_clm920_cn 4G_module调试
  17. 完全用GNU/Linux工作,摈弃Windows---你我共勉 (转)
  18. ubuntu系统安装时的分区方案
  19. 【量化课程】01_投资与量化投资
  20. 虚岁与周岁的本质区别,是时间与人的不同关系

热门文章

  1. 【常用配置】——WPS文字常用快捷键大全【史上最全面】转
  2. JAVA调用K3Cloud WebApi接口
  3. VS2012更改/重置默认开发环境
  4. 【ArcGIS微课1000例】0033:地图点状标记符号设计教程
  5. 2022-09-13 mysql列存储引擎-POC-查询数据错误追踪
  6. 利用python从网络上爬取图片_我用Python爬取了妹子网100G的套图
  7. 爬取mzi.com妹子图片网站(requests库)
  8. 基于java校园教务排课系统设计与实现(springboot框架)
  9. STM32红外串口接收
  10. 电力前沿:Hightopo 助力贵州院打造智慧能源生态系统