npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hathora/server-sdk

v1.1.0

Published

![Hathora Logo](https://user-images.githubusercontent.com/7004280/223056895-c16419d2-2b91-4013-82f0-7616c84d31b7.svg)

Downloads

138

Readme

Hathora Logo

Hathora TypeScript Server SDK

The Hathora TypeScript Server SDK provides a set of tools for handling connected users to your game server, and interfacing with their cooresponding clients.

Installation

Run the following from your server app's directory to install the SDK as an NPM dependency:

npm install @hathora/server-sdk

Getting an APP_ID and APP_SECRET

Visit console.hathora.dev and login if you haven't already done so.

You will be greeted with a project list screen, if you have already created an app the App ID and App Secret can be copied directly from the list...

A screenshot of the Hathora console's project list

If you have not yet created an app, click the Create Application button. You will then be faced with the following screen...

A screenshot of the Hathora console's Create Application screen

After entering a valid name and creating your application, it's App ID and App Secret will be available to be copied.

Defining Rooms

import { UserId, RoomId, Application, startServer, verifyJwt } from "@hathora/server-sdk";

// A type to define a player, internal to the server
type InternalPlayer = {
  id: UserId;
  x: number;
  y: number;
};

// A type to define the state of a single room, internal to the server
type InternalState = {
  players: InternalPlayer[];
};

// A map which the server uses to contain all rooms
const rooms: Map<RoomId, InternalState> = new Map();

Defining the Store

// ...

const store: Application = {
  // A function called by Hathora to verify a connecting user's token
  verifyToken(token: string): UserId | undefined {
    const userId = verifyJwt(token, "YOUR_HATHORA_APP_SECRET");

    if (userId === undefined) {
      console.error("Failed to verify token", token);
    }

    return userId;
  },

  // Called when a new user connects to your server, this is a good place to init rooms and spawn players
  subscribeUser(roomId: RoomId, userId: string): void {
    // Make sure the room exists (or create one if not)
    if (!rooms.has(roomId)) {
      console.log("Creating new room...");

      rooms.set(roomId, {
        players: []
      });
    }

    const game = rooms.get(roomId)!;

    // Make sure the player hasn't already spawned, then spawn them
    if (!game.players.some((player) => player.id === userId)) {
      game.players.push({
        id: userId,
        x: 0,
        y: 0
      });
    }
  },

  // Called when a user disconnects from your server, this is a good place to cleanup data for that player
  unsubscribeUser(roomId: RoomId, userId: string): void {
    // Make sure the room exists
    if (!rooms.has(roomId)) {
      return;
    }
    
    const game = rooms.get(roomId)!;
    const idx = game.players.findIndex((player) => player.id === userId);
    
    // Remove the player from the room's state
    if (idx >= 0) {
      game.players.splice(idx, 1);
    }
  },

  // Called when a message is sent to the server for handling, much of your core logic will live here
  async onMessage(roomId: RoomId, userId: string, data: ArrayBuffer): Promise<void> {
    // Make sure the room exists
    if (!rooms.has(roomId)) {
      return;
    }

    // Get the player, or return out of the function if they don't exist
    const game = rooms.get(roomId)!;
    const player = game.players.find((player) => player.id === userId);
    if (player === undefined) {
      return;
    }

    // Parse out the data string being sent from the client
    const message = JSON.parse(Buffer.from(data).toString("utf8"));

    if (message.type === 'test-message') {
      if (message.value === 'Hello Hathora server!') {
        // Define a response message...
        const msg = {
          type: 'test-response',
          value: 'Hello Hathora clients!'
        };

        // Then broadcast it to all connected clients in this room
        server.broadcastMessage(roomId, Buffer.from(JSON.stringify(msg), "utf8"));
      }
    }
    // else if (message.type === 'some-other-action') {
    //   // (handle other message types)
    // }
  }
};

Boot the Server

// ...

// Boot server
const port = 4000;
const server = await startServer(store, port);
console.log(`Server listening on port ${port}`);