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中实现服务器 (Implement a 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在两个后端服务之间进行通信。

Install it using npm:

使用npm安装它:

npm init
npm install 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

调试WebSocket (Debugging websockets)

Chrome and Firefox have a handy way to visualize all the information that is sent through WebSockets. Their DevTools support is always improving. Firefox at the time of writing has the most useful and informative debugging tool.

Chrome和Firefox提供了一种方便的方法来可视化通过WebSockets发送的所有信息。 他们的DevTools支持始终在提高。 撰写本文时,Firefox具有最有用,最有用的调试工具。

In both, you look into the Network panel and choose WS to filter only WebSockets connections.

在这两种方法中,您都需要查看“网络”面板,然后选择“ WS”以仅过滤WebSockets连接。

I took these screenshots of Firefox (Developer Edition) in action:

我拍摄了以下Firefox(开发人员版)的屏幕截图:

The Firefox DevTools can do much more than what I showed. In the example I used to test, I’m just sending a string, but you can inspect any data that’s sent in a more organized fashion:

Firefox DevTools可以做的比我展示的要多得多。 在我用来测试的示例中,我只是发送一个字符串,但是您可以检查以更有条理的方式发送的任何数据:

Check out this post on Mozilla Hacks to know more about how to use this tool.

查看有关Mozilla Hacks的文章,以了解更多有关如何使用此工具的信息。

Here is a screenshot of Chrome, which hopefully will improve its WebSockets debugging tools soon:

这是Chrome的屏幕截图,希望可以很快改进其WebSockets调试工具:

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

websockets

websockets_WebSockets简介相关推荐

  1. etcd 笔记(01)— etcd 简介、特点、应用场景、常用术语、分布式 CAP 理论、分布式原理

    1. etcd 简介 etcd 官网定义: A highly-available key value store for shared configuration and service discov ...

  2. Docker学习(一)-----Docker简介与安装

    一.Docker介绍 1.1什么是docker Docker是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源 Docker可以让开发者打包他们的应用以及依赖包到一个轻量级,可移植 ...

  3. 【Spring】框架简介

    [Spring]框架简介 Spring是什么 Spring是分层的Java SE/EE应用full-stack轻量级开源框架,以IOC(Inverse Of Control:反转控制)和AOP(Asp ...

  4. TensorRT简介

    TensorRT 介绍 引用:https://arleyzhang.github.io/articles/7f4b25ce/ 1 简介 TensorRT是一个高性能的深度学习推理(Inference) ...

  5. 谷粒商城学习笔记——第一期:项目简介

    一.项目简介 1. 项目背景 市面上有5种常见的电商模式 B2B.B2C.C2B.C2C.O2O B2B 模式(Business to Business),是指商家和商家建立的商业关系.如阿里巴巴 B ...

  6. 通俗易懂的Go协程的引入及GMP模型简介

    本文根据Golang深入理解GPM模型加之自己的理解整理而来 Go协程的引入及GMP模型 一.协程的由来 1. 单进程操作系统 2. 多线程/多进程操作系统 3. 引入协程 二.golang对协程的处 ...

  7. Linux 交叉编译简介

    Linux 交叉编译简介 主机,目标,交叉编译器 主机与目标 编译器是将源代码转换为可执行代码的程序.像所有程序一样,编译器运行在特定类型的计算机上,输出的新程序也运行在特定类型的计算机上. 运行编译 ...

  8. TVM Operator Inventory (TOPI)简介

    TOPI简介 这是 TVM Operator Inventory (TOPI) 的介绍.TOPI 提供了比 TVM 具有更高抽象的 numpy 风格的,通用操作和调度.TOPI 如何在 TVM 中,编 ...

  9. 计算机视觉系列最新论文(附简介)

    计算机视觉系列最新论文(附简介) 目标检测 1. 综述:深度域适应目标检测标题:Deep Domain Adaptive Object Detection: a Survey作者:Wanyi Li, ...

  10. 2021年大数据ELK(二十三):Kibana简介

    全网最详细的大数据ELK文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. Kibana简介 通过上面的这张图就可以看到,Kibana可以用来展示丰富的图表. ...

最新文章

  1. mysql架构设计书籍推荐_最近很火的MySQL:抛开复杂的架构设计,MySQL优化思想基本都在这...
  2. Selenium 与 PhantomJS
  3. SiamMask:视频跟踪最高精度 (中科院王强大神作品)
  4. C++构造函数调用规则
  5. linux-磁盘分区、挂载
  6. 批量下载baidu音乐主页的歌曲
  7. 基于IP子网将加域的电脑移动到不同的OU
  8. 业界分享 | 百度图神经网络实践
  9. 第5章 见缝插针(《C和C++游戏趣味编程》配套教学视频)
  10. 算法笔记_面试题_10.所有可能的满二叉树
  11. PHP 安全检测代码片段
  12. 自动驾驶 2-4 环境表示 Environment Representation
  13. 【电商运营】京东数字化运营【客单价篇】
  14. 2018-2019的裁员风波(程序员要有危机意识,程序员堪比娱乐圈,更新换代快)
  15. android wifi连接优先级,gogo平台靠谱吗-官方网站
  16. Matlab学习——求解微分方程(组)
  17. colorbox去除close关闭按钮,附上colorbox的基本使用方法
  18. 西安千锋培训python
  19. 怎么把视频变成GIF
  20. STM32实战③RGB渐变

热门文章

  1. Linux应用层24点小游戏,C++ Builder构建算二十四点小游戏
  2. Android APK反编译技巧全讲解
  3. c语言对称矩阵的压缩存储_对称矩阵的压缩存储和输出
  4. 加密狗映射至虚拟服务器,ESXI 5.1/5.5 主机添加或映射USB设备(加密狗)(示例代码)...
  5. lattice diamond/radiant license申请
  6. 高速电路设计与仿真之PCB篇(二)
  7. 北京环球度假区宣布首批21家旅游渠道官方授权合作伙伴
  8. ​新手到底如何入门PLC?
  9. java.this的作用包括,智慧职教: 以下不是Java中this关键字的作用的是()。
  10. ES6阮一峰笔记部分对象新增方法、字符串的扩展和新增方法