From d230808af8bdb334f98f0765a757c24ab9851a2c Mon Sep 17 00:00:00 2001 From: Hamcha Date: Tue, 17 Sep 2019 15:04:27 +0200 Subject: [PATCH] Add connection error handling --- src/network/PeerClient.ts | 6 ++++++ src/store/network/actions.ts | 8 +++++++- src/store/network/mutations.ts | 9 +++++++++ src/store/network/types.ts | 2 +- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/network/PeerClient.ts b/src/network/PeerClient.ts index f956b6c..2dc1a09 100644 --- a/src/network/PeerClient.ts +++ b/src/network/PeerClient.ts @@ -25,6 +25,12 @@ export class PeerClient extends Client { this.connection.on("open", () => { this.emit("connected"); }); + this.connection.on("close", () => { + this.emit("disconnected"); + }); + this.connection.on("error", err => { + this.emit("error", err); + }); this.connection.on("data", data => { this._received(data); }); diff --git a/src/store/network/actions.ts b/src/store/network/actions.ts index bbf4ba6..dfc9f4b 100644 --- a/src/store/network/actions.ts +++ b/src/store/network/actions.ts @@ -13,9 +13,15 @@ const actions: ActionTree = { connect({ commit }, options: ConnectOptions) { const client = new PeerClient(options.playerInfo, options._customPeer); commit("becomeClient", { peer: client }); - client.once("connected", () => { + client.on("connected", () => { commit("connected"); }); + client.on("disconnected", () => { + commit("disconnected"); + }); + client.on("error", err => { + commit("connectionError", err); + }); client.connect(options.serverID); } }; diff --git a/src/store/network/mutations.ts b/src/store/network/mutations.ts index b40d8b1..5a0d135 100644 --- a/src/store/network/mutations.ts +++ b/src/store/network/mutations.ts @@ -23,6 +23,15 @@ const mutations: MutationTree = { connected(state) { (state as ClientNetworkState).connectionStatus = "connected"; + }, + + disconnected(state) { + (state as ClientNetworkState).connectionStatus = "disconnected"; + }, + + connectionError(state, error) { + (state as ClientNetworkState).connectionStatus = "error"; + (state as ClientNetworkState).connectionError = error; } }; diff --git a/src/store/network/types.ts b/src/store/network/types.ts index d5f813b..4c8e555 100644 --- a/src/store/network/types.ts +++ b/src/store/network/types.ts @@ -23,7 +23,7 @@ export interface NoNetworkState extends SharedNetworkState { export interface ClientNetworkState extends SharedNetworkState { peerType: "client"; - connectionStatus: "connecting" | "connected" | "error"; + connectionStatus: "connecting" | "connected" | "disconnected" | "error"; connectionError?: Error; peer: PeerClient; }