Skip to content

Commit 2448490

Browse files
committed
🚀 initial commit
0 parents  commit 2448490

File tree

10 files changed

+2555
-0
lines changed

10 files changed

+2555
-0
lines changed

‎.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Tells the .editorconfg plugin to stop searching once it finds this file
2+
root = true
3+
4+
[*]
5+
indent_size = 2
6+
indent_style = space
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
quote_type = single
12+
13+
[*.py]
14+
indent_size = 4

‎.gitignore

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Ignore generated JS and TypeScript files
2+
*.d.ts
3+
build/
4+
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
yarn-debug.log*
10+
yarn-error.log*
11+
lerna-debug.log*
12+
13+
# Diagnostic reports (https://nodejs.org/api/report.html)
14+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
15+
16+
# Runtime data
17+
pids
18+
*.pid
19+
*.seed
20+
*.pid.lock
21+
22+
# Directory for instrumented libs generated by jscoverage/JSCover
23+
lib-cov
24+
25+
# Coverage directory used by tools like istanbul
26+
coverage
27+
*.lcov
28+
29+
# nyc test coverage
30+
.nyc_output
31+
32+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
33+
.grunt
34+
35+
# Bower dependency directory (https://bower.io/)
36+
bower_components
37+
38+
# node-waf configuration
39+
.lock-wscript
40+
41+
# Compiled binary addons (https://nodejs.org/api/addons.html)
42+
build/Release
43+
44+
# Dependency directories
45+
node_modules/
46+
jspm_packages/
47+
48+
# Snowpack dependency directory (https://snowpack.dev/)
49+
web_modules/
50+
51+
# TypeScript cache
52+
*.tsbuildinfo
53+
54+
# Optional npm cache directory
55+
.npm
56+
57+
# Optional eslint cache
58+
.eslintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variables file
76+
.env
77+
.env.test
78+
79+
# parcel-bundler cache (https://parceljs.org/)
80+
.cache
81+
.parcel-cache
82+
83+
# Next.js build output
84+
.next
85+
out
86+
87+
# Nuxt.js build / generate output
88+
.nuxt
89+
dist
90+
91+
# Gatsby files
92+
.cache/
93+
# Comment in the public line in if your project uses Gatsby and not Next.js
94+
# https://nextjs.org/blog/next-9-1#public-directory-support
95+
# public
96+
97+
# vuepress build output
98+
.vuepress/dist
99+
100+
# Serverless directories
101+
.serverless/
102+
103+
# FuseBox cache
104+
.fusebox/
105+
106+
# DynamoDB Local files
107+
.dynamodb/
108+
109+
# TernJS port file
110+
.tern-port
111+
112+
# Stores VSCode versions used for testing VSCode extensions
113+
.vscode-test
114+
115+
# yarn v2
116+
.yarn/cache
117+
.yarn/unplugged
118+
.yarn/build-state.yml
119+
.yarn/install-state.gz
120+
.pnp.*

‎.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v14.3.0

‎README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 2021-frontend-wss
2+
3+
Simple WebSocket server that can be used for client development.
4+
5+
## Requirements
6+
7+
- Node.js v14.3
8+
- npm 6.14
9+
10+
## Usage
11+
12+
### Run in Dev Mode
13+
14+
Making changes to the TypeScript source code after running `npm run dev` will
15+
cause the server to automatically restart with the new changes.
16+
17+
```
18+
git clone https://github.com/rhdemo/2021-frontend-wss
19+
cd 2021-frontend-wss
20+
21+
npm install
22+
npm run dev
23+
```
24+
25+
### Run in Production Mode
26+
27+
TODO

‎config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const { get } = require('env-var');
4+
5+
const config = {
6+
HTTP_PORT: get('HTTP_PORT').default(3000).asPortNumber(),
7+
8+
// Reject web socket payloads greater than this many bytes (5KB by default)
9+
WS_MAX_PAYLOAD: get('WS_MAX_PAYLOAD')
10+
.default(1024 * 5)
11+
.asIntPositive(),
12+
};
13+
14+
export = config;

‎index.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict ';
2+
3+
import fastify, { FastifyInstance } from 'fastify';
4+
import { Server } from 'http';
5+
import { ServerOptions } from 'ws';
6+
import { WebsocketPluginOptions } from 'fastify-websocket';
7+
import { WS_MAX_PAYLOAD, HTTP_PORT } from './config';
8+
import { getWsAddressFromServer } from './utils';
9+
10+
const app = fastify({ logger: true });
11+
12+
app.get('/', async (request, reply) => {
13+
return { hello: 'world' };
14+
});
15+
16+
// Register the WS plugin, apply a max payload limit, and optional authorisation
17+
app.register(require('fastify-websocket'), {
18+
options: {
19+
maxPayload: WS_MAX_PAYLOAD,
20+
verifyClient: (info, next) => {
21+
// Can add optional verification logic into this block
22+
const addr =
23+
info.req.headers['x-forwarded-for'] || info.req.socket.remoteAddress;
24+
app.log.info(`accepted connection from ${addr}`);
25+
next(true);
26+
},
27+
} as ServerOptions,
28+
} as WebsocketPluginOptions);
29+
30+
// This is the WS endpoint, i.e ws://localhost:3000/game
31+
app.get('/game', { websocket: true }, (conn, req) => {
32+
conn.on('error', (err: any) => {
33+
app.log.error(
34+
`error generated. client will be disconnected due to: ${err}`
35+
);
36+
});
37+
conn.on('close', () => {
38+
app.log.error(`client connection closed`);
39+
});
40+
conn.socket.on('message', (message) => {
41+
app.log.info(`received message ${message}`);
42+
// echo the incoming message back
43+
conn.socket.send(message);
44+
});
45+
});
46+
47+
const start = async () => {
48+
try {
49+
await app.listen(HTTP_PORT);
50+
app.log.info(
51+
`Connect vai WebSocket to ws://${getWsAddressFromServer(app.server)}/game`
52+
);
53+
} catch (err) {
54+
app.log.error(err);
55+
process.exit(1);
56+
}
57+
};
58+
59+
start();

0 commit comments

Comments
 (0)