@@ -40,6 +40,16 @@ export type OwnerInfo = {
40
40
owner : boolean ;
41
41
} ;
42
42
43
+ export type ConnectOption = {
44
+ roomJoined ?: ( ...args : any [ ] ) => Promise < void > ;
45
+ roomCreated ?: ( data : any ) => Promise < void > ;
46
+ onAddRtcPeer ?: ( id : string , pc : any ) => Promise < void > ;
47
+ onConnectComplete ?: ( ...args : any [ ] ) => void ;
48
+ onBeforeCreateOffer ?: ( ...args : any [ ] ) => Promise < void > ;
49
+ onBeforeCreateAnswer ?: ( ...args : any [ ] ) => Promise < void > ;
50
+ onTrack ?: ( ...args : any [ ] ) => void ;
51
+ } ;
52
+
43
53
export const useRoom = ( value : MaybeRef < string > | CommonFnType ) => {
44
54
const realValue = resolveRef ( value ) ;
45
55
const roomIdReg = / ^ [ a - z A - Z 0 - 9 ] { 4 , 32 } $ / ;
@@ -101,13 +111,15 @@ export const useCreateRoom = (type: 'password' | 'video' = 'password') => {
101
111
} ;
102
112
} ;
103
113
104
- export const useRoomConnect = ( ) => {
114
+ export const useRoomConnect = ( option : ConnectOption = { } ) => {
105
115
const roomInfo = useGetRoomInfo ( ) ;
106
116
107
117
const { members, selfInfo } = roomInfo ;
108
118
const rtcConnects = new Map < string , RTCPeerConnection > ( ) ;
119
+ const dataChanelMap = new Map < string , RTCDataChannel > ( ) ;
109
120
const initData = inject ( InitDataKey ) ! ;
110
121
const socketRef = shallowRef ( ) ;
122
+ const completed = ref ( false ) ;
111
123
112
124
watchArray (
113
125
( ) => members . value ,
@@ -116,41 +128,90 @@ export const useRoomConnect = () => {
116
128
117
129
// 处理 exit
118
130
if ( removed . length ) {
119
- removed . forEach ( ( peer ) => {
131
+ removed . forEach ( async ( peer ) => {
120
132
if ( peer . id ) {
133
+ const rtcConnect = await getRtcConnect ( peer . id ) ;
134
+ rtcConnect . close ( ) ;
121
135
rtcConnects . delete ( peer . id ) ;
122
136
}
123
137
} ) ;
124
138
}
125
139
}
126
140
) ;
127
141
128
- const createRtcConnect = ( id : string ) => {
129
- const pc = new RTCPeerConnection ( initData . value . config ) ;
142
+ const createRtcConnect = async ( id : string ) => {
143
+ // const pc = new RTCPeerConnection(initData.value.config);
144
+ const pc = new RTCPeerConnection ( ) ;
145
+ pc . onicecandidate = ( event ) => {
146
+ if ( event . candidate != null ) {
147
+ const message = {
148
+ from : selfInfo . value . socketId ,
149
+ to : id ,
150
+ room : selfInfo . value . roomId ,
151
+ sdpMid : event . candidate . sdpMid ,
152
+ sdpMLineIndex : event . candidate . sdpMLineIndex ,
153
+ sdp : event . candidate . candidate ,
154
+ } ;
155
+ socketRef . value . emit ( 'candidate' , message ) ;
156
+ }
157
+ } ;
158
+
159
+ const dataChanel = pc . createDataChannel ( 'datachanel' ) ;
160
+ dataChanelMap . set ( id , dataChanel ) ;
161
+
162
+ dataChanel . onopen = ( ) => {
163
+ // dataChanel.send('aaa');
164
+ } ;
165
+
166
+ pc . ondatachannel = ( e ) => {
167
+ const chanel = e . channel ;
168
+ if ( chanel . label === 'datachanel' ) {
169
+ chanel . onmessage = ( e : any ) => {
170
+ // console.log('接受', e.data);
171
+ } ;
172
+ }
173
+ } ;
174
+
175
+ pc . onconnectionstatechange = ( e ) => {
176
+ if ( pc . connectionState === 'connected' ) {
177
+ // console.log('完成');
178
+ completed . value = true ;
179
+ }
180
+ } ;
130
181
131
- pc . onicecandidate = ( ) => {
132
- // console.log('on candidate', e );
182
+ pc . ontrack = ( e ) => {
183
+ option ?. onTrack ?. ( e , id ) ;
133
184
} ;
134
185
135
186
rtcConnects . set ( id , pc ) ;
187
+ await option . onAddRtcPeer ?.( id , pc ) ;
136
188
137
189
return pc ;
138
190
} ;
139
191
140
- const getRtcConnect = ( id : string ) => {
141
- return rtcConnects . get ( id ) || createRtcConnect ( id ) ;
192
+ const getRtcConnect = async ( id : string ) => {
193
+ return rtcConnects . get ( id ) || ( await createRtcConnect ( id ) ) ;
194
+ } ;
195
+
196
+ const roomCreated = async ( data : any ) => {
197
+ await option ?. roomCreated ?.( data ) ;
142
198
} ;
143
199
144
200
const roomJoined = async ( data : any ) => {
145
- createOffer ( getRtcConnect ( data . id ) , data ) ;
201
+ const peer = await getRtcConnect ( data . id ) ;
202
+
203
+ await option ?. roomJoined ?.( data . id , peer ) ;
204
+ createOffer ( peer , data ) ;
146
205
} ;
147
206
148
207
/**
149
208
* @description 这里的 createOffer
150
209
*/
151
210
const createOffer = async ( pc : RTCPeerConnection , peer : any ) => {
211
+ await option ?. onBeforeCreateOffer ?.( peer . id , pc ) ;
152
212
const offer = await pc . createOffer ( initData . value . options ) ;
153
213
await pc . setLocalDescription ( offer ) ;
214
+ console . log ( 'create offer - send' ) ;
154
215
socketRef . value . emit ( SocketEventName . RoomOffer , {
155
216
from : selfInfo . value . socketId ,
156
217
to : peer . id ,
@@ -163,13 +224,14 @@ export const useRoomConnect = () => {
163
224
* @description offer 监听事件
164
225
*/
165
226
const roomOffer = async ( data : any ) => {
166
- const pc = getRtcConnect ( data . from ) ;
227
+ const pc = await getRtcConnect ( data . from ) ;
167
228
await pc . setRemoteDescription (
168
229
new RTCSessionDescription ( { type : 'offer' , sdp : data . sdp } )
169
230
) ;
231
+ await option ?. onBeforeCreateAnswer ?.( data . from , pc ) ;
170
232
const answer = await pc . createAnswer ( initData . value . options ) ;
171
233
await pc . setLocalDescription ( answer ) ;
172
-
234
+ console . log ( 'receive offer - send answer' ) ;
173
235
socketRef . value . emit ( SocketEventName . RoomAnswer , {
174
236
from : selfInfo . value . socketId ,
175
237
to : data . from ,
@@ -182,25 +244,37 @@ export const useRoomConnect = () => {
182
244
* @description answer 监听事件
183
245
*/
184
246
const roomAnswer = async ( data : any ) => {
185
- const pc = getRtcConnect ( data . from ) ;
247
+ const pc = await getRtcConnect ( data . from ) ;
248
+ console . log ( 'receive answer' ) ;
186
249
await pc . setRemoteDescription (
187
250
new RTCSessionDescription ( { type : 'answer' , sdp : data . sdp } )
188
251
) ;
189
252
} ;
190
- const roomCandidate = ( ) => {
191
- //
253
+ const roomCandidate = async ( data : any ) => {
254
+ const pc = await getRtcConnect ( data . from ) ;
255
+ const rtcIceCandidate = new RTCIceCandidate ( {
256
+ candidate : data . sdp ,
257
+ sdpMid : data . sdpMid ,
258
+ sdpMLineIndex : data . sdpMLineIndex ,
259
+ } ) ;
260
+ await pc . addIceCandidate ( rtcIceCandidate ) ;
192
261
} ;
193
262
194
263
const handleRoomConnect = ( socket : any ) => {
195
264
socketRef . value = socket ;
196
- socket . on ( SocketEventName . RoomJoin , roomJoined ) ;
197
- socket . on ( SocketEventName . RoomOffer , roomOffer ) ;
198
- socket . on ( SocketEventName . RoomAnswer , roomAnswer ) ;
199
- socket . on ( SocketEventName . RoomCandidate , roomCandidate ) ;
265
+ startConnect ( ) ;
266
+ } ;
267
+
268
+ const startConnect = ( ) => {
269
+ socketRef . value . on ( SocketEventName . RoomCreated , roomCreated ) ;
270
+ socketRef . value . on ( SocketEventName . RoomJoin , roomJoined ) ;
271
+ socketRef . value . on ( SocketEventName . RoomOffer , roomOffer ) ;
272
+ socketRef . value . on ( SocketEventName . RoomAnswer , roomAnswer ) ;
273
+ socketRef . value . on ( SocketEventName . RoomCandidate , roomCandidate ) ;
200
274
} ;
201
275
useSocket ( handleRoomConnect ) ;
202
276
203
- return { ...roomInfo } ;
277
+ return { ...roomInfo , rtcConnects , dataChanelMap , completed , startConnect } ;
204
278
} ;
205
279
206
280
// 获取房间的一些信息,例如 peer 等信息
0 commit comments