国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微信小程序websocket使用protobuf,發(fā)送arraybuffer

這篇具有很好參考價(jià)值的文章主要介紹了微信小程序websocket使用protobuf,發(fā)送arraybuffer。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

??砥礪前行,不負(fù)余光,永遠(yuǎn)在路上??

前言

這是一次繼前文websocket的一個(gè)更新:小程序中使用websocket,區(qū)分房間、空間 現(xiàn)在遇到了一個(gè)需求是小程序接入 io-game 的websocket 和unity 游戲端同時(shí)使用一個(gè)websocket,io-game那邊收發(fā)websocket消息都是采用 Protobuf 處理的。

一、如何在小程序websocket中使用 Protobuf 發(fā)送buffer

參考項(xiàng)目:https://github.com/Zhang19910325/protoBufferForWechat/tree/master

二、使用過程遇到的坑(版本問題)

1、需要注意下Protobuf版本 使用 protobufjs@6.8.6最好,我在使用的時(shí)候安裝7.多 莫名奇妙 pbjs 用不起

微信小程序websocket使用protobuf,發(fā)送arraybuffer,微信小程序,websocket,小程序

cnpm install -g protobufjs@6.8.6

然后執(zhí)行pbjs即可
微信小程序websocket使用protobuf,發(fā)送arraybuffer,微信小程序,websocket,小程序

2、websocket中發(fā)送 buffer

支持string和arraybuffer類型,所以把Uint8Array直接轉(zhuǎn)換為arraybuffer

new Uint8Array([...buffer]).buffer

三、完整步驟

1、下載protoBufferForWechat 導(dǎo)入到項(xiàng)目中

git clone https://github.com/Zhang19910325/protoBufferForWechat.git 

2、安裝pbjs工具6.8.6

cnpm install -g protobufjs@6.8.6
//or
yarn add globle protobufjs@6.8.6

3、驗(yàn)證是否安裝成功

執(zhí)行pbjs出現(xiàn)如下信息即可

protobuf.js v6.7.0 CLI for JavaScript

Translates between file formats and generates static code.

  -t, --target     Specifies the target format. Also accepts a path to require a custom target.

                   json          JSON representation
                   json-module   JSON representation as a module
                   proto2        Protocol Buffers, Version 2
                   proto3        Protocol Buffers, Version 3
                   static        Static code without reflection (non-functional on its own)
                   static-module Static code without reflection as a module

  -p, --path       Adds a directory to the include path.

  -o, --out        Saves to a file instead of writing to stdout.

  --sparse         Exports only those types referenced from a main file (experimental).

  Module targets only:

  -w, --wrap       Specifies the wrapper to use. Also accepts a path to require a custom wrapper.

                   default   Default wrapper supporting both CommonJS and AMD
                   commonjs  CommonJS wrapper
                   amd       AMD wrapper
                   es6       ES6 wrapper (implies --es6)
                   closure   A closure adding to protobuf.roots where protobuf is a global

  --dependency     Specifies which version of protobuf to require. Accepts any valid module id

  -r, --root       Specifies an alternative protobuf.roots name.

  -l, --lint       Linter configuration. Defaults to protobuf.js-compatible rules:

                   eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins

  --es6            Enables ES6 syntax (const/let instead of var)

  Proto sources only:

  --keep-case      Keeps field casing instead of converting to camel case.

  Static targets only:

  --no-create      Does not generate create functions used for reflection compatibility.
  --no-encode      Does not generate encode functions.
  --no-decode      Does not generate decode functions.
  --no-verify      Does not generate verify functions.
  --no-convert     Does not generate convert functions like from/toObject
  --no-delimited   Does not generate delimited encode/decode functions.
  --no-beautify    Does not beautify generated code.
  --no-comments    Does not output any JSDoc comments.

  --force-long     Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.
  --force-number   Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.
  --force-message  Enfores the use of message instances instead of plain objects.

usage: pbjs [options] file1.proto file2.json ...  (or pipe)  other | pbjs [options] -

4、轉(zhuǎn)換proto文件

例如:我的Req_LoginVerify.proto文件

syntax = "proto3";

// {classComment}
message Req_LoginVerify {
  // 用戶id
  int64 userId = 1;
  // 場次id
  int64 sessionId = 2;
  // 房間總?cè)藬?shù)
  int32 roomCountNum = 3;

}

運(yùn)行之后會生成一個(gè) Req_LoginVerify.json文件

pbjs -t json Req_LoginVerify.proto > Req_LoginVerify.json

內(nèi)容為:

{
  "nested": {
    "Req_LoginVerify": {
      "fields": {
        "userId": {
          "type": "int64",
          "id": 1
        },
        "sessionId": {
          "type": "int64",
          "id": 2
        },
        "roomCountNum": {
          "type": "int32",
          "id": 3
        }
      }
    }
  }
}

但此時(shí)的json文件我們不能直接使用,不過我們可以將json對象取出放到小程序項(xiàng)目當(dāng)中去,比如在小程序項(xiàng)目中新建一個(gè)Req_LoginVerify.js,內(nèi)容為

module.exports = {
        "nested": {
                "Req_LoginVerify": {
                        "fields": {
                                "userId": {
                                        "type": "int64",
                                        "id": 1
                                },
                                "sessionId": {
                                        "type": "int64",
                                        "id": 2
                                },
                                "roomCountNum": {
                                        "type": "int32",
                                        "id": 3
                                }
                        }
                }
        }
}

5、最后使用

注意我的文件結(jié)構(gòu):

var protobuf = require('../../weichatPb/protobuf.js');
var loginConfig = require('../../proto/Req_LoginVerify');//加載awesome.proto對應(yīng)的json
var Login = protobuf.Root.fromJSON(loginConfig);
var LoginMsg = Login.lookupType("Req_LoginVerify");//這就是我們的Message類

微信小程序websocket使用protobuf,發(fā)送arraybuffer,微信小程序,websocket,小程序

6、websocket中發(fā)送buffer

sendSocketMessage: function () {
                if (this.data.socketOpen) {
                        var payload = {
                                userId: 1,
                                sessionId: 12,
                                roomCountNum: 6
                        };
                        var message = LoginMsg.create(payload);
                        var buffer = LoginMsg.encode(message).finish();
                        console.log("buffer", buffer);
                        wx.sendSocketMessage({
                                data: new Uint8Array([...buffer]).buffer
                        })
                }
        },

7、處理服務(wù)端返回的buffer

wx.onSocketMessage(function (res) {
        console.log('接收到服務(wù)器發(fā)送的原始數(shù)據(jù):', res.data)
        var deMessage = LoginMsg.decode(res.data);
        console.log("解析buffer之后的數(shù)據(jù)", deMessage);

})

四、小程序中的效果

微信小程序websocket使用protobuf,發(fā)送arraybuffer,微信小程序,websocket,小程序
微信小程序websocket使用protobuf,發(fā)送arraybuffer,微信小程序,websocket,小程序文章來源地址http://www.zghlxwxcb.cn/news/detail-617155.html

到了這里,關(guān)于微信小程序websocket使用protobuf,發(fā)送arraybuffer的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • uniapp vue3 微信小程序 項(xiàng)目中使用 websocet、微信小程序真機(jī)調(diào)試 websocket 報(bào)錯(cuò) errMsg: “Invalid HTTP status.“

    uniapp-websocket官方文檔 注意點(diǎn):需要在確定建立連接后才能去發(fā)送數(shù)據(jù) 這個(gè)錯(cuò)誤通常是因?yàn)樾〕绦?WebSocket 請求的地址沒有配置為 HTTPS,而是使用了 HTTP,因此需要注意以下幾點(diǎn): 小程序開發(fā)者工具可以支持使用 ws:// 前綴的 WebSocket 地址,但在真機(jī)上會因?yàn)椴话踩脑蚨鵁o

    2024年01月17日
    瀏覽(45)
  • 【微信小程序】使用 WebSocket 進(jìn)行訂閱操作、連接監(jiān)聽、接收到服務(wù)器的消息事件

    在微信小程序中使用 WebSocket 進(jìn)行訂閱操作,可以通過 wx.connectSocket 方法創(chuàng)建 WebSocket 連接,并通過相關(guān)事件處理函數(shù)進(jìn)行訂閱和數(shù)據(jù)處理。 以下是一個(gè)示例代碼,演示了在微信小程序中使用 WebSocket 進(jìn)行訂閱: 在上述代碼中,我們首先使用 wx.connectSocket 方法創(chuàng)建 WebSocket 連接

    2024年02月16日
    瀏覽(49)
  • 【微信小程序】使用 wx.request 方法來發(fā)送POST網(wǎng)絡(luò)請求,攜帶RequestBody參數(shù)

    在微信小程序中,你可以使用 wx.request 方法來發(fā)送網(wǎng)絡(luò)請求。以下是將上述 Java 代碼轉(zhuǎn)換為微信小程序版本的示例: 在上述代碼中,我們使用 wx.request 方法發(fā)送 POST 請求,并將請求的 URL、請求體數(shù)據(jù)、請求頭等信息進(jìn)行相應(yīng)的設(shè)置。請求成功后,會在回調(diào)函數(shù)的 success 中處

    2024年02月15日
    瀏覽(31)
  • 微信小程序如何使用原生Websocket api與Asp.Net Core SignalR 通信

    微信小程序如何使用原生Websocket api與Asp.Net Core SignalR 通信

    如題,這可能算是.net 做小程序的服務(wù)端時(shí),繞不開的一個(gè)問題,老生常談了。同樣的問題,我記得我2018/19年的一個(gè)項(xiàng)目的解決方案是: 修改官方的SignalR.js的客戶端 :把里面用到瀏覽器的Websocket改成微信小程序的官方api的。目前網(wǎng)上也有不少這樣的方案,已經(jīng)改好開源了;

    2024年02月09日
    瀏覽(39)
  • 微信小程序 -- 微信小程序發(fā)送紅包

    微信小程序 -- 微信小程序發(fā)送紅包

    參考鏈接:微信小程序 – 微信小程序發(fā)送紅包 組織參數(shù) 點(diǎn)擊查看微信的文檔 發(fā)送示例 拼接參數(shù)(ASCII碼從小到大排序(字典序)) 加密參數(shù) 將參數(shù)實(shí)體轉(zhuǎn)換成xml格式 最后就是發(fā)送請求 相信發(fā)送post請求大家都不陌生了吧 最后就是對微信返回的參數(shù)進(jìn)行解析,建議遇到問

    2024年02月10日
    瀏覽(16)
  • 【微信小程序】使用WxNotificationCenter實(shí)現(xiàn)復(fù)雜的事件通信功能,在任意頁面中訂閱事件、發(fā)送事件和取消訂閱事件

    在微信小程序中,如果需要實(shí)現(xiàn)復(fù)雜的事件通信功能,可以使用第三方庫來輔助實(shí)現(xiàn)。以下是一些常用的第三方庫示例: WxNotificationCenter Github地址(https://github.com/icindy/WxNotificationCenter) WxNotificationCenter是一個(gè)基于發(fā)布/訂閱模式的事件通知庫,可以在微信小程序中實(shí)現(xiàn)跨頁面

    2024年02月16日
    瀏覽(36)
  • 微信小程序全局websocket

    全篇干貨無廢話 實(shí)現(xiàn)微信小程序全局websocket 含掉線重連,心跳?;畹葯C(jī)制,可做參考示例 app.js

    2024年02月09日
    瀏覽(23)
  • 微信小程序?qū)崿F(xiàn)發(fā)送短信的功能(發(fā)送短信)

    微信小程序?qū)崿F(xiàn)發(fā)送短信的功能(發(fā)送短信)

    我使用的是微信小程序的云開發(fā)這種方式來實(shí)現(xiàn)的,純前端操作,無需后端接入。 1,打開微信公眾平臺中的【云開發(fā)】 ?2,在概覽里面點(diǎn)擊開通靜態(tài)網(wǎng)站 ?3,點(diǎn)擊開通 ?4,確定開通,這地方看上去是要收費(fèi)的,但是第一個(gè)月是有免費(fèi)的額度給你使用的,后期收不收費(fèi)要通

    2024年02月09日
    瀏覽(91)
  • 微信小程序:發(fā)送小程序訂閱消息

    微信小程序:發(fā)送小程序訂閱消息

    文檔:小程序訂閱消息(用戶通過彈窗訂閱)開發(fā)指南 在微信公眾平臺(https://mp.weixin.qq.com)手動配置獲取模板 ID 2.1、獲取消息下發(fā)權(quán)限 文檔:一次性訂閱消息、長期訂閱消息 示例代碼 這里需要注意一個(gè)坑,如果用戶未授權(quán),需要引導(dǎo)用戶打開設(shè)置手動設(shè)置 2.2、獲取登錄憑

    2024年01月25日
    瀏覽(21)
  • 微信小程序發(fā)送消息推送

    微信小程序發(fā)送消息推送

    在小程序開發(fā)中,如果想實(shí)現(xiàn):用戶發(fā)給小程序的消息以及開發(fā)者需要的事件推送,在小程序項(xiàng)目中,我們想要實(shí)現(xiàn)這樣一個(gè)功能, 比如我們小程序中的客服功能,我不想要使用小程序后臺的在線客服功能,但我又想實(shí)現(xiàn)客服功能,這個(gè)時(shí)候微信提供了消息推送功能,在小程序

    2024年02月09日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包