@@ -78,6 +78,25 @@ function brokerAvailable() {
7878 return typeof Peer !== 'undefined' ;
7979}
8080
81+ // If the broker never confirms our registration, surface an error instead of
82+ // hanging silently on the placeholder.
83+ const BROKER_TIMEOUT_MS = 15000 ;
84+ let brokerTimer = null ;
85+
86+ function armBrokerWatchdog ( statusId ) {
87+ clearTimeout ( brokerTimer ) ;
88+ brokerTimer = setTimeout ( ( ) => {
89+ $ ( statusId ) . textContent =
90+ 'Could not reach the signaling broker. Check your internet, or use the manual code.' ;
91+ setStatus ( 'Connection error' , 'error' ) ;
92+ } , BROKER_TIMEOUT_MS ) ;
93+ }
94+
95+ function clearBrokerWatchdog ( ) {
96+ clearTimeout ( brokerTimer ) ;
97+ brokerTimer = null ;
98+ }
99+
81100// Create side: register a code and wait for the other device to connect.
82101function startEasyHost ( ) {
83102 if ( ! brokerAvailable ( ) ) {
@@ -91,8 +110,10 @@ function startEasyHost() {
91110
92111 const code = randomCode ( ) ;
93112 peer = new Peer ( PEER_PREFIX + code , { debug : 1 } ) ;
113+ armBrokerWatchdog ( 'create-easy-status' ) ;
94114
95115 peer . on ( 'open' , ( ) => {
116+ clearBrokerWatchdog ( ) ;
96117 $ ( 'my-code' ) . textContent = code ;
97118 setStatus ( 'Waiting for the other device…' , 'connecting' ) ;
98119 $ ( 'create-easy-status' ) . textContent = 'Waiting for the other device to join…' ;
@@ -105,6 +126,7 @@ function startEasyHost() {
105126 } ) ;
106127
107128 peer . on ( 'error' , ( err ) => {
129+ clearBrokerWatchdog ( ) ;
108130 if ( err . type === 'unavailable-id' ) {
109131 // Rare clash on the shared broker — try a fresh code.
110132 peer . destroy ( ) ;
@@ -129,13 +151,18 @@ function startEasyJoin() {
129151 $ ( 'join-easy-status' ) . textContent = 'Connecting…' ;
130152 setStatus ( 'Connecting…' , 'connecting' ) ;
131153 peer = new Peer ( { debug : 1 } ) ;
154+ armBrokerWatchdog ( 'join-easy-status' ) ;
132155
133156 peer . on ( 'open' , ( ) => {
134- const conn = peer . connect ( PEER_PREFIX + code , { reliable : true , serialization : 'none' } ) ;
157+ clearBrokerWatchdog ( ) ;
158+ // 'raw' passes strings and binary through untouched; we do our own
159+ // chunking. (Note: PeerJS has no 'none' serializer despite the enum.)
160+ const conn = peer . connect ( PEER_PREFIX + code , { reliable : true , serialization : 'raw' } ) ;
135161 adoptPeerConnection ( conn ) ;
136162 } ) ;
137163
138164 peer . on ( 'error' , ( err ) => {
165+ clearBrokerWatchdog ( ) ;
139166 if ( err . type === 'peer-unavailable' ) {
140167 $ ( 'join-easy-status' ) . textContent = 'No device is waiting on that code. Double-check it.' ;
141168 setStatus ( 'Not connected' , 'idle' ) ;
@@ -452,6 +479,7 @@ function addOutgoingNote(text) {
452479// ---- Mode toggles & reset -------------------------------------------------
453480
454481function teardownConnections ( ) {
482+ clearBrokerWatchdog ( ) ;
455483 if ( channel ) try { channel . close ( ) ; } catch ( _ ) { }
456484 if ( pc ) try { pc . close ( ) ; } catch ( _ ) { }
457485 if ( peer ) try { peer . destroy ( ) ; } catch ( _ ) { }
0 commit comments