File tree Expand file tree Collapse file tree 2 files changed +20
-11
lines changed
packages/transport-webrtc/src Expand file tree Collapse file tree 2 files changed +20
-11
lines changed Original file line number Diff line number Diff line change 11import { AbstractStreamMuxer } from '@libp2p/utils'
2- import { pEvent } from 'p-event'
32import { MUXER_PROTOCOL } from './constants.js'
43import { createStream , WebRTCStream } from './stream.js'
54import type { DataChannelOptions } from './index.js'
@@ -157,16 +156,6 @@ export class DataChannelMuxer extends AbstractStreamMuxer<WebRTCStream> implemen
157156
158157 this . log ( 'open channel %d for protocol %s' , channel . id , options ?. protocol )
159158
160- if ( channel . readyState !== 'open' ) {
161- this . log ( 'channel ready state is "%s" and not "open", waiting for "open" event before creating stream' , channel . readyState )
162- await pEvent ( channel , 'open' , {
163- rejectionEvents : [
164- 'close' ,
165- 'error'
166- ]
167- } )
168- }
169-
170159 const stream = createStream ( {
171160 ...options ,
172161 ...this . dataChannelOptions ,
Original file line number Diff line number Diff line change @@ -107,6 +107,26 @@ export class WebRTCStream extends AbstractStream {
107107 }
108108 }
109109 this . addEventListener ( 'close' , cleanUpDatachannelOnClose )
110+
111+ // chrome can receive message events before the open even is fired - calling
112+ // code needs to attach message event listeners before these events occur
113+ // but we need to wait before sending any data so this has to be done async
114+ if ( this . channel . readyState !== 'open' ) {
115+ this . log ( 'channel ready state is "%s" and not "open", waiting for "open" event before sending data' , this . channel . readyState )
116+ pEvent ( this . channel , 'open' , {
117+ rejectionEvents : [
118+ 'close' ,
119+ 'error'
120+ ]
121+ } )
122+ . then ( ( ) => {
123+ this . log ( 'channel ready state is now "%s", dispatching drain' , this . channel . readyState )
124+ this . safeDispatchEvent ( 'drain' )
125+ } )
126+ . catch ( err => {
127+ this . abort ( err . error ?? err )
128+ } )
129+ }
110130 }
111131
112132 sendNewStream ( ) : void {
You can’t perform that action at this time.
0 commit comments