Skip to content

Commit

Permalink
测试用网络服务器
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Apr 24, 2024
1 parent 2ccb935 commit 5f5e573
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@
"title": "复制名称",
"enablement": "viewItem == 玩家属性",
"category": "Y3开发助手"
},
{
"command": "y3-helper.networkServer",
"title": "启动网络服务器(用于测试`network`库)",
"category": "Y3开发助手"
}
],
"configuration": {
Expand Down
10 changes: 10 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { GameLauncher } from './launchGame';
import { EXCELimporter } from './editorTable/EXCEL/EXCELimporter';
import { TemplateGenerator } from './editorTable/templateGenerator';
import { englishPathToChinese } from './constants';
import { NetworkServer } from './networkServer';
import {
CSVimporter, EditorTableDataProvider, GoEditorTableSymbolProvider,
GoEditorTableDocumentSymbolProvider, FileNode,
Expand Down Expand Up @@ -52,6 +53,14 @@ class Helper {
});
}

private registerCommandOfNetworkServer() {
let server: NetworkServer | undefined;
vscode.commands.registerCommand('y3-helper.networkServer', async () => {
server?.dispose();
server = new NetworkServer('127.0.0.1', 25895);
});
}

private registerCommandOfInitProject() {
let running = false;
vscode.commands.registerCommand('y3-helper.initProject', async () => {
Expand Down Expand Up @@ -696,6 +705,7 @@ class Helper {
this.checkNewProject();
this.reloadEnvWhenConfigChange();

this.registerCommandOfNetworkServer();
this.registerCommonCommands();

this.registerCommandOfCSVeditor();
Expand Down
44 changes: 44 additions & 0 deletions src/networkServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as net from "net";
import * as vscode from "vscode";

export class NetworkServer extends vscode.Disposable {
private server: net.Server;
constructor(public ip: string, public port: number) {
super(() => {
this.server.close();
});
let channel = vscode.window.createOutputChannel('Y3-网络服务器');
channel.show();

this.server = net.createServer()
.on('connection', (socket) => {
socket.write('欢迎,你已连接到服务器');
channel.appendLine(`有客户端连接`);

socket.on('data', (data) => {
channel.appendLine(`收到来自客户端的消息:${data}`);
socket.write(`你发送了:${data}`);

if (data.toString().trim() === '再见!') {
socket.end('马达捏~');
}
});

socket.on('close', () => {
channel.appendLine(`客户端已断开`);
});

socket.on('error', (err) => {
channel.appendLine(`客户端错误:${err}`);
});
})
.on('error', (err) => {
channel.appendLine(`服务器错误:${err}`);
})
.listen(port, ip, () => {
channel.appendLine(`服务器已启动:${ip}:${port}`);
channel.appendLine(`客户端发送消息后,会回复“你发送了:<收到的消息>”`);
channel.appendLine(`客户端发送“再见!”会断开连接`);
});
}
}

0 comments on commit 5f5e573

Please sign in to comment.