WebSocket+Vue+SpringBoot實(shí)現(xiàn)語音通話的使用示例
整體思路
前端點(diǎn)擊開始對(duì)話按鈕后,將監(jiān)聽麥克風(fēng),獲取到當(dāng)前的音頻,將其裝化為二進(jìn)制數(shù)據(jù),通過websocket發(fā)送到webscoket服務(wù)端,服務(wù)端在接收后,將消息寫入給指定客戶端,客戶端拿到發(fā)送過來的二進(jìn)制音頻后再轉(zhuǎn)化播放
注意事項(xiàng)
由于音頻轉(zhuǎn)化后的二進(jìn)制數(shù)據(jù)較大,websocket默認(rèn)的消息傳輸大小不能被接收,所以需要通過 @OnMessage(maxMessageSize=5242880)注解進(jìn)行調(diào)整
Vue代碼
<template> <div class="play-audio"> <el-button @click="startCall" ref="start">開始對(duì)講</el-button> <el-button @click="stopCall" ref="stop">結(jié)束對(duì)講</el-button> </div> </template> <script> export default { data() { return { ws: null, mediaStack: null, audioCtx: null, scriptNode: null, source: null, play: true } }, methods: { initWs1() { //設(shè)置好友ID let recipientId=localStorage.getItem('userId')=="2"?"1":"2"; this.ws = new WebSocket('ws://192.168.206.204:8081/video/'+localStorage.getItem('userId')+"/"+recipientId) this.ws.onopen = () => { console.log('socket 已連接') } this.ws.binaryType = 'arraybuffer' this.ws.onmessage = ({ data }) => { console.log("接收到的數(shù)據(jù)--》"+ data) // 將接收的數(shù)據(jù)轉(zhuǎn)換成與傳輸過來的數(shù)據(jù)相同的Float32Array const buffer = new Float32Array(data) // 創(chuàng)建一個(gè)空白的AudioBuffer對(duì)象,這里的4096跟發(fā)送方保持一致,48000是采樣率 const myArrayBuffer = this.audioCtx.createBuffer(1, 4096, 48000) // 也是由于只創(chuàng)建了一個(gè)音軌,可以直接取到0 const nowBuffering = myArrayBuffer.getChannelData(0) // 通過循環(huán),將接收過來的數(shù)據(jù)賦值給簡(jiǎn)單音頻對(duì)象 for (let i = 0; i < 4096; i++) { nowBuffering[i] = buffer[i] } // 使用AudioBufferSourceNode播放音頻 const source = this.audioCtx.createBufferSource() source.buffer = myArrayBuffer const gainNode = this.audioCtx.createGain() source.connect(gainNode) gainNode.connect(this.audioCtx.destination) var muteValue = 1 if (!this.play) { // 是否靜音 muteValue = 0 } gainNode.gain.setValueAtTime(muteValue, this.audioCtx.currentTime) source.start() } this.ws.onerror = (e) => { console.log('發(fā)生錯(cuò)誤', e) } this.ws.onclose = () => { console.log('socket closed') } }, // 開始對(duì)講 startCall() { this.play = true this.audioCtx = new AudioContext() this.initWs1() // 該變量存儲(chǔ)當(dāng)前MediaStreamAudioSourceNode的引用 // 可以通過它關(guān)閉麥克風(fēng)停止音頻傳輸 // 創(chuàng)建一個(gè)ScriptProcessorNode 用于接收當(dāng)前麥克風(fēng)的音頻 this.scriptNode = this.audioCtx.createScriptProcessor(4096, 1, 1) navigator.mediaDevices .getUserMedia({ audio: true, video: false }) .then((stream) => { this.mediaStack = stream this.source = this.audioCtx.createMediaStreamSource(stream) this.source.connect(this.scriptNode) this.scriptNode.connect(this.audioCtx.destination) }) .catch(function (err) { /* 處理error */ console.log('err', err) }) // 當(dāng)麥克風(fēng)有聲音輸入時(shí),會(huì)調(diào)用此事件 // 實(shí)際上麥克風(fēng)始終處于打開狀態(tài)時(shí),即使不說話,此事件也在一直調(diào)用 this.scriptNode.onaudioprocess = (audioProcessingEvent) => { const inputBuffer = audioProcessingEvent.inputBuffer // console.log("inputBuffer",inputBuffer); // 由于只創(chuàng)建了一個(gè)音軌,這里只取第一個(gè)頻道的數(shù)據(jù) const inputData = inputBuffer.getChannelData(0) console.log("調(diào)用") // 通過socket傳輸數(shù)據(jù),實(shí)際上傳輸?shù)氖荈loat32Array if (this.ws.readyState === 1) { // console.log("發(fā)送的數(shù)據(jù)",inputData); this.ws.send(inputData) } } }, // 關(guān)閉麥克風(fēng) stopCall() { this.play = false this.mediaStack.getTracks()[0].stop() this.scriptNode.disconnect() } } } </script>
java代碼
webscoket配置類
package com.example.software.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.server.standard.ServerEndpointExporter; /** * @Description: websocket配置 */ @Configuration @EnableWebSocket public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
webscoket服務(wù)類
package com.example.software.service.webscoket; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * @Author:wf * @Date 2023/5/14 13:55 * 消息收發(fā) **/ @Controller @ServerEndpoint(value = "/video/{senderID}/{recipientId}") @Slf4j public class WebSocketServer { /** 當(dāng)前在線連接數(shù)。應(yīng)該把它設(shè)計(jì)成線程安全的 */ private static int onlineCount = 0; /** 存放每個(gè)客戶端對(duì)應(yīng)的MyWebSocket對(duì)象。實(shí)現(xiàn)服務(wù)端與單一客戶端通信的話,其中Key可以為用戶標(biāo)識(shí) */ private static ConcurrentHashMap<String, Session> webSocketSet = new ConcurrentHashMap<String, Session>(); /** 與某個(gè)客戶端的連接會(huì)話,需要通過它來給客戶端發(fā)送數(shù)據(jù) */ private Session WebSocketsession; /** 當(dāng)前發(fā)消息的人員編號(hào) */ private String senderID = ""; /** * 連接建立成功調(diào)用的方法 * @param param 發(fā)送者ID,是由誰發(fā)送的 * @param WebSocketsession 可選的參數(shù)。session為與某個(gè)客戶端的連接會(huì)話,需要通過它來給客戶端發(fā)送數(shù)據(jù) */ @OnOpen public void onOpen(@PathParam(value = "senderID") String param, @PathParam(value = "recipientId") String recipientId,Session WebSocketsession) { System.out.println("人員-------**-------編號(hào):"+param+":加入聊天"); System.out.println("盆友是:"+recipientId+""); //接收到發(fā)送消息的人員編號(hào) senderID = param; System.out.println("senderID:"+senderID); //設(shè)置消息大小最大為10M,這種方式也可以達(dá)到效果,或者使用下面的 @OnMessage(maxMessageSize=5242880) //The default buffer size for text messages is 8192 bytes.消息超過8192b,自動(dòng)斷開連接 // WebSocketsession.setMaxTextMessageBufferSize(10*1024*1024); // WebSocketsession.setMaxBinaryMessageBufferSize(10*1024*1024); //加入map中,綁定當(dāng)前用戶和socket webSocketSet.put(param, WebSocketsession); //在線數(shù)加1 addOnlineCount(); } /** * 連接關(guān)閉調(diào)用的方法 */ @OnClose public void onClose() { if (StrUtil.isNotBlank(senderID)) { //從set中刪除 webSocketSet.remove(senderID); //在線數(shù)減1 subOnlineCount(); } } /** * 收到客戶端消息后調(diào)用的方法 * *設(shè)置最大接收消息大小 */ @OnMessage(maxMessageSize=5242880) public void onMessage(@PathParam(value = "senderID") String senderID ,@PathParam(value = "recipientId") String recipientId,InputStream inputStream) { System.out.println(senderID+":發(fā)送給"+recipientId+"的消息-->"+inputStream); try { byte[] buff = new byte[inputStream.available()]; inputStream.read(buff, 0, inputStream.available()); Session session = webSocketSet.get("2"); synchronized (session) { //給2號(hào)發(fā)送 session.getBasicRemote().sendBinary(ByteBuffer.wrap(buff)); } } catch (Exception e) { e.printStackTrace(); } } /** * 發(fā)生錯(cuò)誤時(shí)調(diào)用 * * @param session * @param error */ @OnError public void onError(Session session, Throwable error) { error.printStackTrace(); } /** * 為指定用戶發(fā)送消息 * * @param message 消息內(nèi)容 * @throws IOException */ public void sendMessage(String message) throws IOException { //加同步鎖,解決多線程下發(fā)送消息異常關(guān)閉 synchronized (this.WebSocketsession){ this.WebSocketsession.getBasicRemote().sendText(message); } } /** * 獲取當(dāng)前在線人數(shù) * @return 返回當(dāng)前在線人數(shù) */ public static synchronized int getOnlineCount() { return onlineCount; } /** * 增加當(dāng)前在線人數(shù) */ public static synchronized void addOnlineCount() { WebSocketServer.onlineCount++; } /** * 減少當(dāng)前在線人數(shù) */ public static synchronized void subOnlineCount() { WebSocketServer.onlineCount--; } }
測(cè)試方法
1.使用兩個(gè)瀏覽器模擬兩個(gè)用戶,首先在瀏覽器本地存儲(chǔ)一個(gè)用戶ID
用戶A–谷歌瀏覽器:
用戶B–火狐瀏覽器
2.點(diǎn)擊按鈕,進(jìn)行測(cè)試
3.關(guān)于谷歌瀏覽器提示TypeError: Cannot read property ‘getUserMedia’ of undefined
原因:chrome下獲取瀏覽器錄音功能,因?yàn)榘踩詥栴},需要在localhost或127.0.0.1或https下才能獲取權(quán)限
解決方案:
1.網(wǎng)頁使用https訪問,服務(wù)端升級(jí)為https訪問,配置ssl證書
2.使用localhost或127.0.0.1 進(jìn)行訪問
3.修改瀏覽器安全配置(最直接、簡(jiǎn)單)
a.首先在chrome瀏覽器中輸入如下指令
chrome://flags/#unsafely-treat-insecure-origin-as-secure
b.然后開啟Insecure origins treated as secure
在下方輸入欄內(nèi)輸入你訪問的地址url,然后將右側(cè)Disabled 改成 Enabled即可
c.然后瀏覽器會(huì)提示重啟, 點(diǎn)擊Relaunch即可
到此這篇關(guān)于WebSocket+Vue+SpringBoot實(shí)現(xiàn)語音通話的使用示例的文章就介紹到這了,更多相關(guān)WebSocket+Vue+SpringBoot語音通話內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解jenkins自動(dòng)部署springboot應(yīng)用的方法
這篇文章主要介紹了詳解jenkins自動(dòng)部署springboot應(yīng)用的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08java實(shí)現(xiàn)HmacSHA256算法進(jìn)行加密方式
這篇文章主要介紹了java實(shí)現(xiàn)HmacSHA256算法進(jìn)行加密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Java使用原型模式展現(xiàn)每日生活應(yīng)用案例詳解
這篇文章主要介紹了Java使用原型模式展現(xiàn)每日生活應(yīng)用案例,較為詳細(xì)的分析了原型模式的概念、原理及Java使用原型模式展現(xiàn)每日生活案例的相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下2018-05-05java實(shí)現(xiàn)簡(jiǎn)單五子棋小游戲(1)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單五子棋小游戲的第一部分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01jbuilder2006連接sqlserver2000的方法
xp jbuiler2006 連接SQL SERVER2000的問題2008-10-10Java try-catch-finally異常處理機(jī)制詳解
這篇文章主要介紹了Java try-catch-finally異常處理機(jī)制詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08JDK8配置環(huán)境變量的bat文件的詳細(xì)教程
這篇文章主要介紹了JDK8配置環(huán)境變量的bat文件,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07