-
Notifications
You must be signed in to change notification settings - Fork 17
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
189 additions
and
5 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 |
---|---|---|
|
@@ -8,5 +8,5 @@ dist | |
*.env* | ||
.wrangler | ||
|
||
adapters | ||
/adapters | ||
websocket.d.ts |
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
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,23 @@ | ||
// You can run this demo using `npm run play:node-uws` in repo | ||
|
||
import { readFileSync } from "node:fs"; | ||
|
||
import { App } from "uWebSockets.js"; | ||
|
||
import nodeAdapter from "../src/adapters/node-uws.ts"; | ||
import { createDemo, getIndexHTMLURL } from "./_common"; | ||
|
||
const adapter = createDemo(nodeAdapter); | ||
|
||
const app = App().ws("/*", adapter.websocket); | ||
|
||
app.get("/*", (res, req) => { | ||
res.writeStatus("200 OK"); | ||
res.writeHeader("Content-Type", "text/html"); | ||
const indexHTML = readFileSync(getIndexHTMLURL(), "utf8"); | ||
res.end(indexHTML); | ||
}); | ||
|
||
app.listen(3001, () => { | ||
console.log("Listening to port 3001"); | ||
}); |
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,106 @@ | ||
// https://github.com/websockets/ws | ||
// https://github.com/websockets/ws/blob/master/doc/ws.md | ||
|
||
import { WebSocketBehavior, WebSocket } from "uWebSockets.js"; | ||
import { WebSocketPeerBase } from "../peer"; | ||
import { WebSocketMessage } from "../message"; | ||
import { defineWebSocketAdapter } from "../adapter"; | ||
|
||
type UserData = { _peer?: any }; | ||
type WebSocketHandler = WebSocketBehavior<UserData>; | ||
|
||
export interface AdapterOptions | ||
extends Exclude< | ||
WebSocketBehavior<any>, | ||
| "close" | ||
| "drain" | ||
| "message" | ||
| "open" | ||
| "ping" | ||
| "pong" | ||
| "subscription" | ||
| "upgrade" | ||
> {} | ||
|
||
export interface Adapter { | ||
websocket: WebSocketHandler; | ||
} | ||
|
||
export default defineWebSocketAdapter<Adapter, AdapterOptions>( | ||
(hooks, opts = {}) => { | ||
const getPeer = (ws: WebSocket<UserData>) => { | ||
const userData = ws.getUserData(); | ||
if (userData._peer) { | ||
return userData._peer as WebSocketPeer; | ||
} | ||
const peer = new WebSocketPeer({ uws: { ws } }); | ||
userData._peer = peer; | ||
return peer; | ||
}; | ||
|
||
const websocket: WebSocketHandler = { | ||
...opts, | ||
close(ws, code, message) { | ||
const peer = getPeer(ws); | ||
hooks["uws:close"]?.(peer, ws, code, message); | ||
hooks.close?.(peer, { code, reason: message?.toString() }); | ||
}, | ||
drain(ws) { | ||
const peer = getPeer(ws); | ||
hooks["uws:drain"]?.(peer, ws); | ||
}, | ||
message(ws, message, isBinary) { | ||
const peer = getPeer(ws); | ||
hooks["uws:message"]?.(peer, ws, message, isBinary); | ||
const msg = new WebSocketMessage(message, isBinary); | ||
hooks.message?.(peer, msg); | ||
}, | ||
open(ws) { | ||
const peer = getPeer(ws); | ||
hooks["uws:open"]?.(peer, ws); | ||
hooks.open?.(peer); | ||
}, | ||
ping(ws, message) { | ||
const peer = getPeer(ws); | ||
hooks["uws:ping"]?.(peer, ws, message); | ||
}, | ||
pong(ws, message) { | ||
const peer = getPeer(ws); | ||
hooks["uws:pong"]?.(peer, ws, message); | ||
}, | ||
subscription(ws, topic, newCount, oldCount) { | ||
const peer = getPeer(ws); | ||
hooks["uws:subscription"]?.(peer, ws, topic, newCount, oldCount); | ||
}, | ||
// error ? TODO | ||
// upgrade(res, req, context) {} | ||
}; | ||
|
||
return { | ||
websocket, | ||
}; | ||
}, | ||
); | ||
|
||
class WebSocketPeer extends WebSocketPeerBase<{ | ||
uws: { | ||
ws: WebSocket<UserData>; | ||
}; | ||
}> { | ||
get id() { | ||
try { | ||
const addr = this.ctx.uws.ws?.getRemoteAddressAsText(); | ||
return new TextDecoder().decode(addr); | ||
} catch { | ||
// Error: Invalid access of closed uWS.WebSocket/SSLWebSocket. | ||
} | ||
} | ||
|
||
// TODO | ||
// get readyState() {} | ||
|
||
send(message: string, compress?: boolean) { | ||
this.ctx.uws.ws.send(message, false, compress); | ||
return 0; | ||
} | ||
} |
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