Add connection error handling
This commit is contained in:
parent
4da4e06871
commit
d230808af8
4 changed files with 23 additions and 2 deletions
|
@ -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);
|
||||||
});
|
});
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue