Basic networking #9
2 changed files with 16 additions and 3 deletions
|
@ -18,7 +18,9 @@ export default class PeerServer extends NetworkPeer {
|
||||||
super();
|
super();
|
||||||
this.room = {
|
this.room = {
|
||||||
info: roomInfo,
|
info: roomInfo,
|
||||||
players: {}
|
players: {
|
||||||
|
//TODO Add local player
|
||||||
|
}
|
||||||
};
|
};
|
||||||
this.peer.on("connection", this._connection);
|
this.peer.on("connection", this._connection);
|
||||||
}
|
}
|
||||||
|
@ -69,6 +71,7 @@ export default class PeerServer extends NetworkPeer {
|
||||||
private addPlayer(conn: DataConnection) {
|
private addPlayer(conn: DataConnection) {
|
||||||
const playerName = conn.metadata.name;
|
const playerName = conn.metadata.name;
|
||||||
this.room.players[playerName] = {
|
this.room.players[playerName] = {
|
||||||
|
kind: "remote",
|
||||||
name: conn.metadata.name,
|
name: conn.metadata.name,
|
||||||
conn: conn
|
conn: conn
|
||||||
};
|
};
|
||||||
|
@ -97,7 +100,11 @@ export default class PeerServer extends NetworkPeer {
|
||||||
private broadcast<T>(message: T) {
|
private broadcast<T>(message: T) {
|
||||||
for (const playerName in this.room.players) {
|
for (const playerName in this.room.players) {
|
||||||
const player = this.room.players[playerName];
|
const player = this.room.players[playerName];
|
||||||
this.send<T>(player.conn, message);
|
if (player.kind == "remote") {
|
||||||
|
this.send<T>(player.conn, message);
|
||||||
|
} else {
|
||||||
|
//TODO Local wrapper
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
import { DataConnection } from "peerjs";
|
import { DataConnection } from "peerjs";
|
||||||
|
|
||||||
|
export interface LocalPlayer {
|
||||||
|
kind: "local";
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface NetworkPlayer {
|
export interface NetworkPlayer {
|
||||||
|
kind: "remote";
|
||||||
name: string;
|
name: string;
|
||||||
conn: DataConnection;
|
conn: DataConnection;
|
||||||
}
|
}
|
||||||
|
@ -11,7 +17,7 @@ export interface PeerMetadata {
|
||||||
|
|
||||||
export interface Room {
|
export interface Room {
|
||||||
info: RoomInfo;
|
info: RoomInfo;
|
||||||
players: Record<string, NetworkPlayer>;
|
players: Record<string, NetworkPlayer | LocalPlayer>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RoomInfo {
|
export interface RoomInfo {
|
||||||
|
|
Loading…
Reference in a new issue