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

uniapp微信小程序使用stomp.js實(shí)現(xiàn)STOMP傳輸協(xié)議的實(shí)時(shí)聊天

這篇具有很好參考價(jià)值的文章主要介紹了uniapp微信小程序使用stomp.js實(shí)現(xiàn)STOMP傳輸協(xié)議的實(shí)時(shí)聊天。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

簡介:

stomp.js:原生微信小程序中使用
stomp.js:官網(wǎng)
stomp.js:GitHub

本來使用websocket,后端同事使用了stomp協(xié)議,導(dǎo)致前端也需要對(duì)應(yīng)修改。
uniapp微信小程序使用stomp.js實(shí)現(xiàn)STOMP傳輸協(xié)議的實(shí)時(shí)聊天,uni-app,微信小程序,IM,stomp,即時(shí)通信,websocket

如何使用

1.yarn add stompjs
2.版本 “stompjs”: “^2.3.3”
3.在static/js中新建websocket.js,然后在需要使用的頁面引入監(jiān)聽代碼+發(fā)送代碼即可

代碼如下:

位置:項(xiàng)目/pages/static/js/websocket.js
1.websocket.js

import Stomp from 'stompjs'

let socketOpen = false
let socketMsgQueue = []

export default {
  client: null,
  init(url, header ,connectWS) {
    if (this.client) {
      return Promise.resolve(this.client)
    }

    return new Promise((resolve, reject) => {
      const ws = {
        send: this.sendMessage,
        onopen: null,
        onmessage: null,
      }

      uni.connectSocket({ url, header })

      uni.onSocketOpen(function (res) {
        console.log('WebSocket連接已打開!', res)

        socketOpen = true
        for (let i = 0; i < socketMsgQueue.length; i++) {
          ws.send(socketMsgQueue[i])
        }
        socketMsgQueue = []

        ws.onopen && ws.onopen()
      })

      uni.onSocketMessage(function (res) {
        // ios 缺少 0x00 導(dǎo)致解析失敗
        if (res && res.data) {
          let value = res.data;
          let code = value.charCodeAt(value.length - 1);
          if (code !== 0x00) {
            value += String.fromCharCode(0x00);
            res.data = value;
          }
        }
        ws.onmessage && ws.onmessage(res)
      })

      uni.onSocketError(function (res) {
        console.log('WebSocket 錯(cuò)誤!', res)
      })

      uni.onSocketClose((res) => {
        this.client = null
        socketOpen = false
        console.log('WebSocket 已關(guān)閉!', res)
        if(res.code !== 1000){
          setTimeout(()=>{
            connectWS()
          },3000)
        }
      })

      Stomp.setInterval = function (interval, f) {
        return setInterval(f, interval)
      }
      Stomp.clearInterval = function (id) {
        return clearInterval(id)
      }

      const client = (this.client = Stomp.over(ws))
      // 關(guān)閉連接
      client.close = () =>{
        uni.closeSocket()
      }
      client.connect(header, function () {
        console.log('stomp connected')
        resolve(client)
      })
    })
  },
  sendMessage(message) {
    if (socketOpen) {
      uni.sendSocketMessage({
        data: message,
      })
    } else {
      socketMsgQueue.push(message)
    }
  },
}

2.監(jiān)聽+發(fā)送+關(guān)閉代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-679608.html

//import WebSocket from "../../static/js/websocket"
import WebSocket from "@/static/js/websocket"
const app = getApp();
data: {
	objUid: '1',
	client: null,
	subscription: '',
	content: '發(fā)送的內(nèi)容'
},
onLoad(options) {
	// stomp協(xié)議請(qǐng)求 
	this.initWS()
},
onShow() {
    // 切換任務(wù)后臺(tái),ws斷開后重連
    if(!WebSocket.client){
        this.initWS()
    }
},
// 離開頁面是關(guān)閉連接 
// 我的業(yè)務(wù)是仿微信這種,每次連接人不同,頻繁建立新連接,根據(jù)自己需要決定什么時(shí)候關(guān)閉連接
onUnload(){
    this.client && this.client.close()
    // 直接關(guān)閉websocket
	// this.client && this.client.close()
	// 不關(guān)閉websocket連接,但是斷開訂閱
	// this.subscription && this.subscription.unsubscribe(this.subscription.id)
},
initWS() {
	WebSocket.init(
		`${app.globalData.WSURL}/chat`,
		// 傳參
		{
			// login: 'admin',
			// passcode: 'admin',
		},
		// ws斷開回調(diào)
		() => {
			this.initWS()
		}
	).then((client) => {
		this.client = client
		// 訂閱
		this.subscription = client.subscribe(
			// 路徑
			`/response/${app.globalData.uid}/${this.objUid}`,
			// 接收到的數(shù)據(jù)
			(res) => {
				console.log(res)
			},
			// 消息不會(huì)被確認(rèn)接收,不確認(rèn)每次連接都會(huì)推送
			// { ack: 'client' } 
		)
	})
},
// 直接調(diào)用發(fā)送即可
send() {
	this.client.send(
		// 路徑
		`/child/${app.globalData.uid}/${this.objUid}`,
		// 自定義參數(shù) http://jmesnil.net/stomp-websocket/doc/
		{},//priority: 9 
		// 發(fā)送文本
		JSON.stringify({ 'content': this.content })
	);
},

到了這里,關(guān)于uniapp微信小程序使用stomp.js實(shí)現(xiàn)STOMP傳輸協(xié)議的實(shí)時(shí)聊天的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • 基于SockJS+Stomp的WebSocket實(shí)現(xiàn)

    前言 ? ? 之前做個(gè)一個(gè)功能,通過websocket長鏈接接收后臺(tái)推送的數(shù)據(jù),然后在前端動(dòng)態(tài)渲染。一直沒來的及輸出個(gè)文檔,現(xiàn)在輸出一下。 WebSocket介紹 ? ? WebSocket 是一種在 Web 應(yīng)用中實(shí)現(xiàn)實(shí)時(shí)通信的方法,它可以在客戶端和服務(wù)器端之間建立長連接,實(shí)現(xiàn)實(shí)時(shí)消息傳遞。 ?

    2024年02月12日
    瀏覽(24)
  • 整合 WebSocket 基于 STOMP 協(xié)議實(shí)現(xiàn)廣播

    整合 WebSocket 基于 STOMP 協(xié)議實(shí)現(xiàn)廣播

    SpringBoot 實(shí)戰(zhàn) (十六) | 整合 WebSocket 基于 STOMP 協(xié)議實(shí)現(xiàn)廣播 如題,今天介紹的是 SpringBoot 整合 WebSocket 實(shí)現(xiàn)廣播消息。 什么是 WebSocket ? WebSocket 為瀏覽器和服務(wù)器提供了雙工異步通信的功能,即瀏覽器可以向服務(wù)器發(fā)送信息,反之也成立。 WebSocket 是通過一個(gè) socket 來實(shí)現(xiàn)雙

    2024年01月21日
    瀏覽(20)
  • java中使用sockjs、stomp完成websocket通信

    主要配置 握手?jǐn)r截(這套方案好像前端無法補(bǔ)充Header,就不在這里做權(quán)限校驗(yàn))這里采用的方法是直接問號(hào)拼接token,前端 new SockJS(這里帶問號(hào)),sockjs使用的是http所以沒毛病,本文使用的是OAuth2權(quán)限校驗(yàn) 之后可以設(shè)置握手之后的身份注入(配置了這個(gè)可以在單對(duì)單訂閱時(shí)直接使用) 儲(chǔ)

    2024年02月10日
    瀏覽(24)
  • 使用uniapp在打包微信小程序時(shí)主包和vendor.js過大(uniCloud的插件分包)

    使用uniapp在打包微信小程序時(shí)主包和vendor.js過大(uniCloud的插件分包)

    正常的的微信小程序在編譯后,主包應(yīng)保持在2MB左右(限制最大2MB),其余的文件通過分包來進(jìn)行加載 ?但是有的情況下,微信小程序的主包能達(dá)到3MB,vendor.js會(huì)達(dá)到1.5MB 當(dāng)你發(fā)現(xiàn)的你的微信vendor.js只有1個(gè)文件夾,且這個(gè)文件非常大時(shí),首先需要檢查 manifest.json 在這個(gè)文件夾

    2024年02月02日
    瀏覽(244)
  • Springboot 整合 WebSocket ,使用STOMP協(xié)議 ,前后端整合實(shí)戰(zhàn) (一)(1)

    Springboot 整合 WebSocket ,使用STOMP協(xié)議 ,前后端整合實(shí)戰(zhàn) (一)(1)

    server: port: 9908 3.WebSocketConfig.java import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springfra

    2024年04月25日
    瀏覽(25)
  • Springboot 整合 WebSocket ,使用STOMP協(xié)議+Redis 解決負(fù)載場(chǎng)景問題

    Springboot 整合 WebSocket ,使用STOMP協(xié)議+Redis 解決負(fù)載場(chǎng)景問題

    ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jacksonSeial.setObjectMapper(om); template.setValueSerializer(jacksonSeial); template.setKeySerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); template

    2024年04月14日
    瀏覽(49)
  • springboot使用 WxJava 實(shí)現(xiàn) 微信小程序(uniapp開發(fā))的登陸功能

    springboot使用 WxJava 實(shí)現(xiàn) 微信小程序(uniapp開發(fā))的登陸功能

    前端使用uniapp來開發(fā)微信小程序 后端springboot中使用WxJava來做服務(wù)端( WxJava是微信服務(wù)端開發(fā) 的Java SDK ) 該圖來源于微信小程序官方文檔 根據(jù)uniapp的官網(wǎng)直接通過它提供的第三方 登陸api直接使用,代碼如下 直接根據(jù)WxJava的官方demo (1) yml配置 (2)兩個(gè)配置文件 (3)contro

    2024年02月15日
    瀏覽(90)
  • Spring Boot 3 + Vue 3 整合 WebSocket (STOMP協(xié)議) 實(shí)現(xiàn)廣播和點(diǎn)對(duì)點(diǎn)實(shí)時(shí)消息

    Spring Boot 3 + Vue 3 整合 WebSocket (STOMP協(xié)議) 實(shí)現(xiàn)廣播和點(diǎn)對(duì)點(diǎn)實(shí)時(shí)消息

    ?? 作者主頁: 有來技術(shù) ?? 開源項(xiàng)目: youlai-mall ?? vue3-element-admin ?? youlai-boot ?? 倉庫主頁: Gitee ?? Github ?? GitCode ?? 歡迎點(diǎn)贊 ?? 收藏 ?留言 ?? 如有錯(cuò)誤敬請(qǐng)糾正! WebSocket是一種在Web瀏覽器與Web服務(wù)器之間建立雙向通信的協(xié)議,而Spring Boot提供了便捷的WebSocket支持

    2024年02月02日
    瀏覽(17)
  • 前端筆記(Css、JS、Vue、UniApp、微信小程序)

    點(diǎn)擊穿透 應(yīng)用場(chǎng)景:點(diǎn)贊或送禮等出現(xiàn)的彈窗遮罩,無法再次觸發(fā)點(diǎn)擊事件 磨砂模糊 底部安全距離 可以放入【common.scss】中,在需要的頁面引入 寬度根據(jù)內(nèi)容決定 媒體查詢@media 必須是以 @media 開頭 使用 mediatype 指定媒體(設(shè)備)類型 使用 and | not | only 邏輯操作符構(gòu)建復(fù)雜

    2024年04月26日
    瀏覽(71)
  • uniapp開發(fā)微信小程序?qū)崿F(xiàn)語音識(shí)別,使用微信同聲傳譯插件,

    uniapp開發(fā)微信小程序?qū)崿F(xiàn)語音識(shí)別,使用微信同聲傳譯插件,

    第一步:在微信小程序管理后臺(tái):“設(shè)置”-》“第三方設(shè)置”-》“插件管理”中添加插件。 但是這個(gè)地方,沒有搜索到插件,就到微信服務(wù)市場(chǎng) 搜索到以后添加到需要的小程序里面,然后返回管理中心查看,就可以看到了 第二步:在配置文件中引入插件 第三步:在需要使

    2024年02月05日
    瀏覽(25)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包