Skip to content

Commit

Permalink
server: Count clients
Browse files Browse the repository at this point in the history
  • Loading branch information
evanpurkhiser committed Jan 10, 2021
1 parent c0e3e5a commit 1c1891e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
8 changes: 7 additions & 1 deletion package.json
Expand Up @@ -28,7 +28,11 @@
"output": "release"
},
"publish": null,
"files": ["dist/", "node_modules/better-sqlite3/**/*", "package.json"],
"files": [
"dist/",
"node_modules/better-sqlite3/**/*",
"package.json"
],
"linux": {
"target": "tar.gz"
},
Expand Down Expand Up @@ -69,6 +73,7 @@
"@babel/preset-typescript": "^7.12.7",
"@emotion/react": "^11.1.4",
"@emotion/styled": "^11.0.0",
"@koa/router": "^10.0.0",
"@octokit/request": "^5.4.5",
"@octokit/types": "^6.1.2",
"@popperjs/core": "^2.6.0",
Expand All @@ -83,6 +88,7 @@
"@types/http-proxy": "^1.17.4",
"@types/jest": "^26.0.13",
"@types/koa": "^2.11.6",
"@types/koa__router": "^8.0.3",
"@types/lodash": "^4.14.161",
"@types/module-alias": "^2.0.0",
"@types/node": "14.14.16",
Expand Down
35 changes: 27 additions & 8 deletions src/server/index.ts
@@ -1,22 +1,41 @@
import Router from '@koa/router';
import Koa from 'koa';
import {Server} from 'socket.io';
import {Server, Socket} from 'socket.io';

import {createServer} from 'http';

type Connection = {
socket: Socket;
};

const ingestMatcher = /^\/ingest\/([^/]+)$/;

const connections: Record<string, Connection> = {};

const app = new Koa();
const server = createServer(app.callback());

const wss = new Server(server);

wss.of('/ingest').on('connection', socket => {
socket.on('store-init', console.log);
socket.on('store-update', console.log);
const router = new Router();

router.get('/stats', ctx => {
ctx.body = `Current number of clients: ${Object.keys(connections).length}`;
});

console.log('here we gooo');
app.use(router.routes()).use(router.allowedMethods());

// Connect
wss.of(ingestMatcher).on('connection', socket => {
const key = socket.nsp.name.match(ingestMatcher)[1];

connections[key] = {
socket,
};

app.use(ctx => {
ctx.body = 'Hello World';
socket.on('disconnect', () => delete connections[key]);

socket.on('store-init', console.log);
socket.on('store-update', console.log);
});

server.listen(8888);
25 changes: 24 additions & 1 deletion yarn.lock
Expand Up @@ -1365,6 +1365,17 @@
"@types/yargs" "^15.0.0"
chalk "^4.0.0"

"@koa/router@^10.0.0":
version "10.0.0"
resolved "https://registry.yarnpkg.com/@koa/router/-/router-10.0.0.tgz#699115561afbd2740e2848ba299fc76b9e058ad6"
integrity sha512-z9ytrKWn/j/qUApMSJzZbUwkbLcN2ZXGq6UsqWkZb50Us+/Qpu0RwgZ6ytawVOhfFBZ1ai5iVWeD2Dcu0qcnJw==
dependencies:
debug "^4.1.1"
http-errors "^1.7.3"
koa-compose "^4.1.0"
methods "^1.1.2"
path-to-regexp "^6.1.0"

"@nodelib/fs.scandir@2.1.4":
version "2.1.4"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.4.tgz#d4b3549a5db5de2683e0c1071ab4f140904bbf69"
Expand Down Expand Up @@ -1971,6 +1982,13 @@
"@types/koa-compose" "*"
"@types/node" "*"

"@types/koa__router@^8.0.3":
version "8.0.3"
resolved "https://registry.yarnpkg.com/@types/koa__router/-/koa__router-8.0.3.tgz#f6a9ea3428d1025401f5fc21c47c18395ac44cdc"
integrity sha512-eS8K49z1x6OaW1ha61kRksVo42L5DWdQUA3kVpH1Kz6TuKBlG0ri42ELA4zSh+xg+6fAqjfuWA7bfNvwVMNXQA==
dependencies:
"@types/koa" "*"

"@types/lodash@^4.14.161", "@types/lodash@^4.14.162":
version "4.14.167"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.167.tgz#ce7d78553e3c886d4ea643c37ec7edc20f16765e"
Expand Down Expand Up @@ -8239,7 +8257,7 @@ merge2@^1.2.3, merge2@^1.3.0:
resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae"
integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==

methods@~1.1.0:
methods@^1.1.2, methods@~1.1.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
integrity sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=
Expand Down Expand Up @@ -9149,6 +9167,11 @@ path-to-regexp@^1.2.0, path-to-regexp@^1.7.0:
dependencies:
isarray "0.0.1"

path-to-regexp@^6.1.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-6.2.0.tgz#f7b3803336104c346889adece614669230645f38"
integrity sha512-f66KywYG6+43afgE/8j/GoiNyygk/bnoCbps++3ErRKsIYkGGupyv07R2Ok5m9i67Iqc+T2g1eAUGUPzWhYTyg==

path-type@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441"
Expand Down

1 comment on commit 1c1891e

@vercel
Copy link

@vercel vercel bot commented on 1c1891e Jan 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.