egos-transfer 0.2.6 → 0.2.8

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.
@@ -7,7 +7,7 @@ import {
7
7
  PING_TIMEOUT,
8
8
  UNAUTHORIZED,
9
9
  } from './constant';
10
- import { DataConnection, Peer, PeerOptions } from 'peerjs';
10
+ import { DataConnection, Peer, PeerOptions, ServerMessageType } from 'peerjs';
11
11
  import {
12
12
  IDeviceStatus,
13
13
  IMessage,
@@ -89,11 +89,11 @@ export abstract class PeerTransfer {
89
89
  // ── Peer lifecycle ──────────────────────────────────────────────
90
90
 
91
91
  async createPeer(force?: boolean): Promise<Peer> {
92
- console.info('createPeer##', this.peer?.open, force);
93
92
  this._exited = false;
94
93
  if (this.peer?.open && !force) {
95
94
  return this.peer;
96
95
  }
96
+ console.info('create peer', Date.now());
97
97
  this.destroyPeer();
98
98
  await this.reloadConfig();
99
99
 
@@ -103,7 +103,13 @@ export abstract class PeerTransfer {
103
103
  await this.waitForOpen(peer);
104
104
  this.destroyed = false;
105
105
  peer.on('close', () => {
106
- setTimeout(() => this.destroyed && this.reconnect(), PEER_CLOSE_RECONNECT_TIMEOUT);
106
+ // 仅当 Peer 非主动销毁(stop/clearInstance/重建)时才重连
107
+ setTimeout(() => {
108
+ if (this.destroyed || this._exited) {
109
+ return;
110
+ }
111
+ this.reconnect();
112
+ }, PEER_CLOSE_RECONNECT_TIMEOUT);
107
113
  });
108
114
 
109
115
  peer.on('error', (err) => {
@@ -128,6 +134,10 @@ export abstract class PeerTransfer {
128
134
  socket?.on('message', (message: IMessage) => {
129
135
  this.handleSocketMessage(message);
130
136
  });
137
+ // signaling socket 断开时主动重连(兜底:部分场景不触发 peer error)
138
+ socket?.on('disconnected', () => {
139
+ this.reconnect();
140
+ });
131
141
 
132
142
  // 启动连接健康检查
133
143
  this.connectionManager.startHealthCheck();
@@ -190,6 +200,9 @@ export abstract class PeerTransfer {
190
200
  case 'HEARTBEAT':
191
201
  this.resetHeartbeatTimer();
192
202
  break;
203
+ case 'RELOAD':
204
+ this.reconnect(true);
205
+ break;
193
206
  case 'ERROR':
194
207
  if (message.payload?.msg === CONNECTION_PEER_CONFLICT) {
195
208
  this.getStore().dispatch({
@@ -198,6 +211,8 @@ export abstract class PeerTransfer {
198
211
  });
199
212
  }
200
213
  break;
214
+ default:
215
+ break;
201
216
  }
202
217
  }
203
218
 
@@ -309,13 +324,6 @@ export abstract class PeerTransfer {
309
324
  private onConnClosed(conn: DataConnection) {
310
325
  const reqIds = this.connRequests.get(conn.connectionId);
311
326
  if (reqIds) {
312
- // for (const reqId of reqIds) {
313
- // const cb = this.requests.get(reqId);
314
- // if (cb) {
315
- // cb({ error: { message: 'connection closed', code: 'CONN_CLOSED' } } as any);
316
- // this.requests.delete(reqId);
317
- // }
318
- // }
319
327
  this.connRequests.delete(conn.connectionId);
320
328
  }
321
329
  }
@@ -351,7 +359,6 @@ export abstract class PeerTransfer {
351
359
  if (this.destroyed) {
352
360
  return { ...payload, body: {}, error: { message: 'destroyed', code: 'CONN_CLOSED' } } as any;
353
361
  }
354
-
355
362
  const conn = await this.connectToPeer(peerId);
356
363
  if (!this.isValidConnection(conn)) {
357
364
  return {
@@ -554,6 +561,7 @@ export abstract class PeerTransfer {
554
561
  // ── Router integration ──────────────────────────────────────────
555
562
 
556
563
  async onRequest(conn: DataConnection, message: PeerMessage) {
564
+ console.info('onRequest===>', message.route);
557
565
  this.resetIdleTimer();
558
566
  try {
559
567
  await this.router.run(conn, message);
@@ -563,6 +571,7 @@ export abstract class PeerTransfer {
563
571
  }
564
572
 
565
573
  async onResponse(_conn: DataConnection, message: PeerMessage) {
574
+ console.info('onResponse<====', message.route);
566
575
  this.resetIdleTimer();
567
576
  // 优先检查 health check ping 回调
568
577
  const cb = this.requests.get(message.requestId);
@@ -618,7 +627,7 @@ export abstract class PeerTransfer {
618
627
  }
619
628
  this.heartbeatTimer = setTimeout(() => {
620
629
  if (!this.destroyed) {
621
- this.reconnect(true);
630
+ this.reconnect();
622
631
  }
623
632
  }, HEARTBEAT_TIMEOUT);
624
633
  }
@@ -668,4 +677,12 @@ export abstract class PeerTransfer {
668
677
  this.peer?.removeAllListeners();
669
678
  this.peer = undefined;
670
679
  }
680
+
681
+ askReload(peerId: string) {
682
+ this.peer?.socket.send({
683
+ type: 'RELOAD',
684
+ payload: {},
685
+ dst: peerId,
686
+ });
687
+ }
671
688
  }