2024-10-10 01:46:14 +00:00
|
|
|
|
class ConnectPipe {
|
|
|
|
|
#websocket;
|
|
|
|
|
//在这里打断点可能会导致debug错误,然后浏览器打不开页面, 这是为啥?
|
|
|
|
|
constructor() {
|
|
|
|
|
//Id,Msgtype,callback
|
|
|
|
|
// this.#websocket = new WebSocket(`ws://${window.location.host}`)
|
|
|
|
|
}
|
2024-10-10 05:15:14 +00:00
|
|
|
|
OpenPipe(config, MsgCb) {
|
|
|
|
|
// var webSocUrl = `ws://${window.location.host}:${window.location.port}/websoc?Name=${config.Name}`
|
2024-10-11 01:04:58 +00:00
|
|
|
|
var webSocUrl = "ws://127.0.0.1:6818/websoc?Name=Test";
|
2024-10-10 01:46:14 +00:00
|
|
|
|
this.#websocket = new WebSocket(webSocUrl);
|
|
|
|
|
this.#websocket.onopen = (event) => {
|
2024-10-11 01:04:58 +00:00
|
|
|
|
var starter = {
|
|
|
|
|
Body: JSON.stringify(config),
|
|
|
|
|
Type: 1,
|
|
|
|
|
Step: 1,
|
|
|
|
|
};
|
|
|
|
|
// console.warn("websocket connected!");
|
|
|
|
|
this.#websocket.send(JSON.stringify(starter));
|
2024-10-10 01:46:14 +00:00
|
|
|
|
};
|
|
|
|
|
this.#websocket.onmessage = (event) => {
|
2024-10-11 01:04:58 +00:00
|
|
|
|
// console.log(event.data);
|
|
|
|
|
MsgCb(JSON.parse(event.data))
|
2024-10-10 01:46:14 +00:00
|
|
|
|
};
|
2024-10-10 05:15:14 +00:00
|
|
|
|
this.#websocket.onclose = (event) => {
|
2024-10-11 01:04:58 +00:00
|
|
|
|
|
|
|
|
|
console.warn(event)
|
|
|
|
|
MsgCb({
|
|
|
|
|
Type: 0,
|
|
|
|
|
Step: 8,
|
|
|
|
|
Body:event.reason
|
|
|
|
|
})
|
|
|
|
|
|
2024-10-10 05:15:14 +00:00
|
|
|
|
};
|
2024-10-10 01:46:14 +00:00
|
|
|
|
this.#websocket.onerror = (e) => {
|
2024-10-11 01:04:58 +00:00
|
|
|
|
console.error(e);
|
|
|
|
|
MsgCb({
|
|
|
|
|
Type: 0,
|
|
|
|
|
Body: "异常错误,查看 Console",
|
|
|
|
|
Step: 7,
|
|
|
|
|
});
|
2024-10-10 01:46:14 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2024-10-11 01:04:58 +00:00
|
|
|
|
ClosePipe() {
|
|
|
|
|
this.#websocket.close();
|
|
|
|
|
}
|
2024-10-10 01:46:14 +00:00
|
|
|
|
|
2024-10-11 01:04:58 +00:00
|
|
|
|
}
|
2024-10-10 01:46:14 +00:00
|
|
|
|
|
2024-10-10 05:15:14 +00:00
|
|
|
|
export default ConnectPipe;
|