Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Build and test

on: [push, pull_request]

env:
CI: true

jobs:
test-job:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- run: npm ci
- run: npx tsc --noEmit
- run: npm test
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
package-lock.json
*.js
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": false,
"trailingComma": "all",
"tabWidth": 4
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Edit your runtime environment to contain the following environment variables:

- `PORT` is the port the proxy will be running on.
- `REDIS_URL` is the path to the same Redis instance you're using on Colyseus' processes.
- `REDIS_CLUSTER` set to `true` if running Redis in cluster mode.

Once installed it can be run with

Expand All @@ -41,6 +42,7 @@ Edit your environment to contain the following environment variables:

- `PORT` is the port the proxy will be running on.
- `REDIS_URL` is the path to the same Redis instance you're using on Colyseus' processes.
- `REDIS_CLUSTER` set to `true` if running Redis in cluster mode.

Start the proxy server:

Expand Down
17 changes: 11 additions & 6 deletions discovery.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import Redis from "ioredis";

const REDIS_URL = process.env.REDIS_URL || 'redis://127.0.0.1:6379';
const REDIS_CLUSTER = process.env.REDIS_CLUSTER === "true";
const REDIS_URL = process.env.REDIS_URL || "redis://127.0.0.1:6379";
const NODES_SET = "colyseus:nodes";
const ROOM_COUNT_KEY = "roomcount";
const DISCOVERY_CHANNEL = "colyseus:nodes:discovery";

const redis = new Redis(REDIS_URL);
const sub = new Redis(REDIS_URL);
const redis = REDIS_CLUSTER
? new Redis.Cluster([REDIS_URL])
: new Redis(REDIS_URL);
const sub = REDIS_CLUSTER
? new Redis.Cluster([REDIS_URL])
: new Redis(REDIS_URL);

export interface Node {
processId: string;
address?: string
address?: string;
}

export type Action = 'add' | 'remove';
export type Action = "add" | "remove";

function parseNode(data: string): Node {
const [processId, address] = data.split("/");
Expand All @@ -22,7 +27,7 @@ function parseNode(data: string): Node {

export async function getNodeList(): Promise<Node[]> {
const nodes: string[] = await redis.smembers(NODES_SET);
return nodes.map(data => parseNode(data));
return nodes.map((data) => parseNode(data));
}

export function listen(cb: (action: Action, node: Node) => void) {
Expand Down
Loading