NetworkDevice interfaces with a server (see the "multiplayer-server" folder) via socket.io to send and receive network events.

Constructors

  • Construct a NetworkDevice, for interacting with a remote server via Socket.IO. Note that by default, the network device is not connected to anything.

    Returns NetworkDevice

Properties

broadcast: ((msg) => void) = ...

An event handler for when a remote user sends a message

Type declaration

    • (msg): void
    • An event handler for when a remote user sends a message

      Parameters

      • msg: string

      Returns void

evtConnected: (() => void) = ...

An event handler for when the socket initially connects

Type declaration

    • (): void
    • An event handler for when the socket initially connects

      Returns void

evtDisconnected: ((msg) => void) = ...

An event handler for when the socket disconnects

Type declaration

    • (msg): void
    • An event handler for when the socket disconnects

      Parameters

      • msg: string

      Returns void

evtUserJoined: ((msg) => void) = ...

An event handler for when a remote user joins the room

Type declaration

    • (msg): void
    • An event handler for when a remote user joins the room

      Parameters

      • msg: string

      Returns void

evtUserLeft: ((msg) => void) = ...

An event handler for when a remote user exits the room

Type declaration

    • (msg): void
    • An event handler for when a remote user exits the room

      Parameters

      • msg: string

      Returns void

resCreated: ((msg) => void) = ...

An event handler for when we succeed in making a new room

Type declaration

    • (msg): void
    • An event handler for when we succeed in making a new room

      Parameters

      • msg: string

      Returns void

resErrFormat: ((msg) => void) = ...

An event handler for errors due to incorrectly formatted requests

Type declaration

    • (msg): void
    • An event handler for errors due to incorrectly formatted requests

      Parameters

      • msg: string

      Returns void

resErrHasRoom: ((msg) => void) = ...

An event handler for when we request a room but already have one

Type declaration

    • (msg): void
    • An event handler for when we request a room but already have one

      Parameters

      • msg: string

      Returns void

resErrHasUser: ((msg) => void) = ...

An event handler for when we try to log in when we're already logged in

Type declaration

    • (msg): void
    • An event handler for when we try to log in when we're already logged in

      Parameters

      • msg: string

      Returns void

resErrNoRoom: ((msg) => void) = ...

An event handler for errors due to not having a room

Type declaration

    • (msg): void
    • An event handler for errors due to not having a room

      Parameters

      • msg: string

      Returns void

resErrNoUser: ((msg) => void) = ...

An event handler for errors due to not logging in first

Type declaration

    • (msg): void
    • An event handler for errors due to not logging in first

      Parameters

      • msg: string

      Returns void

resErrUserTaken: ((msg) => void) = ...

An event handler for when the server says our username is taken

Type declaration

    • (msg): void
    • An event handler for when the server says our username is taken

      Parameters

      • msg: string

      Returns void

resJoined: ((msg) => void) = ...

An event handler for when we succeed in joining an existing room

Type declaration

    • (msg): void
    • An event handler for when we succeed in joining an existing room

      Parameters

      • msg: string

      Returns void

resLoggedIn: ((msg) => void) = ...

An event handler for when a login request succeeds

Type declaration

    • (msg): void
    • An event handler for when a login request succeeds

      Parameters

      • msg: string

      Returns void

resLoggedOut: ((msg) => void) = ...

An event handler for when a logout request succeeds

Type declaration

    • (msg): void
    • An event handler for when a logout request succeeds

      Parameters

      • msg: string

      Returns void

resSent: ((msg) => void) = ...

An event handler for when a broadcast is sent successfully

Type declaration

    • (msg): void
    • An event handler for when a broadcast is sent successfully

      Parameters

      • msg: string

      Returns void

socket: undefined | Socket<DefaultEventsMap, DefaultEventsMap>

A socket for communicating with the server

state: NetworkState = NetworkState.Start

The current state of the network stack

Methods

  • Connect to a server and start sending/receiving events

    Parameters

    • serverAddress: string

      The address of the server to connect to

    Returns void

  • Broadcast a message to all other recipients in the room

    Parameters

    • message: string

      The message to send

    Returns void

  • Forcibly disconnect from the server

    Returns void

  • Send a message to the server to try to create a new room

    Returns void

  • Send a message to the server to try to join an existing room

    Parameters

    • roomId: string

      The Id of the room to join

    Returns void

  • Send a message to the server to try to log in

    Parameters

    • userId: string

      The desired userId

    • userName: string

      The desired userName

    Returns void

  • Send a request to the server to log out

    Returns void

  • Reset all of the event handlers. This is a good thing to do after disconnecting.

    Returns void