mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 07:50:25 +08:00
44 lines
881 B
TypeScript
44 lines
881 B
TypeScript
class SocketBuilder {
|
|
websocket: WebSocket;
|
|
|
|
constructor(url: string) {
|
|
if (typeof WebSocket === 'undefined') {
|
|
throw new Error('不支持websocket');
|
|
}
|
|
if (!url) {
|
|
throw new Error('websocket url不能为空');
|
|
}
|
|
this.websocket = new WebSocket(url);
|
|
}
|
|
|
|
static builder(url: string) {
|
|
return new SocketBuilder(url);
|
|
}
|
|
|
|
open(onopen: any) {
|
|
this.websocket.onopen = onopen;
|
|
return this;
|
|
}
|
|
|
|
error(onerror: any) {
|
|
this.websocket.onerror = onerror;
|
|
return this;
|
|
}
|
|
|
|
message(onmessage: any) {
|
|
this.websocket.onmessage = onmessage;
|
|
return this;
|
|
}
|
|
|
|
close(onclose: any) {
|
|
this.websocket.onclose = onclose;
|
|
return this;
|
|
}
|
|
|
|
build() {
|
|
return this.websocket;
|
|
}
|
|
}
|
|
|
|
export default SocketBuilder;
|