-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
35 lines (25 loc) · 942 Bytes
/
Copy pathindex.ts
File metadata and controls
35 lines (25 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { ConsoleLogger } from './provider/console.ts';
import { parseConfigFromArgs } from './provider/parseArgs.ts';
import { WSClient, WSServer } from './provider/ws.ts';
import { type Client, type Config, type Server } from './types.ts';
const config: Config = parseConfigFromArgs();
const logger = new ConsoleLogger(config.debug);
const server: Server = new WSServer(config.listen, logger);
server.on('connection', (connection) => {
const client: Client = new WSClient(config.url, logger);
client.on('message', (data) => {
logger.log('Message from server: ', data);
connection.send(data);
});
client.on('close', () => {
connection.close();
});
connection.on('message', async (data) => {
logger.log('Message from client: ', data);
await client.ready();
await client.send(data);
});
connection.on('close', () => {
client.close();
});
});