Basic networking #9
2 changed files with 51 additions and 4 deletions
|
@ -11,7 +11,10 @@ import {
|
||||||
RoomInfoMessage,
|
RoomInfoMessage,
|
||||||
Player,
|
Player,
|
||||||
NetworkMessage,
|
NetworkMessage,
|
||||||
RenameMessage
|
RenameMessage,
|
||||||
|
LeaveMessage,
|
||||||
|
NetworkPlayer,
|
||||||
|
AckMessage
|
||||||
} from "./types";
|
} from "./types";
|
||||||
import LocalClient from "./local";
|
import LocalClient from "./local";
|
||||||
|
|
||||||
|
@ -133,11 +136,38 @@ export default class PeerServer extends NetworkPeer {
|
||||||
// Notify other players
|
// Notify other players
|
||||||
this.broadcast<JoinMessage>({
|
this.broadcast<JoinMessage>({
|
||||||
kind: "player-joined",
|
kind: "player-joined",
|
||||||
name: conn.metadata.name
|
name: playerName
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _received(player: Player, data: NetworkMessage) {}
|
private removePlayer(player: NetworkPlayer) {
|
||||||
|
// Tell the player everything's fine
|
||||||
|
this.send<AckMessage>(player.conn, { kind: "ok", what: "leave-req" });
|
||||||
|
|
||||||
|
// Close connection with player
|
||||||
|
player.conn.close();
|
||||||
|
|
||||||
|
// Notify other players
|
||||||
|
this.broadcast<LeaveMessage>({
|
||||||
|
kind: "player-left",
|
||||||
|
name: player.name
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private _received(player: Player, data: NetworkMessage) {
|
||||||
|
switch (data.kind) {
|
||||||
|
// Player is leaving!
|
||||||
|
case "leave-req":
|
||||||
|
// If we're leaving, end the server
|
||||||
|
if (player.kind == "local") {
|
||||||
|
//TODO
|
||||||
|
} else {
|
||||||
|
// Remove and disconnect player
|
||||||
|
this.removePlayer(player);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private get playerCount(): number {
|
private get playerCount(): number {
|
||||||
return Object.keys(this.room.players).length;
|
return Object.keys(this.room.players).length;
|
||||||
|
|
|
@ -63,10 +63,13 @@ type DraftInfo = {
|
||||||
export type NetworkMessage =
|
export type NetworkMessage =
|
||||||
| PasswordRequest
|
| PasswordRequest
|
||||||
| PasswordResponse
|
| PasswordResponse
|
||||||
|
| LeaveRequest
|
||||||
| ErrorMessage
|
| ErrorMessage
|
||||||
| RoomInfoMessage
|
| RoomInfoMessage
|
||||||
| JoinMessage
|
| JoinMessage
|
||||||
| RenameMessage;
|
| LeaveMessage
|
||||||
|
| RenameMessage
|
||||||
|
| AckMessage;
|
||||||
|
|
||||||
export interface PasswordRequest {
|
export interface PasswordRequest {
|
||||||
kind: "password-req";
|
kind: "password-req";
|
||||||
|
@ -83,17 +86,31 @@ export interface RoomInfoMessage {
|
||||||
players: string[];
|
players: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface LeaveRequest {
|
||||||
|
kind: "leave-req";
|
||||||
|
}
|
||||||
|
|
||||||
export interface JoinMessage {
|
export interface JoinMessage {
|
||||||
kind: "player-joined";
|
kind: "player-joined";
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface LeaveMessage {
|
||||||
|
kind: "player-left";
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface RenameMessage {
|
export interface RenameMessage {
|
||||||
kind: "rename";
|
kind: "rename";
|
||||||
oldname: string;
|
oldname: string;
|
||||||
newname: string;
|
newname: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AckMessage {
|
||||||
|
kind: "ok";
|
||||||
|
what: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ErrorMessage {
|
export interface ErrorMessage {
|
||||||
kind: "error";
|
kind: "error";
|
||||||
error: string;
|
error: string;
|
||||||
|
|
Loading…
Reference in a new issue