Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Oct 1, 2023
1 parent ebf5a90 commit ae90498
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 291 deletions.
4 changes: 3 additions & 1 deletion examples/bun/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// bun run client.ts

const ws = new WebSocket("ws://localhost:3000");

ws.onopen = () => {
console.log("Connected!");
ws.send("6aa24e6aa34db4a4faf55c69c6f612aeb06053c2")
ws.send("90a60a0dccc4ba24b84f93fb777af45cd7a70350")
};

ws.onmessage = (event) => {
Expand Down
18 changes: 18 additions & 0 deletions examples/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>Websocket Example</title>
</head>
<body>
<script type="module" src="./index.js"></script>
<h1>Substreams WebSocket</h1>
<div>Hash Module</div>
<form id="form">
<input type="text" id="message" size="45" placeholder="Hash Module" value="90a60a0dccc4ba24b84f93fb777af45cd7a70350">
<input type="button" id="send" value="Send">
</form>
<div id="messages"></div>
</body>
</html>
19 changes: 19 additions & 0 deletions examples/html/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const messages = document.querySelector("#messages");
const send = document.querySelector("#send");
const message = document.querySelector("#message");

const ws = new WebSocket("ws://localhost:3000");

ws.onopen = () => {
console.log("Connected!");
ws.send("90a60a0dccc4ba24b84f93fb777af45cd7a70350");
};

ws.onmessage = (event) => {
console.log(`Message from server: ${event.data}`);
messages.innerHTML += `<li>${event.data}</li>`;
};

send.addEventListener("click", () => {
ws.send(message.value)
})
35 changes: 0 additions & 35 deletions examples/next/.gitignore

This file was deleted.

36 changes: 0 additions & 36 deletions examples/next/README.md

This file was deleted.

Binary file removed examples/next/app/favicon.ico
Binary file not shown.
27 changes: 0 additions & 27 deletions examples/next/app/globals.css

This file was deleted.

22 changes: 0 additions & 22 deletions examples/next/app/layout.tsx

This file was deleted.

10 changes: 0 additions & 10 deletions examples/next/app/page.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions examples/next/components.json

This file was deleted.

45 changes: 0 additions & 45 deletions examples/next/components/component.tsx

This file was deleted.

6 changes: 0 additions & 6 deletions examples/next/lib/utils.ts

This file was deleted.

4 changes: 0 additions & 4 deletions examples/next/next.config.js

This file was deleted.

32 changes: 0 additions & 32 deletions examples/next/package.json

This file was deleted.

6 changes: 0 additions & 6 deletions examples/next/postcss.config.js

This file was deleted.

20 changes: 0 additions & 20 deletions examples/next/tailwind.config.ts

This file was deleted.

27 changes: 0 additions & 27 deletions examples/next/tsconfig.json

This file was deleted.

8 changes: 4 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Bun.serve<{key: string}>({
prometheus.active_connections.inc(1);
prometheus.connected.inc(1);
console.log('open', {key: ws.data.key, remoteAddress: ws.remoteAddress});
ws.send("🎉 Connected!");
ws.send(JSON.stringify({message: "🎉 Connected!"}));
},
close(ws, code, reason) {
prometheus.active_connections.dec(1);
Expand All @@ -121,17 +121,17 @@ Bun.serve<{key: string}>({
// Handle Subscribe
const moduleHash = String(message);
if ( ws.isSubscribed(moduleHash) ) {
ws.send(`⚠️ Already subscribed to ${moduleHash}.`);
ws.send(JSON.stringify({message: `⚠️ Already subscribed to ${moduleHash}.`}));
console.log('already subscribed', {key: ws.data.key, remoteAddress: ws.remoteAddress, moduleHash});
return;
}
if ( !sqlite.exists(db, "moduleHash", moduleHash) ) {
ws.send(`❌ ModuleHash ${moduleHash} not found.`);
ws.send(JSON.stringify({message: `❌ ModuleHash ${moduleHash} not found.`}));
console.log('moduleHash not found', {key: ws.data.key, remoteAddress: ws.remoteAddress, moduleHash});
return;
}
ws.subscribe(moduleHash);
ws.send(`🚀 Subscribed to ${moduleHash}!`);
ws.send(JSON.stringify({message: `🚀 Subscribed to ${moduleHash}!`}));
console.log('subscribed', {key: ws.data.key, remoteAddress: ws.remoteAddress, moduleHash});
},
},
Expand Down

0 comments on commit ae90498

Please sign in to comment.