Skip to content

Commit

Permalink
ws noodling
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Sep 8, 2021
1 parent 4d16a5c commit d43431f
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 192 deletions.
21 changes: 20 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -50,7 +50,7 @@
"qs": "^6.10.1",
"uuid": "^8.3.2",
"winston": "^3.3.3",
"ws": "^8.2.0",
"ws": "^8.2.1",
"yargs": "^17.1.1"
},
"devDependencies": {
Expand All @@ -60,6 +60,7 @@
"@types/primus": "^7.3.5",
"@types/puppeteer": "^5.4.4",
"@types/uuid": "^8.3.1",
"@types/ws": "^7.4.7",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"puppeteer": "^10.2.0",
Expand Down Expand Up @@ -92,6 +93,7 @@
"debug": "tsc && ts-node-dev --transpile-only --no-deps --inspect -- ./src/server ",
"start": "node ./dist/server.js",
"build": "rm -rf dist && ./node_modules/.bin/tsc --sourceMap false --declaration",
"watch": "rm -rf dist && ./node_modules/.bin/tsc --sourceMap false --declaration --watch",
"docs": "typedoc --out docs --theme default src/index.ts",
"lint": "prettier --check src __tests__",
"pretty": "prettier --write src __tests__"
Expand Down
5 changes: 1 addition & 4 deletions public/chat.html
Expand Up @@ -7,10 +7,7 @@
<link rel="icon" href="/public/favicon.ico" />
<title>actionhero.js</title>

<script
type="text/javascript"
src="/public/javascript/ActionheroWebsocketClient.js"
></script>
<script type="text/javascript" src="/public/websocket.js"></script>
</head>

<body onload="boot()">
Expand Down
Empty file removed public/javascript/.gitkeep
Empty file.
40 changes: 38 additions & 2 deletions src/clients/websocket.ts
@@ -1,6 +1,6 @@
export type WebsocketClientState = "disconnected" | "connected" | "connecting";

export class WebsocketClient {
export class ActionheroWebsocketClient {
url: string;
options: Record<string, any>;
id: string;
Expand All @@ -9,12 +9,13 @@ export class WebsocketClient {
rooms: string[];
state: WebsocketClientState;
messageId: number;
pingTimeout: NodeJS.Timeout;
connection: WebSocket; // built-in type

/**
* Build a new Websocket client to talk to an Actionhero server
*
* @param url: The URL to connect to + path. `"http://localhost:8080/ws"` would be the localhost default.
* @param url: The URL to connect to. `"http://localhost:8080"` would be the localhost default.
* @param options: Options to pass to the websocket connection.
*/
constructor(url: string, options?: { protocols: string }) {
Expand All @@ -35,5 +36,40 @@ export class WebsocketClient {
this.state = "connecting";
delete this.connection;
this.connection = new WebSocket(this.url, this.options.protocols);

this.connection.onopen = () => {
this.heartbeat();
};

this.connection.onclose = () => {
clearTimeout(this.pingTimeout);
};

this.connection.onerror = (error: any) => {
console.error(error);
};

this.connection.onmessage = (message: any) => {
let data: Record<string, any> = message;
try {
data = JSON.parse(message);
} catch {}

console.log(data);
// this.connection.on("ping", this.heartbeat);
};
}

private heartbeat() {
clearTimeout(this.pingTimeout);
this.state = "connected";

// Use `WebSocket#terminate()`, which immediately destroys the connection,
// instead of `WebSocket#close()`, which waits for the close timer.
// Delay should be equal to the interval at which your server
// sends out pings plus a conservative assumption of the latency.
this.pingTimeout = setTimeout(() => {
this.connection.close();
}, 15 * 1000 * 2);
}
}
49 changes: 2 additions & 47 deletions src/config/servers/websocket.ts
@@ -1,55 +1,10 @@
// Note that to use the websocket server, you also need the web server enabled
// Note that to use the websocket server, you also need the `web` server enabled

export const DEFAULT = {
servers: {
websocket: (config) => {
websocket: () => {
return {
enabled: true,
// you can pass a FQDN (like https://company.com) here or 'window.location.origin'
// clientUrl: "window.location.origin",
// Directory to render client-side JS.
// Path should start with "/" and will be built starting from api.config..general.paths.public
// clientJsPath: "javascript/",
// the name of the client-side JS file to render. Both `.js` and `.min.js` versions will be created
// do not include the file extension
// set to `undefined` to not render the client-side JS on boot
// clientJsName: "ActionheroWebsocketClient",
// should the server signal clients to not reconnect when the server is shutdown/reboot
destroyClientsOnShutdown: false,
// what route should the websocket server bind to?
mount: "/ws",

// websocket Server Options:
server: {
// authorization: null,
// pathname: '/primus',
// parser: 'JSON',
// transformer: 'websockets',
// plugin: {},
// timeout: 35000,
// origins: '*',
// methods: ['GET','HEAD','PUT','POST','DELETE','OPTIONS'],
// credentials: true,
// maxAge: '30 days',
// exposed: false,
},

// // websocket Client Options:
// client: {
// apiPath: "/api", // the api base endpoint on your actionhero server
// // the cookie name we should use for shared authentication between WS and web connections
// cookieKey: config.servers.web.fingerprintOptions.cookieKey,
// // reconnect: {},
// // timeout: 10000,
// // ping: 25000,
// // pong: 10000,
// // strategy: "online",
// // manual: false,
// // websockets: true,
// // network: true,
// // transport: {},
// // queueSize: Infinity,
// },
};
},
},
Expand Down

0 comments on commit d43431f

Please sign in to comment.