File tree 2 files changed +41
-0
lines changed
2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -217,6 +217,22 @@ export class Socket<
217
217
* });
218
218
*/
219
219
public connected : boolean = false ;
220
+ /**
221
+ * The identifier of the current packet. Multiple retries of the same packet will have the same identifier, in order
222
+ * to allow deduplication (only process a packet once).
223
+ *
224
+ * @example
225
+ * io.on("connection", (socket) => {
226
+ * socket.on("my-event", async (payload, callback) => {
227
+ * const offset = socket.currentOffset;
228
+ *
229
+ * await insertInDatabase(payload, offset);
230
+ *
231
+ * callback();
232
+ * })
233
+ * });
234
+ */
235
+ public currentOffset : string ;
220
236
221
237
/**
222
238
* The session ID, which must not be shared (unlike {@link id}).
@@ -652,6 +668,7 @@ export class Socket<
652
668
if ( null != packet . id ) {
653
669
debug ( "attaching ack callback to event" ) ;
654
670
args . push ( this . ack ( packet . id ) ) ;
671
+ this . currentOffset = `${ this . id } -${ packet . id } ` ;
655
672
}
656
673
657
674
if ( this . _anyListeners && this . _anyListeners . length ) {
Original file line number Diff line number Diff line change @@ -1105,4 +1105,28 @@ describe("socket", () => {
1105
1105
socket3 . on ( "disconnect" , partialDone ) ;
1106
1106
} ) ;
1107
1107
} ) ;
1108
+
1109
+ // TODO: enable once a new version of the socket.io-client package is released
1110
+ // it("should retry with the same packet ID", (done) => {
1111
+ // const io = new Server(0);
1112
+ // let counter = 0;
1113
+ //
1114
+ // io.on("connection", (socket) => {
1115
+ // socket.on("my-event", (cb) => {
1116
+ // expect(socket.currentOffset).to.eql(socket.id + "-0");
1117
+ // if (++counter === 3) {
1118
+ // cb();
1119
+ //
1120
+ // success(done, io, clientSocket);
1121
+ // }
1122
+ // });
1123
+ // });
1124
+ //
1125
+ // const clientSocket = createClient(io, "/", {
1126
+ // retries: 10,
1127
+ // ackTimeout: 20,
1128
+ // });
1129
+ //
1130
+ // clientSocket.emit("my-event");
1131
+ // });
1108
1132
} ) ;
You can’t perform that action at this time.
0 commit comments