-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
181 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// This serverless function connects to a remote websocket echo server, sends | ||
// messages to it and logs any responses to the console. | ||
|
||
// Remove this line and the bug disappears | ||
export const runtime = "edge"; | ||
|
||
export async function GET(req) { | ||
const ws = new WebSocket("ws://localhost:3030"); | ||
|
||
ws.addEventListener("error", console.error); | ||
|
||
let timer; | ||
|
||
ws.addEventListener("open", function open() { | ||
let counter = 0; | ||
|
||
timer = setInterval(() => { | ||
const message = counter.toString(); | ||
|
||
ws.send(message); | ||
|
||
console.log("sent: ", message); | ||
|
||
counter++; | ||
}, 1000); | ||
}); | ||
|
||
ws.addEventListener("message", function message(event) { | ||
console.log("received: %s", event.data); | ||
}); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 60000)); | ||
|
||
clearInterval(timer); | ||
|
||
return new Response("Test complete after running for 60 seconds"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Fulcrum is a Bitcoin indexer written in C++ (https://github.com/cculianu/Fulcrum) | ||
// It sends a ping every 20 seconds, so you will need to wait 20 seconds to observe the bug | ||
|
||
// Remove this line and the bug disappears | ||
export const runtime = "edge"; | ||
|
||
export async function GET(req) { | ||
const ws = new WebSocket("ws://bch.imaginary.cash:50003"); | ||
|
||
ws.addEventListener("error", console.error); | ||
|
||
let timer; | ||
|
||
ws.addEventListener("open", function open() { | ||
let counter = 0; | ||
|
||
setInterval(() => { | ||
const message = counter < 1 | ||
? `{"id":0,"method":"server.version","params":["vercel/edge-runtime#983","1.5"]}` | ||
: `{"id":${counter.toString()},"method":"server.ping","params":[]}`; | ||
|
||
ws.send(message); | ||
|
||
console.log("sent: ", message); | ||
|
||
counter++; | ||
}, 1000); | ||
|
||
|
||
}); | ||
|
||
ws.addEventListener("message", function message(event) { | ||
console.log("received: %s", event.data); | ||
}); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 60000)); | ||
|
||
clearInterval(timer); | ||
|
||
return new Response("Test complete after running for 60 seconds"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// This serverless function connects to an upstream websocket server and | ||
// streams messages to the client. | ||
|
||
// Remove this line and the bug disappears | ||
export const runtime = "edge"; | ||
|
||
export async function GET(req) { | ||
const ws = new WebSocket("ws://localhost:3030"); | ||
|
||
ws.addEventListener("error", console.error); | ||
|
||
let timer; | ||
|
||
ws.addEventListener("open", function open() { | ||
let counter = 0; | ||
|
||
timer = setInterval(() => { | ||
const message = counter.toString(); | ||
|
||
ws.send(message); | ||
|
||
console.log("sent: ", message); | ||
|
||
counter++; | ||
}, 1000); | ||
}); | ||
|
||
|
||
const encoder = new TextEncoder(); | ||
const customReadable = new ReadableStream({ | ||
start(controller) { | ||
ws.addEventListener("message", function message(event) { | ||
console.log("received: %s", event.data); | ||
|
||
controller.enqueue(`event: data\ndata: ${event.data}\n\n`); | ||
}); | ||
}, | ||
cancel() { | ||
ws.close(); | ||
|
||
clearInterval(timer); | ||
} | ||
}); | ||
|
||
return new Response(customReadable, { | ||
headers: { | ||
"Content-Type": "text/event-stream" | ||
} | ||
}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { WebSocketServer } from 'ws'; | ||
|
||
const wss = new WebSocketServer({ | ||
port: 3030, | ||
|
||
}); | ||
|
||
wss.on('connection', function connection(ws) { | ||
ws.on('message', function message(data) { | ||
console.log('received: %s', data); | ||
ws.send(data.toString()); | ||
console.log('sent: %s', data); | ||
}); | ||
|
||
setInterval(() => ws.ping(), 3000); | ||
|
||
ws.on('error', console.error); | ||
|
||
console.log('connection opened'); | ||
}); | ||
|
||
wss.on('close', function close() { | ||
console.log('connection closed'); | ||
}); |