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

9
electron-app/index.css Normal file
View File

@ -0,0 +1,9 @@
.sendCode {
width: 100%;
height: 300px;
margin-top: 10px;
margin-bottom: 10px;
}
.recvMsgError{
color: red;
}

34
electron-app/index.html Normal file
View File

@ -0,0 +1,34 @@
<html>
<head>
<script src="lib/vue.js"></script>
<link rel="stylesheet" href="index.css">
</head>
<body>
<div id="app">
<div>
链接状态:{{status}}
</div>
<div v-show="webSocketInstance">
<div style="display: flex;flex-direction: column;">
<span>收到的消息:</span>
<label>
<textarea :value="recvMsg" style="width: 100%;height: 300px;"
:class="{'recvMsgError':recvMsgError}">
</textarea>
</label>
</div>
<div style="display: flex;flex-direction: column">
<label>
<textarea v-model="sendCode" class="sendCode"></textarea>
</label>
<div style="display: flex;flex-direction: row-reverse;">
<button @click="onRunCmd">发送代码</button>
</div>
</div>
</div>
</div>
<script src="./index.js"></script>
</body>
</html>

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));
}
},
}
});

11944
electron-app/lib/vue.js Normal file

File diff suppressed because it is too large Load Diff

21
electron-app/main.js Normal file
View File

@ -0,0 +1,21 @@
const Path = require("path");
const {BrowserWindow, app} = require("electron");
app.on("ready", () => {
const win = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInSubFrames: true,
nodeIntegrationInWorker: true,
webSecurity: true,
contextIsolation: false,
}
});
win.webContents.openDevTools({mode: "right"});
win.loadFile(Path.join(__dirname, "index.html"));
win.show();
});
app.on("window-all-closed", () => {
app.quit();
});

16
electron-app/package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "electron",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "electron ./"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.24.0",
"electron": "14.0.0",
"ws": "^8.2.3"
}
}

32
electron-app/test/cmd.js Normal file
View File

@ -0,0 +1,32 @@
// 游戏的inspect脚本
const host = "192.168.1.5";//"localhost";
const port = 1109;
let url = `ws://${host}:${port}`;
const ws = new WebSocket(url);
ws.onopen = () => {
console.log("成功链接调试服务器", url);
};
ws.onmessage = (event) => {
console.log("收到消息", event);
const {code} = JSON.parse(event.data);
if (code) {
let ret = null;
let error = false;
try {
ret = eval(`${code}`);
} catch (e) {
error = true;
ret = e.toString();
}
ws.send(JSON.stringify({
error,
data: ret,
}));
}
};
ws.onerror = () => {
console.log("error");
};
ws.onclose = () => {
console.log("close");
};

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="cmd.js"></script>
</head>
<body>
</body>
</html>