2021最新版阿里巴巴Java性能调优速成手册强烈推荐
为什么要做性能调优? 一款线上产品如果没有经过性能测试,那它就好比是一颗定时炸弹,你不知道它什么时候会出现问题,你也不清楚它能承受的极限在哪儿。 所以,要不要做性能调优,这个问题其实很好回答。所有的系统在开发完之后,多多少少都会有性能问题,我们首先要做的就是想办法把问题暴露出来,例如进行压力测试、模拟可能的操作场景等等,再通过性能调优去解决这些问题。 好的系统性能调优不仅仅可以提高系统的性能,还能为公司节省资源。这也是我们做性能调优的最直接的目的!所以,接下来我就给大家带来了一份“阿里巴巴lava性能调优实战(2021华山版)”想要学习的朋友们,我们就先来看看文章大概内容:(同时在文末会有笔记领取方式!大家自行解决) 主要内容 模块一 概述 为你建立两个标准。-个是性能调优标准,告诉你可以通过哪些参数去衡量系统性能;另-一个是调优过程标准,带你了解通过哪些严格的调优策略,我们可以排查性能问题,从而解决问题。 模块二 Java 编程性能调优 JDK是Java语言的基础库,熟悉JDK中各个包中的工具类,可以帮助你编写出高性能代码。这里我会从基础的数据类型讲起,涉及容器在实际应用场景中的调优,还有现在互联网系统架构中比较重要的网络通信调优。 03.字符串性能优化不容小觑,百M内存轻松存储几十G数据 05.ArrayList还是LinkedList?使用不当性能差千倍 06.Stream如何提高遍历集合效率? 10.网络通信优化之通信协议:如何优化RPC网络通信? 11.推荐几款常用的性能测试工具 模块三 多线程性能调优 目前大部分服务器都是多核处理器,多线程编程的应用广泛。为了保证线程的安全性,通常会用到同步锁,这会为系统埋下很多隐患;除此之外,还有多线程高并发带来的性能问题,这些都会在这个模块重点讲解。 12.多线程之锁优化(上):深入了解Synchronized同步锁的优化方法 13.多线程之锁优化(中):深入了解Lock 同步锁的优化方法 15.多线程调优(上):哪些操作导致了上下文切换? 17.并发容器的使用:识别不同场景下最优容器 模块四 JVM性能监测及调优 Java 应用程序是运行在JVM之上的,对JVM进行调优可以提升系统性能。这里重点讲解Java对象的创建和回收、内存分配等。 20. 磨刀不误砍柴工:欲知JVM调优先了解JVM内存模型 21.深入JVM即时编译器JIT,优化Java编译 22.如何优化垃圾回收机制? 模块五 设计模式调优 在架构设计中,我们经常会用到-一些设计模式来优化架构设计。这里我将结合一-些复 杂的应用场景,分享设计优化案例。 29.生产者消费者模式:电商库存设计优化 30. 装饰器模式:如何优化电商系统中复杂的商品价格策略? 模块六 数据库性能调优 数据库最容易成为整个系统的性能瓶颈,这里我会重点解析-一些数据库的常用调优方法。 33.MySQL调优之事务:高并发场景下的数据库事务调优 35.记一次线上SQL死锁事故:如何避免死锁? 38.数据库参数设置优化,失之毫厘差之千里 模块七 实战演练场 以上六个模块的内容,都是基于某个点的调优,现在是时候把你前面所学都调动起来了,这里我将带你进入综合性能问题高频出现的应用场景,学习整体调优方法。 41.如何设计更优的分布式锁? 43.如何使用缓存优化系统性能? 最后 作者:努力向上的小芷 链接:https://juejin.cn/post/6920124384027885581 来源:掘金 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
View DetailsJS数据类型转换
显式转换 其他数据类型转换成Number 将其他数据类型的值转化为数值时,有几种方法? Number() parseInt() parseFloat() +/- (一元操作符) 说一说上面方法的异同? 它们都是可以将非数值转化为数值,不同点如下:
1 2 3 4 5 |
1. Number() 可以处理Boolean、null、undefined,Object而parseInt不可以 2. 在处理字符串时 - number能处理数字字符串(正负),包括十六进制的数字以及空字符串; - parseInt是从第一个非空格字符开始解析,直到遇到一个非数字字符串,如果第一个不是数字或者负号,parseInt就会返回NaN 3. parseFloat()与parseInt()类似,+/-与Number()类似 |
规则 除以下情况外能正常转化为数字,其余全为NaN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
1. Boolean值, true--->1 false--->0 2. null--->0 3. 空字符串('')---> 0 4. 数字字符串('123', '1.23', '0xf')---> 123, 1.23, 15 5. 对象则是先调用valueOf方法,看该方法的返回值是否能转化成数字,如果不行则基于这个返回值调用toString方法,再测试改返回值能不能转化成数字 var a = { valueOf: function() { return 'xyxy'; }, toString: function() { return 1; } }; Number(a) // NaN |
其他数据类型转换成String 将其他数据类型的值转化为字符串时,有几种方法? 其他数据类型的值.toString() String(其它数据类型的值) 说一说上面方法的异同?
1 |
null与undefined上没有toString()方法 |
规则 字符串拼接时,加号两边如果出现字符串或者引用类型的值({}、[]、function(){}),都会变成字符串拼接(因为原本应该是把引用类型的值转为数字,但是需要先转为字符串,遇到字符串就直接变成字符串拼接了)
1 2 3 4 |
{} => "[object Object]" function(){} =>"function(){}" {}+0; //0 没有变成字符串拼接的原因:把{}当做块处理了,不参与数学运算 ({}+0); //"[object Object]0" 用括号运算符包起来就是数学运算了 |
1 2 3 4 5 6 |
// 做个题目吧 let result =10 + false +undefined+[]+'Tencent'+null+true+{}; //(10+0+NaN)+''+'Tencent' +'null' +'true' + '[object Object]' //"NaNTencentnulltrue[object Object]" console.log(result) |
其他数据类型转换成Boolean 将其他数据类型的值转化为布尔值时,有几种方法? ! 转换为布尔类型后取反 !! 转换为布尔类型 Boolean 规则?
1 |
只有 0、NaN、空字符串、undefined、null 五个值会变为false,其余都是true |
隐式转换 isNaN、Math方法
1 |
参考显式转换成Number的规则 |
+ 操作
1 2 |
两边的数据有一个是字符串类型则选用字符串拼接操作, 之后才是考虑数学计算的加操作 |
== 「类型一样的几个特殊点」
1 2 3 |
{} == {} : false 说明:对象(引用类型的值)比较的是堆内存的地址 [] == [] : false 同上 NaN == NaN : false |
2.「类型不一样的转换规则」
1 2 3 4 5 6 |
1. null == undefined : true 2. null、undefined和其他任何值都不相等即 在比较相等性之前,不能将null与undefined转换成其他任何职 3. 字符串 == 对象 要把对象转换为字符串 4. 剩下如果==两边数据类型不一致,都是需要转换为数字再进行比较 5.注意点: 虽然Number(null)的结果是0,但是null和0 ==比较时并不是转为数字比较的,即验证3中所说 |
作者:妍等等xy 链接:https://juejin.cn/post/6920236688224550925 来源:掘金 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
View DetailsCSS 奇思妙想边框动画
今天逛博客网站 — shoptalkshow,看到这样一个界面,非常有意思: 觉得它的风格很独特,尤其是其中一些边框。 嘿嘿,所以来一篇边框特辑,看看运用 CSS,可以在边框上整些什么花样。 border 属性 谈到边框,首先会想到 border,我们最常用的莫过于 solid,dashed,上图中便出现了 dashed。 除了最常见的 solid,dashed,CSS border 还支持 none,hidden, dotted, double, groove, ridge, inset, outset 等样式。除去 none,hidden,看看所有原生支持的 border 的样式: 基础的就这些,如果希望实现一个其他样式的边框,或者给边框加上动画,那就需要配合一些其他属性,或是脑洞大开。OK,一起来看看一些额外的有意思的边框。 边框长度变化 先来个比较简单的,实现一个类似这样的边框效果: 这里其实是借用了元素的两个伪元素。两个伪元素分别只设置上、左边框,下、右边框,通过 hover 时改变两个伪元素的高宽即可。非常好理解。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<span class="hljs-selector-tag">div</span> { <span class="hljs-attribute">position</span>: relative; <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#03A9F3</span>; &::before, &::after { <span class="hljs-attribute">content</span>: <span class="hljs-string">""</span>; <span class="hljs-attribute">position</span>: absolute; <span class="hljs-attribute">width</span>: <span class="hljs-number">20px</span>; <span class="hljs-attribute">height</span>: <span class="hljs-number">20px</span>; } &<span class="hljs-selector-pseudo">::before</span> { <span class="hljs-attribute">top</span>: -<span class="hljs-number">5px</span>; <span class="hljs-attribute">left</span>: -<span class="hljs-number">5px</span>; <span class="hljs-attribute">border-top</span>: <span class="hljs-number">1px</span> solid <span class="hljs-built_in">var</span>(--borderColor); <span class="hljs-attribute">border-left</span>: <span class="hljs-number">1px</span> solid <span class="hljs-built_in">var</span>(--borderColor); } &<span class="hljs-selector-pseudo">::after</span> { <span class="hljs-attribute">right</span>: -<span class="hljs-number">5px</span>; <span class="hljs-attribute">bottom</span>: -<span class="hljs-number">5px</span>; <span class="hljs-attribute">border-bottom</span>: <span class="hljs-number">1px</span> solid <span class="hljs-built_in">var</span>(--borderColor); <span class="hljs-attribute">border-right</span>: <span class="hljs-number">1px</span> solid <span class="hljs-built_in">var</span>(--borderColor); } &<span class="hljs-selector-pseudo">:hover</span><span class="hljs-selector-pseudo">::before</span>, &<span class="hljs-selector-pseudo">:hover</span><span class="hljs-selector-pseudo">::after</span> { <span class="hljs-attribute">width</span>: <span class="hljs-built_in">calc</span>(<span class="hljs-number">100%</span> + <span class="hljs-number">9px</span>); <span class="hljs-attribute">height</span>: <span class="hljs-built_in">calc</span>(<span class="hljs-number">100%</span> + <span class="hljs-number">9px</span>); } } <span class="copy-code-btn">复制代码</span> |
CodePen Demo — width border animation 接下来,会开始加深一些难度。 虚线边框动画 使用 dashed 关键字,可以方便的创建虚线边框。
1 2 3 4 5 |
<span class="hljs-selector-tag">div</span> { <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> dashed <span class="hljs-number">#333</span>; } <span class="copy-code-btn">复制代码</span> |
当然,我们的目的是让边框能够动起来。使用 dashed 关键字是没有办法的。但是实现虚线的方式在 CSS 中有很多种,譬如渐变就是一种很好的方式:
1 2 3 4 5 6 |
<span class="hljs-selector-tag">div</span> { <span class="hljs-attribute">background</span>: <span class="hljs-built_in">linear-gradient</span>(<span class="hljs-number">90deg</span>, #<span class="hljs-number">333</span> <span class="hljs-number">50%</span>, transparent <span class="hljs-number">0</span>) repeat-x; <span class="hljs-attribute">background-size</span>: <span class="hljs-number">4px</span> <span class="hljs-number">1px</span>; <span class="hljs-attribute">background-position</span>: <span class="hljs-number">0</span> <span class="hljs-number">0</span>; } <span class="copy-code-btn">复制代码</span> |
看看,使用渐变模拟的虚线如下: 好,渐变支持多重渐变,我们把容器的 4 个边都用渐变表示即可:
1 2 3 4 5 6 7 8 9 10 |
<span class="hljs-selector-tag">div</span> { <span class="hljs-attribute">background</span>: <span class="hljs-built_in">linear-gradient</span>(<span class="hljs-number">90deg</span>, #<span class="hljs-number">333</span> <span class="hljs-number">50%</span>, transparent <span class="hljs-number">0</span>) repeat-x, <span class="hljs-built_in">linear-gradient</span>(<span class="hljs-number">90deg</span>, #<span class="hljs-number">333</span> <span class="hljs-number">50%</span>, transparent <span class="hljs-number">0</span>) repeat-x, <span class="hljs-built_in">linear-gradient</span>(<span class="hljs-number">0deg</span>, #<span class="hljs-number">333</span> <span class="hljs-number">50%</span>, transparent <span class="hljs-number">0</span>) repeat-y, <span class="hljs-built_in">linear-gradient</span>(<span class="hljs-number">0deg</span>, #<span class="hljs-number">333</span> <span class="hljs-number">50%</span>, transparent <span class="hljs-number">0</span>) repeat-y; <span class="hljs-attribute">background-size</span>: <span class="hljs-number">4px</span> <span class="hljs-number">1px</span>, <span class="hljs-number">4px</span> <span class="hljs-number">1px</span>, <span class="hljs-number">1px</span> <span class="hljs-number">4px</span>, <span class="hljs-number">1px</span> <span class="hljs-number">4px</span>; <span class="hljs-attribute">background-position</span>: <span class="hljs-number">0</span> <span class="hljs-number">0</span>, <span class="hljs-number">0</span> <span class="hljs-number">100%</span>, <span class="hljs-number">0</span> <span class="hljs-number">0</span>, <span class="hljs-number">100%</span> <span class="hljs-number">0</span>; } <span class="copy-code-btn">复制代码</span> |
效果如下: OK,至此,我们的虚线边框动画其实算是完成了一大半了。虽然 border-style: dashed 不支持动画,但是渐变支持呀。我们给上述 div 再加上一个 hover 效果,hover 的时候新增一个 animation 动画,改变元素的 background-position 即可。
1 2 3 4 5 6 7 8 9 10 |
<span class="hljs-selector-tag">div</span><span class="hljs-selector-pseudo">:hover</span> { <span class="hljs-attribute">animation</span>: linearGradientMove .<span class="hljs-number">3s</span> infinite linear; } <span class="hljs-keyword">@keyframes</span> linearGradientMove { 100% { <span class="hljs-attribute">background-position</span>: <span class="hljs-number">4px</span> <span class="hljs-number">0</span>, -<span class="hljs-number">4px</span> <span class="hljs-number">100%</span>, <span class="hljs-number">0</span> -<span class="hljs-number">4px</span>, <span class="hljs-number">100%</span> <span class="hljs-number">4px</span>; } } <span class="copy-code-btn">复制代码</span> |
OK,看看效果,hover 上去的时候,边框就能动起来,因为整个动画是首尾相连的,无限循环的动画看起来就像是虚线边框在一直运动,这算是一个小小的障眼法或者小技巧: 这里还有另外一个小技巧,如果我们希望虚线边框动画是从其他边框,过渡到虚线边框,再行进动画。完全由渐变来模拟也是可以的,如果想节省一些代码,使用 border 会更快一些,譬如这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span class="hljs-selector-tag">div</span> { <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#333</span>; &:hover { <span class="hljs-attribute">border</span>: none; <span class="hljs-attribute">background</span>: <span class="hljs-built_in">linear-gradient</span>(<span class="hljs-number">90deg</span>, #<span class="hljs-number">333</span> <span class="hljs-number">50%</span>, transparent <span class="hljs-number">0</span>) repeat-x, <span class="hljs-built_in">linear-gradient</span>(<span class="hljs-number">90deg</span>, #<span class="hljs-number">333</span> <span class="hljs-number">50%</span>, transparent <span class="hljs-number">0</span>) repeat-x, <span class="hljs-built_in">linear-gradient</span>(<span class="hljs-number">0deg</span>, #<span class="hljs-number">333</span> <span class="hljs-number">50%</span>, transparent <span class="hljs-number">0</span>) repeat-y, <span class="hljs-built_in">linear-gradient</span>(<span class="hljs-number">0deg</span>, #<span class="hljs-number">333</span> <span class="hljs-number">50%</span>, transparent <span class="hljs-number">0</span>) repeat-y; <span class="hljs-attribute">background-size</span>: <span class="hljs-number">4px</span> <span class="hljs-number">1px</span>, <span class="hljs-number">4px</span> <span class="hljs-number">1px</span>, <span class="hljs-number">1px</span> <span class="hljs-number">4px</span>, <span class="hljs-number">1px</span> <span class="hljs-number">4px</span>; <span class="hljs-attribute">background-position</span>: <span class="hljs-number">0</span> <span class="hljs-number">0</span>, <span class="hljs-number">0</span> <span class="hljs-number">100%</span>, <span class="hljs-number">0</span> <span class="hljs-number">0</span>, <span class="hljs-number">100%</span> <span class="hljs-number">0</span>; } } <span class="copy-code-btn">复制代码</span> |
由于 border 和 background 在盒子模型上位置的差异,视觉上会有一个很明显的错位的感觉: 要想解决这个问题,我们可以把 border 替换成 outline,因为 outline […]
View Detailsnpm ERR! Unexpected end of JSON input while parsing near
You are trying to create a new angular, react, vue app, and you end up with an error message saying: npm ERR! Unexpected end of JSON input while parsing near There’s a high chance that your npm cache been damaged. Try:
1 |
npm cache clean --force |
If you are a windows user, try deleting all files in this folder:
1 |
C:\Users{{your-username}}\AppData\Roaming\npm-cache |
Then…
1 |
npm cache verify |
If that doesn’t work, try updating to the lastest (understand the risks)
1 |
npm i npm@latest -g |
I hope this helps! from:https://dev.to/rishiabee/npm-err-unexpected-end-of-json-input-while-parsing-near-743
View DetailsVue 魔法师 —— 将 API “类化”
前言 【类】和【接口】是 JavaScript 未来的方向,我们的 API 调用也将如此,朝着这个方向和业务模块同步分类、同步升级。本文讲的正是 —— Vue 魔法师,如何将 API “类化”? 万物皆模块,万物可归类。闲言少叙,直接正题。 分类 API 环境: Vue2+ 或 Vue3+ 咱这里直接 VueCLI 迅速快捷构建
1 |
vue create vue-api-module |
得到如下目录
1 2 3 4 5 |
--src ----store ------index.js ----App.vue ----main.js |
然后新建文件 api.js 如下,
1 2 3 4 5 6 7 |
--src ----services ------api.js ----store ------index.js ----App.vue ----main.js |
基础类 API 我们先创建一个基础类如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class BaseApiService { baseUrl = "https://jsonplaceholder.typicode.com"; resource; constructor(resource) { if (!resource) throw new Error("Resource is not provided"); this.resource = resource; } getUrl(id = "") { return `${this.baseUrl}/${this.resource}/${id}`; } handleErrors(err) { // 处理错误: console.log({ message: "Errors is handled here", err }); } } |
baseUrl:设置请求根路径; resource:来源; getUrl:获取链接函数,包含 baseUrl、resource、id; handleErrors:处理错误函数; 只读类 API 然后,我们再创建一个子类:包含 fetch、get 只读方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class ReadOnlyApiService extends BaseApiService { constructor(resource) { super(resource); } async fetch(config = {}) { try { const response = await fetch(this.getUrl(), config); return await response.json(); } catch (err) { this.handleErrors(err); } } async get(id) { try { if (!id) throw Error("Id 不合法"); const response = await fetch(this.getUrl(id)); return await response.json(); } catch (err) { this.handleErrors(err); } } } |
fetch:获取数据; get:通过 id 获取数据; 写入类 API 接着,我们再创建一个包含可读写方法的子类:post、put、delete
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
class ModelApiService extends ReadOnlyApiService { constructor(resource) { super(resource); } async post(data = {}) { try { const response = await fetch(this.getUrl(), { method: "POST", body: JSON.stringify(data) }); const { id } = response.json(); return id; } catch (err) { this.handleErrors(err); } } async put(id, data = {}) { if (!id) throw Error("Id 不合法"); try { const response = await fetch(this.getUrl(id), { method: "PUT", body: JSON.stringify(data) }); const { id: responseId } = response.json(); return responseId; } catch (err) { this.handleErrors(err); } } async delete(id) { if (!id) throw Error("Id 不合法"); try { await fetch(this.getUrl(id), { method: "DELETE" }); return true; } catch (err) { this.handleErrors(err); } } } |
post:创建数据; put:更新数据; delete:删除数据; 继承 让我们看看两个简单的继承示例:
1 2 3 4 5 |
class UsersApiService extends ReadOnlyApiService { constructor() { super("users"); } } |
1 2 3 4 5 |
class PostsApiService extends ModelApiService { constructor() { super("posts"); } } |
【UsersApiService 类】继承了只读类 API —— ReadOnlyApiService,可以使用 fetch、get 两种方法。而 【PostsApiService 类】继承了读写类 API —— ModelApiService,可以使用 fetch、get、post、put、delete 五种方法。 我们也可以根据业务来写继承 API 类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class AlbumsApiService extends ModelApiService { constructor() { super("albums"); } async uploadImage() { /* 这里可以写你的上传图片逻辑 */ console.log({ message: "图片上传成功!" }); return true; } async triggerError() { try { throw Error(" API 模块调用错误!"); } catch (err) { this.handleErrors(err); } } } |
导出 我们在 api.js 导出这些 […]
View DetailsRGB颜色参考
实色效果 英文名称 R.G.B 16色 实色效果 英文名称 R.G.B 16色 Snow 255 250 250 #FFFAFA PaleTurquoise1 187 255 255 #BBFFFF GhostWhite 248 248 255 #F8F8FF PaleTurquoise2 174 238 238 #AEEEEE WhiteSmoke 245 245 245 #F5F5F5 PaleTurquoise3 150 205 205 #96CDCD Gainsboro 220 220 220 #DCDCDC PaleTurquoise4 102 139 139 #668B8B FloralWhite 255 250 240 #FFFAF0 CadetBlue1 152 245 255 #98F5FF OldLace 253 245 230 #FDF5E6 CadetBlue2 142 229 238 #8EE5EE Linen 250 240 230 #FAF0E6 CadetBlue3 122 197 205 #7AC5CD AntiqueWhite 250 235 215 #FAEBD7 CadetBlue4 83 134 139 #53868B […]
View Detailsc# base64转字符串
转成 Base64 形式的 System.String:
1 2 3 4 5 |
string a = "base64字符串与普通字符串互转"; byte[] b = System.Text.Encoding.Default.GetBytes(a); //转成 Base64 形式的 System.String a = Convert.ToBase64String(b); Response.Write(a); |
转回到原来的 System.String:
1 2 3 |
byte[] c = Convert.FromBase64String(a); a = System.Text.Encoding.Default.GetString(c); Response.Write(a);<br> |
from:https://www.cnblogs.com/daimaxuejia/p/12893207.html
View Detailsjs实现保存文件到本地
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function fake_click(obj) { var ev = document.createEvent("MouseEvents"); ev.initMouseEvent( "click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null ); obj.dispatchEvent(ev); } function download(name, data) { var urlObject = window.URL || window.webkitURL || window; var downloadData = new Blob([data]); var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a") save_link.href = urlObject.createObjectURL(downloadData); save_link.download = name; fake_click(save_link); } //调用方法 download("save.txt","内容"); |
from:https://www.cnblogs.com/frost-yen/p/10226705.html
View Detailsc# 确定dynamic类型的数据对象是否存在某个属性
1 2 3 4 5 6 |
public static bool IsPropertyExist(dynamic data, string propertyname) { if (data is ExpandoObject) return ((IDictionary<string, object>)data).ContainsKey(propertyname); return data.GetType().GetProperty(propertyname) != null; } |
https://www.cnblogs.com/94cool/p/8135579.html
View Detailsjs解析websocket二进制数据包
js解释数据包 做一个项目,服务器要给我发一个数据包,格式为,前面16位short,后32位int,后面就全是string,我用javascript怎么去把这个数据包解读出来? 用WebSocket,接收到的就是一个Blob对象.而我要给服务器返回的也是这种格式,我又怎么把我的数据封装成这样的数据包? ——解决方案——————--
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
var reader = { readAs: function(type,blob,cb){ var r = new FileReader(); r.onloadend = function(){ if(typeof(cb) === 'function') { cb.call(r,r.result); } } try{ r['readAs'+type](blob); }catch(e){} } } function parseBlob(blob){ var shortVar, intVar, stringVar; reader.readAs('ArrayBuffer',blob.slice(0,2),function(arr){ shortVar = (new Int16Array(arr))[0]; console.log(shortVar); }); reader.readAs('ArrayBuffer',blob.slice(2,6),function(arr){ intVar = (new Int32Array(arr))[0]; console.log(intVar); }); reader.readAs('Text',blob.slice(6,blob.size,'text/plain;charset=UTF-8'),function(result){ stringVar = result; console.log(stringVar); }); } var buffer = new ArrayBuffer(6);//建立6个字节的缓冲,前两个字节是short,后四个是int var bufferView = new Int16Array(buffer); //建立16位的视图,那么视图的长度应该是3 bufferView[0] = 32767; bufferView[1] = 0; bufferView[2] = 1; //现在buffer中的内容由低位到高位应该是 //11111111 11111110 00000000 00000000 100000000 00000000 var blob = new Blob([bufferView,"words words 文本文本文本文本"]); //构造这个blob类型 //测试一下parseBlob函数是否正确 parseBlob(blob); //32767 //65536 //words words 文本文本文本文本 //第一个short是32767 //第二个int,前16位为0,第17位为1,所以结果是65536 //第三个字符串,和我们构造blob的时候一样 |
from:https://blog.csdn.net/woxingwosu0100/article/details/51273523/
View Details