Socket
Socket
Sign inDemoInstall

@brajkowski/connect4-multiplayer-client

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@brajkowski/connect4-multiplayer-client

A websocket-based client that connects users to connect 4 multiplayer game sessions.


Version published
Weekly downloads
2
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

Connect 4 Multiplayer: Client

Build npm:latest npm:beta semantic-release

A websocket-based client that connects users to connect 4 multiplayer game sessions.

Installation

Using npm:

$ npm i --save @brajkowski/connect4-multiplayer-client

Usage

This client is designed to connect to an instance of the connect4-multiplayer-server. To use the client, simply create an instance and open a connection to the server:

const client: Connect4Client = new Connect4Client();
client.open('wss://my-multiplayer-server-instance.myhost.com');

From there you can either create a new multiplayer session or join an existing session:

client.createSession(username);
// or
client.joinSession(sessionName, username);

The Connect4Client object provides methods to perform actions such as placing a chip or leaving the session:

client.makeMove(0); // Sends action to server to place chip in first column.
client.quit(); // Gracefully leave the multiplayer session and alert the server.

Finally there are several methods to write custom callbacks in order to handle actions that are coming from the server:

onOpen(callback: () => any)
onSessionCreated(callback: (sessionName: string) => any)
onSessionJoined(callback: (opponentUsername: string) => any)
onSessionNotFound(callback: () => any)
onOpponentJoin(callback: (username: string) => any)
onOpponentMove(callback: (column: number) => any)
onOpponentQuit(callback: () => any)
onGameRestart(callback: (thisClientStartsFirst: boolean) => any)
onSessionEnded(callback: () => any)
onClose(callback: () => any)

Example: Tying it all together

const client: Connect4Client = new Connect4Client();
client.onOpen(() => client.createSession(username));
client.onSessionCreated((sessionName) => shareSessionName(sessionName));
client.onOpponentJoin((username) => {
  setOpponentUsername(username);
  promptUserForMove(); // The session creator always goes first.
});
client.onOpponentMove((column) => {
  trackOpponentChipPlacement(column);
  promptUserForMove();
});
client.onGameRestart((thisClientStartsFirst) => {
  resetGameState();
  if (thisClientStartsFirst) promptUserForMove(); // Server decides who goes first.
});
client.open('wss://my-multiplayer-server-instance.myhost.com');

Building from Source

Using npm:

$ npm run build

will produce the compiled library under /dist.

Keywords

FAQs

Package last updated on 16 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc