socket 调试

This commit is contained in:
xyf-mac
2021-11-09 17:40:46 +08:00
parent e3215376df
commit 24f778de84
8 changed files with 12123 additions and 0 deletions

56
electron-app/index.js Normal file
View File

@@ -0,0 +1,56 @@
const WS = require("ws");
let Server = null;
new Vue({
el: "#app",
data: {
status: "---",
recvMsg: "",
recvMsgError: false,
sendCode: "console.log('hello')",
webSocketInstance: null,
},
created() {
Server = new WS.Server({port: 1109});
Server.on("connection", (webSocket) => {
this.status = "link";
this.webSocketInstance = webSocket;
webSocket.on("message", (msg) => {
const {error, data} = JSON.parse(msg);
this.recvMsgError = !!error;
if (data) {
this.recvMsg = data;
} else {
this.recvMsg = null;
}
});
webSocket.on("close", () => {
console.log("close");
this.status = "close";
this.webSocketInstance = null;
});
webSocket.on("open", () => {
console.log("open");
this.status = "open";
});
webSocket.on("error", () => {
console.log("error");
this.status = "error";
this.webSocketInstance = null;
});
});
},
mounted() {
},
methods: {
onRunCmd() {
if (this.webSocketInstance) {
let str = {
code: this.sendCode,
};
this.webSocketInstance.send(JSON.stringify(str));
}
},
}
});