Class MultiPlayerServer

Classdesc

MultiPlayerServer class, used for constructing, running, and managing a multiplayer server

Example

const multiplayerSever = new MultiPlayerServer({

// server config
httpServer: server, // const server = http.createServer(=);
showPlayerLabels: true,
port: 3000,
// game config
newPlayerObject: {
class: Polygon,
options: {
points: [[0,0],[100,0],[100,100],[0,100]],
backgroundColor: "red",
}
},
sceneManager: new MultiPlayerSceneManager({
initialScene: new Scene({
lighting: true,
lightOptions: {
ambient: 0.2
}
}),
showPlayerLabels: true
}),

// events
onNewConnection: () => {
console.log('new connection');
},
onDisconnect: () => {
console.log('disconnected');
},
onNewPlayer: (socket, id, data) => {
console.log("new player", id, data);
gameObject = new Polygon({
points: [[0,0],[100,0],[100,100],[0,100]],
backgroundColor: "red",
});
return new Promise((resolve, reject) => {
socket.on("send_username", (username) => {
console.log("got username", username)
resolve([gameObject, username])
});
})
},

// input
inputHandler: new MultiPlayerInputHandler({
monitors: [
new ServerInputHandler({
key: "w",
on: (socket, playerGameObject) => {
playerGameObject.move([0, -10])
}
}),
new ServerInputHandler({
key: "a",
on: (socket, playerGameObject) => {
playerGameObject.move([-10,0])
}
}),
new ServerInputHandler({
key: "s",
on: (socket, playerGameObject) => {
playerGameObject.move([0, 10])
}
}),
new ServerInputHandler({
key: "d",
on: (socket, playerGameObject) => {
playerGameObject.move([10,0])
}
})

]
})
});

Constructors

Properties

InputHandler to use on the server

io: any

Socket.io instance

newPlayerObject: GameObjectBuilder

GameObject to use when a new player joins the server

onDisconnect: Function

Function to run when a connection is disconnected

onNewConnection: Function

Function to run when a new connection is made

onNewPlayer: ((socket, id, data) => [GameObject, string] | Promise<[GameObject, string]>)

Function to run when a new player joins the server. Should return a GameObject and a label for the player, or a promise that will resolve into a GameObject and a label for the player. If no function is provided, the GameObject will be a clone of newPlayerObject

Type declaration

    • (socket, id, data): [GameObject, string] | Promise<[GameObject, string]>
    • Function to run when a new player joins the server. Should return a GameObject and a label for the player, or a promise that will resolve into a GameObject and a label for the player. If no function is provided, the GameObject will be a clone of newPlayerObject

      Parameters

      • socket: any
      • id: string
      • data: object

      Returns [GameObject, string] | Promise<[GameObject, string]>

SceneManager instance that the server is using

socketConnections: Connection[]

Array of socket connections that the server is connected to

tickSpeed: number

Speed (in ms) of the server tick (Default: 1000/60)

Methods

  • Adds a connection and fires onNewConnection event

    Parameters

    • socket: any

      Socket.io socket to add to the server

    Returns void

  • Broadcasts data to all connected sockets

    Parameters

    • data: any

      Data to broadcast

    Returns void

  • Broadcasts an event to all connected sockets. "key_down", "key_up", "player_data", and "player_initialized" are reserved events.

    Parameters

    • event: string

      Event to broadcast to all connected sockets

    • data: any

      Data to broadcast to all connected sockets

    Returns void

  • Emits data to a given socket

    Parameters

    • socket: any

      Either socket.io socket, label of a connection, or a connection to emit data to

    • data: any

      The data to emit

    Returns void

  • Emits an event to a given socket. "key_down", "key_up", "player_data", and "player_initialized" are reserved events.

    Parameters

    • socket: any

      Either socket.io socket, label of a connection, or a connection to emit an event to

    • event: string

      Event to fire

    • data: any

      Data to emit

    Returns void

  • Listens for an event on the server

    Parameters

    • event: string

      Event to listen for

    • callback: Function

      Callback function to call when the event is fired

    Returns void

  • Ticks the server 1 tick (called automatically ever tickSpeed ms)

    Returns void

Generated using TypeDoc