mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-16 14:20:24 +08:00
46 lines
869 B
TypeScript
46 lines
869 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;
|
||
|
|
|