Skip to content

WebSocket

Eden Treaty 使用 subscribe 方法支持 WebSocket。

¥Eden Treaty supports WebSocket using subscribe method.

typescript
import { 
Elysia
,
t
} from "elysia";
import {
treaty
} from "@elysiajs/eden";
const
app
= new
Elysia
()
.
ws
("/chat", {
body
:
t
.
String
(),
response
:
t
.
String
(),
message
(
ws
,
message
) {
ws
.
send
(
message
);
}, }) .
listen
(3000);
const
api
=
treaty
<typeof
app
>("localhost:3000");
const
chat
=
api
.
chat
.
subscribe
();
chat
.
subscribe
((
message
) => {
console
.
log
("got",
message
);
});
chat
.
on
("open", () => {
chat
.
send
("hello from client");
});

.subscribe 接受与 gethead 相同的参数。

¥.subscribe accepts the same parameter as get and head.

响应

¥Response

Eden.subscribe 返回 EdenWS,它以相同的语法扩展了 WebSocket 的结果。

¥Eden.subscribe returns EdenWS which extends the WebSocket results in identical syntax.

如果需要更多控制,可以访问 EdenWebSocket.raw 来与原生 WebSocket API 交互。

¥If more control is need, EdenWebSocket.raw can be accessed to interact with the native WebSocket API.