亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解vue中使用protobuf踩坑記

 更新時間:2019年05月07日 09:14:26   作者:小緦lv  
這篇文章主要介紹了vue中使用protobuf踩坑記,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

官方解釋為:

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the "old" format.

翻譯是(機翻---我英語不好)

協(xié)議緩沖區(qū)是用于序列化結(jié)構(gòu)化數(shù)據(jù)的靈活,高效的自動化機制 - 思考XML,但更小,更快,更簡單。您可以定義一次數(shù)據(jù)的結(jié)構(gòu),然后您可以使用特殊的源代碼輕松地將結(jié)構(gòu)化數(shù)據(jù)寫入各種數(shù)據(jù)流并使用各種語言讀取和讀取數(shù)據(jù)。您甚至可以更新您的數(shù)據(jù)結(jié)構(gòu),而不會中斷根據(jù)“舊”格式編譯的已部署程序。

特點:

  • 更簡單
  • 是3到10倍小
  • 速度要快20到100倍
  • 不太模糊
  • 生成更易于以編程方式使用的數(shù)據(jù)訪問類

代碼

在github上寫了個demo demo地址 有需要的可以下載下來跑一下就理解了。PS:如果覺得有用 請給我個小星星 (筆芯~)

使用

其實最開始我嘗試使用一個第三方JSprotobuf.js protobuf.load 的時候瀏覽器報了個錯illegal token '<' (/demo.proto, line 1) 查找了下官網(wǎng)issue,大意應該是proto文件多了個字符,但是我查看過proto文件并沒有發(fā)現(xiàn)有多的'<',怎么辦呢,最后放棄使用第三方。用官方提供的方法。

下載protobuf編譯器

下載地址 (我下載的是3.40版) github也提供了zip包,可自行下載 (目前最新版本是v3.6.0) 用來編譯proto為JS文件方便調(diào)用

配置環(huán)境變量

由于公司用的是win10 只需要將下載的文件地址添加到path即可 Mac與window命令唯一的區(qū)別就是需要將protoc改成protoc.exe 前提是需要添加環(huán)境變量

編寫proto文件

為了確保前后一致,下面是后臺寫給我的一個測試proto,我司后臺是java

syntax = "proto2";//protobuf版本
option java_package = "com.test.protobuf";
option java_outer_classname = "PersonMessage";
message Person {
 required int32 id = 1;
 optional string name = 2;
 optional string email = 3;
 repeated string list = 4;
 extensions 100 to 1000;//允許擴展的ID
}

message PersonTree {
 optional string id = 1;
 optional string title = 2;
 repeated PersonTree childs = 3;
}

extend Person {
 optional int32 count = 101;
 optional int32 likes_cnt= 102;
}

message PersonEx {
 optional int32 id = 1;
 extend Person {
  optional int32 px = 103;
  optional int32 py= 104;
 }
 optional Person p = 2;
}

使用vue-cli構(gòu)建一個工程目錄

npm install -g vue-cli
vue init webpack my-project
cd my-project
npm install
npm run dev

安裝插件: npm install axios element-ui google-protobuf --save

編譯proto為JS

進入 awesome.proto 的存放路徑 使用如下命令 protoc.exe --js_out=import_style=commonjs,binary:. awesome.proto

  • 會生成一個awesome_pb.js文件
  • 點擊查看awesome_pb.js其實可以看到里面是生成好的方法。只需要在頁面中引入JS調(diào)用即可

之后我們將這個文件引入頁面,當然你也可以考慮全局引用

測試

本地測試

編寫一個測試頁面,創(chuàng)建一個測試按鈕 我是在測試頁面 import messages from './awesome_pb.js' 方法為:

methods: {
  protobufferTest () {
   var message = new messages.Person() // 調(diào)用Person對象 實例化
   // 賦值
   message.setId(23)
   message.setName('asd')
   // 序列化
   var bytes = message.serializeBinary()

   console.log(bytes) // Uint8Array(7) [8, 23, 18, 3, 97, 115, 100]

   // 反序列化
   var message2 = messages.Person.deserializeBinary(bytes)

   console.log(message2) // proto.PersonTree {wrappers_: null, messageId_: undefined, arrayIndexOffset_: -1, array: Array(3), pivot_: 1.7976931348623157e+308, …}

  }
 }

到此,本地測試完成,沒什么毛病了。

前后端聯(lián)調(diào)測試

前方有坑

前后傳輸是使用的FormData,然后悲劇的事情來了。后臺解析不了。查看了下數(shù)據(jù) [8, 23, 18, 3, 97, 115, 100] 確實是傳過去了。

后來排查出原因是應該是解析成了字符串,然后數(shù)值變了。所以解析不出來。 后來使用fromCharCode()方法編輯成字符串形式傳輸給后臺。在使用charCodeAt()取值。

此方法已棄用
protobufferTest () {
   var message = new messages.Person() // 調(diào)用Person對象 實例化
   // 賦值
   message.setId(23)
   message.setName('asd')
   // 序列化
   var bytes = message.serializeBinary()

   console.log(bytes) // Uint8Array(7) [8, 23, 18, 3, 97, 115, 100]

   var tests = ''
   for (let index = 0; index < bytes.length; index++) {
    tests += String.fromCharCode(bytes[index])
   }
   console.log(tests) // asd

   // 存入FormData
   let uploadDatas = new FormData()
   uploadDatas.append('protobuf', tests)

   // 使用axios傳輸給后臺
   this.axios.post('/test', uploadDatas)
    .then(function (response) {
     // 將傳回的字符串轉(zhuǎn)為數(shù)組
     console.log(response.data.split('')) // ["↵", "", "3", "2", "", "", "a", "s", "d", "f"]
     let str = response.data.split('')
     let toChar = []
     for (let index = 0; index < str.length; index++) {
      toChar.push(str[index].charCodeAt())
     }
     console.log(toChar) // [10, 2, 51, 50, 18, 4, 97, 115, 100, 102]

     // 后臺傳回來的是PersonTree里面的值所以調(diào)用PersonTree來反序列化
     var message2 = messages.PersonTree.deserializeBinary(toChar)

     console.log(message2) // proto.PersonTree {wrappers_: null, messageId_: undefined, arrayIndexOffset_: -1, array: Array(3), pivot_: 1.7976931348623157e+308, …}

     // 獲取PersonTree的id值
     console.log(message2.getId()) // 32
    })
    .catch(function (error) {
     console.log(error)
    })

  }

以上方法可能存在安全隱患。 向后端傳值 因為FormData支持兩種方式傳輸string和blob所以將bytes存入blob中 前端獲取數(shù)據(jù) 對axios的默認傳輸方式做個更改 axios.defaults.responseType = 'arraybuffer' 將以上的JS代碼更改為以下內(nèi)容

protobufferTest () {
   var message = new messages.Person()
   message.setId(23)
   message.setName('asd')
   var bytes = message.serializeBinary()
   
   console.log(bytes)
   let uploadDatas = new FormData()
   var blob = new Blob([bytes], {type: 'application/octet-stream'})

   uploadDatas.append('protobuf', blob)
   
   this.axios.post('/test', uploadDatas)
    .then(function (response) {
     console.log(response)

     var message2 = messages.PersonTree.deserializeBinary(response.data)
     console.log(message2.getId())
    })
    .catch(function (error) {
     console.log(error)
    })
   // console.log(bytes)
  }

至此前后聯(lián)調(diào)完成

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue中子組件傳遞數(shù)據(jù)給父組件的講解

    vue中子組件傳遞數(shù)據(jù)給父組件的講解

    今天小編就為大家分享一篇關(guān)于vue中子組件傳遞數(shù)據(jù)給父組件的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • vue?router如何實現(xiàn)tab切換

    vue?router如何實現(xiàn)tab切換

    這篇文章主要介紹了vue?router如何實現(xiàn)tab切換,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue零基礎(chǔ)入門之模板語法與數(shù)據(jù)綁定及Object.defineProperty方法詳解

    Vue零基礎(chǔ)入門之模板語法與數(shù)據(jù)綁定及Object.defineProperty方法詳解

    這篇文章主要介紹了Vue初學基礎(chǔ)中的模板語法、數(shù)據(jù)綁定、Object.defineProperty方法等基礎(chǔ),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-09-09
  • vue使用axios的小技巧分享

    vue使用axios的小技巧分享

    這篇文章主要為大家詳細介紹了一些vue使用axios的小技巧,文中的示例代碼講解詳細,對我們深入掌握vue有一定的幫助,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-10-10
  • 解決vue項目,npm run build后,報路徑錯的問題

    解決vue項目,npm run build后,報路徑錯的問題

    這篇文章主要介紹了解決vue項目,npm run build后,報路徑錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Vue項目保持element組件同行,設(shè)置組件不自動換行問題

    Vue項目保持element組件同行,設(shè)置組件不自動換行問題

    這篇文章主要介紹了Vue項目保持element組件同行,設(shè)置組件不自動換行問題。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Vue對象的組成和掛載方式詳解

    Vue對象的組成和掛載方式詳解

    這篇文章主要介紹了Vue對象的基本組成和Vue對象掛載的幾種方式,文中通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07
  • Vue3在css中使用v-bind綁定js/ts變量(在scss和less中使用方式)

    Vue3在css中使用v-bind綁定js/ts變量(在scss和less中使用方式)

    v-bind是Vue.js中的一個核心指令,用于在Vue組件或DOM元素上綁定數(shù)據(jù)屬性,下面這篇文章主要給大家介紹了關(guān)于Vue3在css中使用v-bind綁定js/ts變量的相關(guān)資料,也可以在scss和less中使用方式,需要的朋友可以參考下
    2024-04-04
  • Vue 實現(xiàn)復制功能,不需要任何結(jié)構(gòu)內(nèi)容直接復制方式

    Vue 實現(xiàn)復制功能,不需要任何結(jié)構(gòu)內(nèi)容直接復制方式

    今天小編就為大家分享一篇Vue 實現(xiàn)復制功能,不需要任何結(jié)構(gòu)內(nèi)容直接復制方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue中自定義指令directive的詳細指南

    vue中自定義指令directive的詳細指南

    這篇文章主要給大家介紹了關(guān)于vue中自定義指令directive的相關(guān)資料,自定義指令解決的問題或者說使用場景是對普通 DOM 元素進行底層操作,所以我們不能盲目的胡亂的使用自定義指令,需要的朋友可以參考下
    2021-09-09

最新評論