diff --git a/src/store/network/actions.ts b/src/store/network/actions.ts index 4a4de2f..b48570d 100644 --- a/src/store/network/actions.ts +++ b/src/store/network/actions.ts @@ -1,7 +1,14 @@ import { ActionTree } from "vuex"; import { AppState } from "../types"; import { NetworkState, StartServerOptions, ConnectOptions } from "./types"; -import { PeerServer, LocalClient, PeerClient } from "@/network"; +import { + PeerServer, + LocalClient, + PeerClient, + NetworkMessage, + Client, + ChatMessage +} from "@/network"; const actions: ActionTree = { startServer({ commit }, options: StartServerOptions) { @@ -23,6 +30,21 @@ const actions: ActionTree = { commit("connectionError", err); }); client.connect(options.serverID); + }, + + sendChatMessage({ commit, dispatch, getters }, message: ChatMessage) { + if (getters.connectionType == "none") { + throw new Error("not connected"); + } + dispatch("sendMessage", message); + commit("receivedChatMessage", message); + }, + + sendMessage({ getters }, message: NetworkMessage) { + if (getters.connectionType == "none") { + throw new Error("not connected"); + } + (getters.client as Client).send(message); } }; diff --git a/src/store/network/getters.ts b/src/store/network/getters.ts index 66454e6..d69b772 100644 --- a/src/store/network/getters.ts +++ b/src/store/network/getters.ts @@ -1,6 +1,7 @@ import { GetterTree } from "vuex"; import { AppState } from "../types"; import { NetworkState } from "./types"; +import { Client } from "@/network"; const getters: GetterTree = { peerID(state): string | null { @@ -23,6 +24,16 @@ const getters: GetterTree = { return null; }, + client(state): Client | null { + switch (state.peerType) { + case "server": + return state.local; + case "client": + return state.peer; + } + return null; + }, + connectionType(state): "client" | "server" | "none" { return state.peerType; }, diff --git a/src/store/network/mutations.ts b/src/store/network/mutations.ts index 7d8e0bb..dae7c3d 100644 --- a/src/store/network/mutations.ts +++ b/src/store/network/mutations.ts @@ -5,7 +5,7 @@ import { ClientNetworkState, ConnectionStatus } from "./types"; -import { LocalClient, PeerServer, PeerClient } from "@/network"; +import { LocalClient, PeerServer, PeerClient, ChatMessage } from "@/network"; const mutations: MutationTree = { becomeServer(state, payload: { local: LocalClient; server: PeerServer }) { @@ -28,6 +28,10 @@ const mutations: MutationTree = { connectionError(state, error) { (state as ClientNetworkState).connectionStatus = "error"; (state as ClientNetworkState).connectionError = error; + }, + + receivedChatMessage(state, message: ChatMessage) { + state.chatLog.push(message); } }; diff --git a/src/store/network/types.ts b/src/store/network/types.ts index c969aa2..5d868eb 100644 --- a/src/store/network/types.ts +++ b/src/store/network/types.ts @@ -3,7 +3,8 @@ import { PeerServer, LocalClient, RoomInfo, - PeerMetadata + PeerMetadata, + ChatMessage } from "@/network"; import Peer from "peerjs"; @@ -13,12 +14,6 @@ export type ConnectionStatus = | "disconnected" | "error"; -export interface ChatMessage { - who: string; - to: string; - message: string; -} - export interface SharedNetworkState { chatLog: ChatMessage[]; }