Add connection error handling
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Hamcha 2019-09-17 15:04:27 +02:00
parent 4da4e06871
commit d230808af8
Signed by: hamcha
GPG key ID: 44AD3571EB09A39E
4 changed files with 23 additions and 2 deletions

View file

@ -25,6 +25,12 @@ export class PeerClient extends Client {
this.connection.on("open", () => { this.connection.on("open", () => {
this.emit("connected"); 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.connection.on("data", data => {
this._received(data); this._received(data);
}); });

View file

@ -13,9 +13,15 @@ const actions: ActionTree<NetworkState, AppState> = {
connect({ commit }, options: ConnectOptions) { connect({ commit }, options: ConnectOptions) {
const client = new PeerClient(options.playerInfo, options._customPeer); const client = new PeerClient(options.playerInfo, options._customPeer);
commit("becomeClient", { peer: client }); commit("becomeClient", { peer: client });
client.once("connected", () => { client.on("connected", () => {
commit("connected"); commit("connected");
}); });
client.on("disconnected", () => {
commit("disconnected");
});
client.on("error", err => {
commit("connectionError", err);
});
client.connect(options.serverID); client.connect(options.serverID);
} }
}; };

View file

@ -23,6 +23,15 @@ const mutations: MutationTree<NetworkState> = {
connected(state) { connected(state) {
(state as ClientNetworkState).connectionStatus = "connected"; (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;
} }
}; };

View file

@ -23,7 +23,7 @@ export interface NoNetworkState extends SharedNetworkState {
export interface ClientNetworkState extends SharedNetworkState { export interface ClientNetworkState extends SharedNetworkState {
peerType: "client"; peerType: "client";
connectionStatus: "connecting" | "connected" | "error"; connectionStatus: "connecting" | "connected" | "disconnected" | "error";
connectionError?: Error; connectionError?: Error;
peer: PeerClient; peer: PeerClient;
} }