Integrate draft/network stack via Vuex #26

Merged
hamcha merged 5 commits from feature/vuex into master 2019-09-17 13:22:43 +00:00
4 changed files with 23 additions and 2 deletions
Showing only changes of commit d230808af8 - Show all commits

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