Skip to content

Commit

Permalink
Implemented onheartbeattimeout callback for ping/pong. (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmaree committed Mar 20, 2023
1 parent f30c26f commit 7d557b6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ The format is based on [keep a changelog](http://keepachangelog.com/) and this p

### Unreleased

### [2.6.1]

### Added

- You can now pass a `onheartbeattimeout` to the socket which will be called directly when a heartbeat timeout occurs. It will also still call `ondisconnect` once the socket is closed (which can take a few seconds for some browsers).


### [2.6.0]

### Added
Expand Down
6 changes: 6 additions & 0 deletions packages/nakama-js/dist/nakama-js.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3310,6 +3310,11 @@ var _DefaultSocket = class {
console.log(streamData);
}
}
onheartbeattimeout() {
if (this.verbose && window && window.console) {
console.log("Heartbeat timeout.");
}
}
send(message, sendTimeout = _DefaultSocket.DefaultSendTimeoutMs) {
const untypedMessage = message;
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -3546,6 +3551,7 @@ var _DefaultSocket = class {
if (window && window.console) {
console.error("Server unreachable from heartbeat.");
}
this.onheartbeattimeout();
this.adapter.close();
}
return;
Expand Down
6 changes: 6 additions & 0 deletions packages/nakama-js/dist/nakama-js.esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3284,6 +3284,11 @@ var _DefaultSocket = class {
console.log(streamData);
}
}
onheartbeattimeout() {
if (this.verbose && window && window.console) {
console.log("Heartbeat timeout.");
}
}
send(message, sendTimeout = _DefaultSocket.DefaultSendTimeoutMs) {
const untypedMessage = message;
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -3520,6 +3525,7 @@ var _DefaultSocket = class {
if (window && window.console) {
console.error("Server unreachable from heartbeat.");
}
this.onheartbeattimeout();
this.adapter.close();
}
return;
Expand Down
6 changes: 6 additions & 0 deletions packages/nakama-js/dist/nakama-js.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -3310,6 +3310,11 @@ var nakamajs = (() => {
console.log(streamData);
}
}
onheartbeattimeout() {
if (this.verbose && window && window.console) {
console.log("Heartbeat timeout.");
}
}
send(message, sendTimeout = _DefaultSocket.DefaultSendTimeoutMs) {
const untypedMessage = message;
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -3546,6 +3551,7 @@ var nakamajs = (() => {
if (window && window.console) {
console.error("Server unreachable from heartbeat.");
}
this.onheartbeattimeout();
this.adapter.close();
}
return;
Expand Down
6 changes: 6 additions & 0 deletions packages/nakama-js/dist/nakama-js.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4042,6 +4042,11 @@
console.log(streamData);
}
};
DefaultSocket.prototype.onheartbeattimeout = function () {
if (this.verbose && window && window.console) {
console.log("Heartbeat timeout.");
}
};
DefaultSocket.prototype.send = function (message, sendTimeout) {
var _this = this;
if (sendTimeout === void 0) { sendTimeout = DefaultSocket.DefaultSendTimeoutMs; }
Expand Down Expand Up @@ -4392,6 +4397,7 @@
if (window && window.console) {
console.error("Server unreachable from heartbeat.");
}
this.onheartbeattimeout();
this.adapter.close();
}
return [2 /*return*/];
Expand Down
8 changes: 8 additions & 0 deletions packages/nakama-js/dist/socket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,13 @@ export interface Socket {
onstreampresence: (streamPresence: StreamPresenceEvent) => void;
/** Receive stream data. */
onstreamdata: (streamData: StreamData) => void;
/**
* An application-level heartbeat timeout that fires after the client does not receive a pong from the server after the heartbeat interval.
* Most browsers maintain an internal heartbeat, in which case its unlikely you'll need to use this callback. However, Chrome does not implement an internal heartbeat.
* We fire this separately from `onclose` because heartbeats fail when there's no connectivity, and many browsers don't fire `onclose` until the closing handshake either succeeds or fails.
* In any case, be aware that `onclose` will still fire if there is a heartbeat timeout in a potentially delayed manner.
*/
onheartbeattimeout: () => void;
/** Receive channel message. */
onchannelmessage: (channelMessage: ChannelMessage) => void;
/** Receive channel presence updates. */
Expand Down Expand Up @@ -640,6 +647,7 @@ export declare class DefaultSocket implements Socket {
onstatuspresence(statusPresence: StatusPresenceEvent): void;
onstreampresence(streamPresence: StreamPresenceEvent): void;
onstreamdata(streamData: StreamData): void;
onheartbeattimeout(): void;
send(message: ChannelJoin | ChannelLeave | ChannelMessageSend | ChannelMessageUpdate | ChannelMessageRemove | CreateMatch | JoinMatch | LeaveMatch | MatchDataSend | MatchmakerAdd | MatchmakerRemove | PartyAccept | PartyClose | PartyCreate | PartyDataSend | PartyJoin | PartyJoinRequestList | PartyLeave | PartyMatchmakerAdd | PartyMatchmakerRemove | PartyPromote | PartyRemove | Rpc | StatusFollow | StatusUnfollow | StatusUpdate | Ping, sendTimeout?: number): Promise<any>;
acceptPartyMember(party_id: string, presence: Presence): Promise<void>;
addMatchmaker(query: string, min_count: number, max_count: number, string_properties?: Record<string, string>, numeric_properties?: Record<string, number>): Promise<MatchmakerTicket>;
Expand Down
15 changes: 15 additions & 0 deletions packages/nakama-js/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,14 @@ export interface Socket {
/** Receive stream data. */
onstreamdata: (streamData: StreamData) => void;

/**
* An application-level heartbeat timeout that fires after the client does not receive a pong from the server after the heartbeat interval.
* Most browsers maintain an internal heartbeat, in which case its unlikely you'll need to use this callback. However, Chrome does not implement an internal heartbeat.
* We fire this separately from `onclose` because heartbeats fail when there's no connectivity, and many browsers don't fire `onclose` until the closing handshake either succeeds or fails.
* In any case, be aware that `onclose` will still fire if there is a heartbeat timeout in a potentially delayed manner.
*/
onheartbeattimeout: () => void;

/** Receive channel message. */
onchannelmessage: (channelMessage: ChannelMessage) => void;

Expand Down Expand Up @@ -977,6 +985,12 @@ export class DefaultSocket implements Socket {
}
}

onheartbeattimeout() {
if (this.verbose && window && window.console) {
console.log("Heartbeat timeout.");
}
}

send(message: ChannelJoin | ChannelLeave | ChannelMessageSend | ChannelMessageUpdate |
ChannelMessageRemove | CreateMatch | JoinMatch | LeaveMatch | MatchDataSend | MatchmakerAdd |
MatchmakerRemove | PartyAccept | PartyClose | PartyCreate | PartyDataSend | PartyJoin |
Expand Down Expand Up @@ -1230,6 +1244,7 @@ export class DefaultSocket implements Socket {
if (window && window.console) {
console.error("Server unreachable from heartbeat.");
}
this.onheartbeattimeout();
this.adapter.close();
}

Expand Down

0 comments on commit 7d557b6

Please sign in to comment.