zann-voice 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/types/index.d.ts +3 -1
package/README.md
CHANGED
|
@@ -28,6 +28,7 @@ voice.connect({
|
|
|
28
28
|
onOpen: () => console.log("Connected!"),
|
|
29
29
|
onTrack: (e) => console.log("Track received", e),
|
|
30
30
|
onError: (e) => console.error(e),
|
|
31
|
+
onAuth:(e) => console.log(e),
|
|
31
32
|
onMessage: (msg) => console.log("WS Message", msg)
|
|
32
33
|
});
|
|
33
34
|
|
|
@@ -59,6 +60,7 @@ voice.connect({
|
|
|
59
60
|
media:{ audio:true, video:false},
|
|
60
61
|
onOpen: () => console.log("Connected!"),
|
|
61
62
|
onTrack: (e) => console.log("Track received", e),
|
|
63
|
+
onAuth:(e) => console.log(e),
|
|
62
64
|
onError: (e) => console.error(e),
|
|
63
65
|
onMessage: (msg) => console.log("WS Message", msg)
|
|
64
66
|
});
|
|
@@ -67,11 +69,12 @@ voice.connect({
|
|
|
67
69
|
#####
|
|
68
70
|
| 属性 | 类型| 是否必须 |备注 |
|
|
69
71
|
| -------------| :-----------: | :-----------: | :-----------: |
|
|
70
|
-
|
|
|
72
|
+
| media | media |否 | 媒体模式, 默认开启麦克风,默认关闭摄像头 |
|
|
71
73
|
| onOpen | function |是 | 监听到连接成功后发生 |
|
|
72
74
|
| onTrack | function |是 | 监听媒体流[请参考](https://developer.mozilla.org/en-US/docs/Web/API/RTCTrackEvent#example) |
|
|
73
75
|
| onError | function |是 | 监听错误问题 |
|
|
74
76
|
| onMessage | function |是 | 监听websocket 发来的内容 |
|
|
77
|
+
| onAuth | function | 是 | 监听到需要用户授权麦克风或者摄像头 |
|
|
75
78
|
|
|
76
79
|
媒体模式参数
|
|
77
80
|
``` markdown
|
|
@@ -93,7 +96,7 @@ voice.connect({
|
|
|
93
96
|
> <br/>必须先调用connect 才可正常调用此方法
|
|
94
97
|
|
|
95
98
|
### **5.startCamera()开启摄像头**
|
|
96
|
-
> 如果调用了stopCamera
|
|
99
|
+
> 如果调用了stopCamera 关闭摄像头,可以调用此方法恢复摄像头,需要click事件
|
|
97
100
|
> <br/>必须先调用connect 才可正常调用此方法
|
|
98
101
|
|
|
99
102
|
### **6.stopCamera()关闭摄像头**
|
package/dist/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function t(o,e,s,i){if(s==="a"&&!i)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?o!==e||!i:!e.has(o))throw new TypeError("Cannot read private member from an object whose class did not declare it");return s==="m"?i:s==="a"?i.call(o):i?i.value:e.get(o)}function n(o,e,s,i,
|
|
1
|
+
"use strict";function t(o,e,s,i){if(s==="a"&&!i)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?o!==e||!i:!e.has(o))throw new TypeError("Cannot read private member from an object whose class did not declare it");return s==="m"?i:s==="a"?i.call(o):i?i.value:e.get(o)}function n(o,e,s,i,d){if(i==="m")throw new TypeError("Private method is not writable");if(i==="a"&&!d)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?o!==e||!d:!e.has(o))throw new TypeError("Cannot write private member to an object whose class did not declare it");return i==="a"?d.call(o,s):d?d.value=s:e.set(o,s),s}typeof SuppressedError=="function"&&SuppressedError;var h,a,r,c,_,u,l,w,p,S,W,V,g,y,m,T,M,k,Z,E;class b{constructor({wss:e,TURN:s,maxReconnect:i=5}){h.add(this),a.set(this,null),r.set(this,null),c.set(this,null),_.set(this,!1),u.set(this,!1),l.set(this,0),w.set(this,5),p.set(this,""),S.set(this,{urls:""}),W.set(this,void 0),V.set(this,void 0),g.set(this,void 0),y.set(this,void 0),m.set(this,void 0),n(this,p,e,"f"),n(this,S,s,"f"),n(this,w,i,"f")}async connect({media:e={audio:!0,video:!1},onOpen:s,onTrack:i,onError:d,onMessage:C,onAuth:v}){if(t(this,_,"f")){console.warn("\u26A0\uFE0F \u5DF2\u7ECF\u5B58\u5728 RTC/WS\uFF0C\u4E0D\u91CD\u590D\u521B\u5EFA");return}n(this,u,!1,"f"),n(this,l,0,"f"),n(this,W,s,"f"),n(this,V,i,"f"),n(this,g,d,"f"),n(this,y,C,"f"),n(this,m,v,"f"),console.log("\u2728 Creating WebRTC + WebSocket..."),n(this,a,new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},t(this,S,"f")]}),"f"),t(this,a,"f").onicecandidate=f=>{f.candidate&&t(this,r,"f")?.readyState===WebSocket.OPEN&&t(this,r,"f").send(JSON.stringify({type:"ice",data:f.candidate}))},t(this,a,"f").ontrack=f=>t(this,V,"f").call(this,f),t(this,a,"f").onconnectionstatechange=()=>{const f=t(this,a,"f")?.connectionState;console.log("RTC connection state:",f),!t(this,u,"f")&&(f==="disconnected"||f==="failed")&&(console.warn("RTC disconnected, retrying offer..."),t(this,h,"m",k).call(this))},t(this,h,"m",T).call(this),n(this,_,!0,"f"),e.audio&&await this.startMic(),e.video&&await this.startCamera()}async startMic(){if(!t(this,a,"f")){console.warn("\u274C startMic \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getAudioTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({audio:!0});e.getAudioTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getAudioTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",k).call(this)}catch(e){t(this,m,"f").call(this,e)}}stopMic(){t(this,c,"f")&&t(this,c,"f").getAudioTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)})}async startCamera(){if(!t(this,a,"f")){console.warn("\u274C startCamera \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getVideoTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({video:!0});e.getVideoTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getVideoTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",k).call(this)}catch(e){t(this,m,"f").call(this,e)}}stopCamera(){t(this,c,"f")&&t(this,c,"f").getVideoTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)})}destroy(){console.log("\u{1F6D1} Destroying ZannVoice..."),n(this,u,!0,"f"),this.stopMic(),this.stopCamera(),t(this,h,"m",Z).call(this),t(this,h,"m",E).call(this),n(this,_,!1,"f")}}a=new WeakMap,r=new WeakMap,c=new WeakMap,_=new WeakMap,u=new WeakMap,l=new WeakMap,w=new WeakMap,p=new WeakMap,S=new WeakMap,W=new WeakMap,V=new WeakMap,g=new WeakMap,y=new WeakMap,m=new WeakMap,h=new WeakSet,T=function o(e=1e3){t(this,p,"f")&&(n(this,r,new WebSocket(t(this,p,"f")),"f"),t(this,r,"f").onopen=()=>{console.log("\u2705 WS connected"),n(this,l,0,"f"),t(this,a,"f")?.connectionState!=="connected"&&t(this,h,"m",k).call(this)},t(this,r,"f").onmessage=s=>t(this,h,"m",M).call(this,s),t(this,r,"f").onerror=s=>t(this,g,"f").call(this,s),t(this,r,"f").onclose=()=>{var s;if(!t(this,u,"f")){if(t(this,l,"f")>=t(this,w,"f")){console.error(`\u274C WS \u8D85\u8FC7\u6700\u5927\u91CD\u8FDE\u6B21\u6570 ${t(this,w,"f")}`),t(this,g,"f")?.call(this,new Error(`WS exceeded max reconnects ${t(this,w,"f")}`));return}n(this,l,(s=t(this,l,"f"),s++,s),"f"),console.warn(`\u{1F504} WS disconnected, retrying in ${e}ms... [${t(this,l,"f")}/${t(this,w,"f")}]`),setTimeout(()=>t(this,h,"m",o).call(this,Math.min(e*2,1e4)),e)}})},M=async function(e){const s=JSON.parse(e.data);switch(s.type){case"answer":await t(this,a,"f")?.setRemoteDescription(s.data),t(this,W,"f").call(this);break;case"ice":await t(this,a,"f")?.addIceCandidate(s.data);break;case"offer":await t(this,a,"f")?.setRemoteDescription(s.data);const i=await t(this,a,"f")?.createAnswer();i&&(await t(this,a,"f")?.setLocalDescription(i),t(this,r,"f")?.send(JSON.stringify({type:"answer",data:i})));break;default:t(this,y,"f").call(this,e);break}},k=async function(){if(!t(this,a,"f")||!t(this,r,"f")||t(this,r,"f").readyState!==WebSocket.OPEN)return;const e=await t(this,a,"f").createOffer();await t(this,a,"f").setLocalDescription(e),t(this,r,"f").send(JSON.stringify({type:"offer",data:e}))},Z=function(){t(this,a,"f")?.getSenders().forEach(e=>e.track?.stop()),t(this,a,"f")?.close(),n(this,a,null,"f")},E=function(){t(this,r,"f")&&t(this,r,"f").readyState===WebSocket.OPEN&&t(this,r,"f").close(),n(this,r,null,"f")},module.exports=b;
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function
|
|
1
|
+
function t(o,e,s,i){if(s==="a"&&!i)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?o!==e||!i:!e.has(o))throw new TypeError("Cannot read private member from an object whose class did not declare it");return s==="m"?i:s==="a"?i.call(o):i?i.value:e.get(o)}function n(o,e,s,i,d){if(i==="m")throw new TypeError("Private method is not writable");if(i==="a"&&!d)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?o!==e||!d:!e.has(o))throw new TypeError("Cannot write private member to an object whose class did not declare it");return i==="a"?d.call(o,s):d?d.value=s:e.set(o,s),s}typeof SuppressedError=="function"&&SuppressedError;var h,a,r,c,_,u,l,w,p,S,W,V,g,y,m,T,M,k,Z,E;class b{constructor({wss:e,TURN:s,maxReconnect:i=5}){h.add(this),a.set(this,null),r.set(this,null),c.set(this,null),_.set(this,!1),u.set(this,!1),l.set(this,0),w.set(this,5),p.set(this,""),S.set(this,{urls:""}),W.set(this,void 0),V.set(this,void 0),g.set(this,void 0),y.set(this,void 0),m.set(this,void 0),n(this,p,e,"f"),n(this,S,s,"f"),n(this,w,i,"f")}async connect({media:e={audio:!0,video:!1},onOpen:s,onTrack:i,onError:d,onMessage:C,onAuth:v}){if(t(this,_,"f")){console.warn("\u26A0\uFE0F \u5DF2\u7ECF\u5B58\u5728 RTC/WS\uFF0C\u4E0D\u91CD\u590D\u521B\u5EFA");return}n(this,u,!1,"f"),n(this,l,0,"f"),n(this,W,s,"f"),n(this,V,i,"f"),n(this,g,d,"f"),n(this,y,C,"f"),n(this,m,v,"f"),console.log("\u2728 Creating WebRTC + WebSocket..."),n(this,a,new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},t(this,S,"f")]}),"f"),t(this,a,"f").onicecandidate=f=>{f.candidate&&t(this,r,"f")?.readyState===WebSocket.OPEN&&t(this,r,"f").send(JSON.stringify({type:"ice",data:f.candidate}))},t(this,a,"f").ontrack=f=>t(this,V,"f").call(this,f),t(this,a,"f").onconnectionstatechange=()=>{const f=t(this,a,"f")?.connectionState;console.log("RTC connection state:",f),!t(this,u,"f")&&(f==="disconnected"||f==="failed")&&(console.warn("RTC disconnected, retrying offer..."),t(this,h,"m",k).call(this))},t(this,h,"m",T).call(this),n(this,_,!0,"f"),e.audio&&await this.startMic(),e.video&&await this.startCamera()}async startMic(){if(!t(this,a,"f")){console.warn("\u274C startMic \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getAudioTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({audio:!0});e.getAudioTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getAudioTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",k).call(this)}catch(e){t(this,m,"f").call(this,e)}}stopMic(){t(this,c,"f")&&t(this,c,"f").getAudioTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)})}async startCamera(){if(!t(this,a,"f")){console.warn("\u274C startCamera \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getVideoTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({video:!0});e.getVideoTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getVideoTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",k).call(this)}catch(e){t(this,m,"f").call(this,e)}}stopCamera(){t(this,c,"f")&&t(this,c,"f").getVideoTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)})}destroy(){console.log("\u{1F6D1} Destroying ZannVoice..."),n(this,u,!0,"f"),this.stopMic(),this.stopCamera(),t(this,h,"m",Z).call(this),t(this,h,"m",E).call(this),n(this,_,!1,"f")}}a=new WeakMap,r=new WeakMap,c=new WeakMap,_=new WeakMap,u=new WeakMap,l=new WeakMap,w=new WeakMap,p=new WeakMap,S=new WeakMap,W=new WeakMap,V=new WeakMap,g=new WeakMap,y=new WeakMap,m=new WeakMap,h=new WeakSet,T=function o(e=1e3){t(this,p,"f")&&(n(this,r,new WebSocket(t(this,p,"f")),"f"),t(this,r,"f").onopen=()=>{console.log("\u2705 WS connected"),n(this,l,0,"f"),t(this,a,"f")?.connectionState!=="connected"&&t(this,h,"m",k).call(this)},t(this,r,"f").onmessage=s=>t(this,h,"m",M).call(this,s),t(this,r,"f").onerror=s=>t(this,g,"f").call(this,s),t(this,r,"f").onclose=()=>{var s;if(!t(this,u,"f")){if(t(this,l,"f")>=t(this,w,"f")){console.error(`\u274C WS \u8D85\u8FC7\u6700\u5927\u91CD\u8FDE\u6B21\u6570 ${t(this,w,"f")}`),t(this,g,"f")?.call(this,new Error(`WS exceeded max reconnects ${t(this,w,"f")}`));return}n(this,l,(s=t(this,l,"f"),s++,s),"f"),console.warn(`\u{1F504} WS disconnected, retrying in ${e}ms... [${t(this,l,"f")}/${t(this,w,"f")}]`),setTimeout(()=>t(this,h,"m",o).call(this,Math.min(e*2,1e4)),e)}})},M=async function(e){const s=JSON.parse(e.data);switch(s.type){case"answer":await t(this,a,"f")?.setRemoteDescription(s.data),t(this,W,"f").call(this);break;case"ice":await t(this,a,"f")?.addIceCandidate(s.data);break;case"offer":await t(this,a,"f")?.setRemoteDescription(s.data);const i=await t(this,a,"f")?.createAnswer();i&&(await t(this,a,"f")?.setLocalDescription(i),t(this,r,"f")?.send(JSON.stringify({type:"answer",data:i})));break;default:t(this,y,"f").call(this,e);break}},k=async function(){if(!t(this,a,"f")||!t(this,r,"f")||t(this,r,"f").readyState!==WebSocket.OPEN)return;const e=await t(this,a,"f").createOffer();await t(this,a,"f").setLocalDescription(e),t(this,r,"f").send(JSON.stringify({type:"offer",data:e}))},Z=function(){t(this,a,"f")?.getSenders().forEach(e=>e.track?.stop()),t(this,a,"f")?.close(),n(this,a,null,"f")},E=function(){t(this,r,"f")&&t(this,r,"f").readyState===WebSocket.OPEN&&t(this,r,"f").close(),n(this,r,null,"f")};export{b as default};
|
package/dist/index.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ type voice = {
|
|
|
7
7
|
onTrack: (event: any) => void;
|
|
8
8
|
onError: (event: any) => void;
|
|
9
9
|
onMessage: (event: any) => void;
|
|
10
|
+
onAuth: (event: any) => void;
|
|
10
11
|
};
|
|
11
12
|
declare class ZannVoice {
|
|
12
13
|
#private;
|
|
@@ -31,9 +32,10 @@ declare class ZannVoice {
|
|
|
31
32
|
* @param onTrack 监听到媒体流
|
|
32
33
|
* @param onError 创建出现了问题
|
|
33
34
|
* @param onMessage 监听websocket 发来的内容
|
|
35
|
+
* @param onAuth 当需要授权时候发生
|
|
34
36
|
* @returns
|
|
35
37
|
*/
|
|
36
|
-
connect({ media, onOpen, onTrack, onError, onMessage, }: voice): Promise<void>;
|
|
38
|
+
connect({ media, onOpen, onTrack, onError, onMessage, onAuth, }: voice): Promise<void>;
|
|
37
39
|
/**
|
|
38
40
|
* 开启麦克风
|
|
39
41
|
* @returns
|