WebSocket & websockets
WebSocket & websockets
https://en.wikipedia.org/wiki/WebSocket
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
构建网络应用的过程中,我们经常需要与服务器进行持续的通讯以保持双方信息的同步。
通常这种持久通讯在不刷新页面的情况下进行,消耗一定的内存资源常驻后台,并且对于用户不可见。
https://caniuse.com/#feat=websockets

https://html.spec.whatwg.org/multipage/comms.html#network
传统轮询 (Traditional Polling)
长轮询 (Long Polling)
服务器发送事件 (Server-Sent Event)
SSE只支持服务器到客户端单向的事件推送.

https://segmentfault.com/a/1190000012948613
https://github.com/Pines-Cheng/share
https://en.wikipedia.org/wiki/Push_technology
http://www.ruanyifeng.com/blog/2017/05/websocket.html
https://developer.mozilla.org/zh-CN/docs/Web/API/WebSocket
https://segmentfault.com/a/1190000012709475#articleHeader20
demo
// todo...
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description WS server & node.js
* @augments
* @example
*
*/
const WebSocket = require('ws');
const wss = new WebSocket.Server({
// host: "",
// path: "",
port: 8888
});
let counter = 1;
wss.on('connection', function (ws, req) {
console.log("client connected", counter);
counter ++;
ws.on("message", function (msg) {
console.log(`receive message = `, msg);
if (ws.readyState === 1) {
const json = {
"success": true,
"message": null,
"data": [
{
"pro_name": "otc",
"pro_instructions": null,
"pro_type_name": "front-end",
"send_time": null,
"incre": true,
},
{
"pro_name": "ds",
"pro_instructions": null,
"pro_type_name": "back-end",
"send_time": null,
"incre": false
}
]
};
// const json = {
// success: true,
// message: "success",
// data: []
// };
let datas = JSON.stringify(json);
// return json datas;
ws.send(datas);
// ws.send("server returned message!");
}
});
let ip = req.connection.remoteAddress;
console.log(`ip =`, ip);
});
full demo
OK


server
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description WS
* @augments
* @example
*
*/
// const WSGenerator = (datas = [], debug = false) => {
// let result = ``;
// // do something...
// return result;
// };
// export default WS;
// export {
// WS,
// };
const WebSocket = require('ws');
const wss = new WebSocket.Server({
// host: "",
// path: "",
port: 8888
});
let counter = 1;
wss.on('connection', function (ws, req) {
console.log("client connected", counter);
counter ++;
ws.on("message", function (msg) {
console.log(`receive message = `, msg);
if (ws.readyState === 1) {
const json = {
"success": true,
"message": null,
"data": [
{
"pro_name": "otc",
"pro_instructions": null,
"pro_type_name": "front-end",
"send_time": null,
"incre": true,
},
{
"pro_name": "ds",
"pro_instructions": null,
"pro_type_name": "back-end",
"send_time": null,
"incre": false
}
]
};
// const json = {
// success: true,
// message: "success",
// data: []
// };
let datas = JSON.stringify(json);
// return json datas;
ws.send(datas);
// ws.send("server returned message!");
}
});
let ip = req.connection.remoteAddress;
console.log(`ip =`, ip);
});
client
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description WS
* @augments
* @example
*
*/
// const WS = (datas = [], debug = false) => {
// let result = ``;
// // do something...
// return result;
// };
// export default WS;
// export {
// WS,
// };
const url = `ws://10.1.64.138:8888/F10_APP/src/test`;
let ws = new WebSocket(url);
ws.onopen = function(e) {
console.log(`已经建立连接 open`, ws.readyState);
console.log(`e = `, e);
};
ws.onerror = function(e) {
console.log(`连接异常 error`, ws.readyState);
console.log(`e = `, e);
};
ws.onmessage = function(res) {
console.log(`收到消息 message`, ws.readyState);
let data = res.data,
origin = res.origin;
console.log(`res & e = `, res);
console.log(`res.data = `, res.data);
console.log(`res.origin = `, res.origin);
};
ws.onclose = function(e) {
console.log(`已经关闭连接 close`, ws.readyState);
console.log(`e = `, e);
};
setTimeout(() => {
ws.onopen = function(e) {
console.log(`已经建立连接 open`, ws.readyState);
console.log(`e = `, e);
};
}, 1000 * 1);
WebSocket & websockets的更多相关文章
- 网络游戏开发-服务器(01)Asp.Net Core中的websocket,并封装一个简单的中间件
先拉开MSDN的文档,大致读一遍 (https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/websockets) WebSocket 是一 ...
- WebSocket——为Web应用带来桌面应用般的灵活性【转载+整理】
原文地址 本文内容 WebSocket 简介 浏览器端的 JavaScript 实现 Java 端的 WebSocket 实现 对 Web 应用的重新思考 使用WebSocket时所需注意的要点 We ...
- WebSocket的使用(基于VUE与SpringBoot)
WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议.WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在 We ...
- Java搭建WebSocket的两种方式
下面分别介绍搭建方法:一.直接使用Java EE的api进行搭建.一共3个步骤:1.添加依赖<dependency> <groupId>javax</groupId ...
- websocket推送进度条百分比给前台
说明:后台springboot项目 前台vue+element-UI 直接放代码: //别忘了开启springboot的websocket <dependency> <groupId ...
- 浅析websocket的基本应用spring boot + vue +C# + WPF
1.基本概念 首先websocket是基于H5的一种通信.在网页中如果定时获取服务器端的实时数据,我们常采用long poll 和ajax轮询的方式.但是在轮询过程中,由于根本没有新数据的改变,而造成 ...
- Kotlin 最佳实践
为什么写此文 Kotlin很烦,Gralde很烦,还都是升级狂,加一块更烦.几个月不接触Kotlin,再次上手时便一片迷茫.所以记录此文,以便再次上手时查阅. 使用Gradle创建Kotlin项目 m ...
- API / DOM
浏览器特性 当控制台报错时,IE9会停止执行JS.当打开控制台时会执行后续JS ------------------------------------------------------------ ...
- Web API 接口-JavaScript全部api接口文档
当使用JavaScript编写网页代码时,有很多API可以使用.以下是所有对象.类型等接口的列表,你在开发网页应用程序或站点时使用它们. API文档地址:https://developer.mozil ...
随机推荐
- UVA 11468 Substring (AC自动机)
用把失配边也加到正常边以后AC自动机,状态是长度递减的DAG,每次选一个不会匹配字符的转移. dp[u][L]表示当前在tire树上u结点长度还剩L时候不匹配的概率,根据全概率公式跑记忆化搜索. #i ...
- UVA11019 Matrix Matcher (AC自动机)
二维的矩阵匹配,把模式矩阵按列拆开构造AC自动机,记录行号(为了缩点判断). 把T矩阵按行匹配,一旦匹配成功,在假想的子矩阵左上角位置加一.最后统计总数. 因为所有模式串长度一样,不用维护last数组 ...
- python_106_创建类的两种方式
class Foo(object): def __init__(self, name): self.name = name f = Foo("alex") print(type(f ...
- 用 label 控制 Pod 的位置
默认配置下,Scheduler 会将 Pod 调度到所有可用的 Node.不过有些情况我们希望将 Pod 部署到指定的 Node,比如将有大量磁盘 I/O 的 Pod 部署到配置了 SSD 的 Nod ...
- SC || Chapter 1
第一章的重中之重就是这张图吧 (具体参见笔记) ┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉ 区分哪些属性是外部的(面向用户 ...
- 线程的sleep方法
- Vue中npm run build报“Error in parsing SVG: Unquoted attribute value”
自己做的一个Vue项目,在打包时老是报这个错误 # Error in parsing SVG: Unquoted attribute value 查了查网上说的,都说报错原因是压缩和抽离CSS的插件中 ...
- NPM下载模块包说明
博主对npm包安装收集了各种资料和实践后对它们之间的差异整理,写下这篇文章避免自己忘记,同时也给node.js猿友一点指引. 我们在使用 npm install 安装模块的模块的时候 ,一般会使用下面 ...
- ps基础入门快捷方法总结
1. 快速打开文件 双击Photoshop的背景空白处(默认为灰色显示区域)即可打开选择文件的浏览窗口. 2. 随意更换画布颜色 选择油漆桶工具并按住Shift点击画布边缘,即可设置画布底色为当前选择 ...
- virtualvenv+django+uWSGI+nginx 部署 踩坑记录
原创博文 转载请注明出处! uwsgi: unrecognized option '--http:8089' uwsgi: unrecognized option '--http' uwsgi trk ...