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; }