Repository navigation
Emulator WebSocket endpoint: echo and paced messages - #59
Merged
Merged
Conversation
…ocal GET /_emulate/openai/websocket accepts a WebSocket upgrade and echoes each message, text or binary, as it was received. With ?chunks=N&interval_ms=M, the paced-answer controls, it also sends N messages of its own, M ms apart, then stays open, for a busy connection or an idle one. It closes when the client closes, answering with the client's code. An emulator control, not OpenAI's Realtime or WebSocket-mode protocol, which stay unsupported. Core gains upgradeWebSocket(request, session) for routes, on both hosts: a WebSocketPair on the Workers runtime (binary messages may arrive as a Blob, read in order), and on Node a server side of RFC 6455 in serve's upgrade handler (masked client frames, fragments, ping/pong, the closing handshake, a 1 MiB message limit). The response finaliser passes a 101 through, and the ledger records an upgrade without reading a body.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Lets a program hold a WebSocket open to a hosted or local emulator for as long as it likes, exchanging messages.
GET /_emulate/openai/websocket(wss://hosted,ws://locally) accepts the upgrade and echoes each message, text or binary, as it was received.?chunks=N&interval_ms=Muses the paced-answer controls, with the same limits (0 to 10,000; 0 to 60,000 ms). The server also sends N messages of its own,{"type":"emulate.message","n":1,"of":N,"at":…}, M ms apart, then stays open. So a client can hold a busy connection or an idle one.Core:
upgradeWebSocket(request, session)gives routes one interface on both hosts.WebSocketPairaccepted in the instance's Durable Object. Binary messages arrive there as aBlob; they are read in order before the session sees them.serve'supgradehandler: the handshake, masked client frames, fragmented messages, ping/pong, the closing handshake, and a 1 MiB message limit. No new dependency.Tests:
WebSocketclient: echo of text, binary and a 70,000-character message; paced messages at the asked spacing; still open and echoing afterwards; clean close with 1000; 426 and 400.WebSocketPair:Blobreading in order, binary sent as its own buffer, and the close echo.On a staging copy of the hosted Worker: echo of text and binary; 5 paced messages in 1.0 s; clean close with 1000. Three deploys of the staging Worker while a connection was open, one of them followed by ordinary requests to the same instance, left the connection open and echoing every message.