Skip to content

Commit

Permalink
docs: update usage to 0.2x
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 25, 2024
1 parent 8ed7908 commit db99a91
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 50 deletions.
46 changes: 24 additions & 22 deletions docs/1.guide/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@ Writing a realtime WebSocket server that can work in different javascript and We
A simple WebSocket implementation looks like this:

```ts
import { defineWebSocketHooks } from "crossws";

websocket = defineWebSocketHooks({
open(peer) {
console.log("[ws] open", peer);
},

message(peer, message) {
console.log("[ws] message", peer, message);
if (message.text().includes("ping")) {
peer.send("pong");
}
},

close(peer, event) {
console.log("[ws] close", peer, event);
},

error(peer, error) {
console.log("[ws] error", peer, error);
},
});
import { defineHooks } from "crossws";

const websocket = {
hooks: defineHooks({
open(peer) {
console.log("[ws] open", peer);
},

message(peer, message) {
console.log("[ws] message", peer, message);
if (message.text().includes("ping")) {
peer.send("pong");
}
},

close(peer, event) {
console.log("[ws] close", peer, event);
},

error(peer, error) {
console.log("[ws] error", peer, error);
},
}),
};
```

::read-more{to="/guide/api" title="API Usage"}
Expand Down
50 changes: 26 additions & 24 deletions docs/1.guide/2.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,32 @@ CrossWS provides a cross-platform API to define WebSocket servers. An implementa
> CrossWS API is still under development and can change.
```ts
import { defineWebSocketHooks } from "crossws";

const websocketHooks = defineWebSocketHooks({
open(peer) {
console.log("[ws] open", peer);
},

message(peer, message) {
console.log("[ws] message", peer, message);
if (message.text().includes("ping")) {
peer.send("pong");
}
},

close(peer, event) {
console.log("[ws] close", peer, event);
},

error(peer, error) {
console.log("[ws] error", peer, error);
},

// ... platform hooks such as bun:drain ...
});
import { defineHooks } from "crossws";

const websocket = {
hooks: defineHooks({
open(peer) {
console.log("[ws] open", peer);
},

message(peer, message) {
console.log("[ws] message", peer, message);
if (message.text().includes("ping")) {
peer.send("pong");
}
},

close(peer, event) {
console.log("[ws] close", peer, event);
},

error(peer, error) {
console.log("[ws] error", peer, error);
},

// ... platform hooks such as bun:drain ...
}),
};
```

> [!TIP]
Expand Down
6 changes: 5 additions & 1 deletion docs/2.adapters/bun.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ To integrate CrossWS with your Bun server, you need to handle upgrade with `hand
```ts
import wsAdapter from "crossws/adapters/bun";

const { websocket, handleUpgrade } = wsAdapter({ message: console.log });
const { websocket, handleUpgrade } = wsAdapter({
hooks: {
message: console.log,
},
});

Bun.serve({
port: 3000,
Expand Down
6 changes: 5 additions & 1 deletion docs/2.adapters/cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ To integrate CrossWS with your Cloudflare Workers, you need to check for the `up
```ts
import wsAdapter from "crossws/adapters/cloudflare";

const { handleUpgrade } = wsAdapter({ message: console.log });
const { handleUpgrade } = wsAdapter({
hooks: {
message: console.log,
},
});

export default {
async fetch(request, env, context) {
Expand Down
6 changes: 5 additions & 1 deletion docs/2.adapters/deno.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ To integrate CrossWS with your Deno server, you need to check for the `upgrade`
```ts
import wsAdapter from "crossws/adapters/deno";

const { handleUpgrade } = wsAdapter({ message: console.log });
const { handleUpgrade } = wsAdapter({
hooks: {
message: console.log,
},
});

Deno.serve({ port: 3000 }, (request) => {
if (request.headers.get("upgrade") === "websocket") {
Expand Down
6 changes: 5 additions & 1 deletion docs/2.adapters/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ Instead of [using `ws`](/adapters/node-ws) you can use [uWebSockets.js](https://
import { App } from "uWebSockets.js";
import wsAdapter from "crossws/adapters/uws";

const { websocket } = wsAdapter({ message: console.log });
const { websocket } = wsAdapter({
hooks: {
message: console.log,
},
});

const server = App().ws("/*", websocket);

Expand Down

0 comments on commit db99a91

Please sign in to comment.