doric-cli:use ws instead

This commit is contained in:
pengfeizhou 2021-02-19 17:57:15 +08:00 committed by osborn
parent 60b61f66ca
commit d40cd13c56
4 changed files with 87 additions and 87 deletions

View File

@ -4,7 +4,7 @@
{
"type": "node",
"request": "launch",
"name": "Debug TS",
"name": "Doric Debugger",
"program": "${workspaceFolder}/${relativeFile}",
"preLaunchTask": "Doric Build",
"sourceMaps": true,

View File

@ -32,14 +32,17 @@
"commander": "^6.0.0",
"glob": "^7.1.6",
"keypress": "^0.2.1",
"nodejs-websocket": "^1.7.2",
"qrcode-terminal": "^0.12.0",
"rollup": "^2.23.0",
"shelljs": "^0.8.4",
"source-map-merger": "^0.2.0",
"typescript": "^3.9.7"
"typescript": "^3.9.7",
"ws": "^7.4.3"
},
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"devDependencies": {
"@types/ws": "^7.4.0"
}
}

View File

@ -59,7 +59,6 @@ export default async function dev() {
process.stdin.resume();
await delay(3000);
console.warn("Start watching");
server.listen(7777);
const cachedContents: Record<string, string> = {}
chokidar
.watch(process.cwd() + "/bundle", {
@ -87,8 +86,8 @@ export default async function dev() {
if (fs.existsSync(sourceMap)) {
mergeMap(sourceMap);
}
server.connections.forEach((e: any) => {
e.sendText(
server.clients.forEach((e) => {
e.send(
JSON.stringify({
cmd: "RELOAD",
script: content,

View File

@ -1,36 +1,35 @@
import fs from "fs";
import { exec, spawn } from "child_process";
import ws from "nodejs-websocket";
import WebSocket from "ws"
import "colors";
import path from "path";
import { delay, glob } from "./util";
import { Shell } from "./shell";
export async function createServer() {
console.log("Create Server")
let contextId: string = "0"
let clientConnection: any = null
let debuggerConnection: any = null
let clientConnection: WebSocket | undefined = undefined
let debuggerConnection: WebSocket | undefined = undefined
let deviceId = 0
const server = (ws as any).createServer((connection: any) => {
let thisDeviceId = deviceId++
console.log('Connected', connection.headers.host)
if (connection.headers.host.startsWith("localhost")) {
return new WebSocket.Server({ port: 7777 })
.on("connection", (ws, request) => {
let thisDeviceId = `Client#${deviceId++}`
console.log('Connected', request.headers.host)
if (request.headers.host?.startsWith("localhost")) {
console.log(`Debugger ${thisDeviceId} attached to dev kit`.green)
debuggerConnection = connection
clientConnection.sendText(JSON.stringify({
debuggerConnection = ws
clientConnection?.send(JSON.stringify({
cmd: 'SWITCH_TO_DEBUG',
contextId: contextId
}), () => { })
}))
} else {
console.log(`Client ${thisDeviceId} attached to dev kit`.green)
console.log(`${thisDeviceId} attached to dev kit`.green)
}
connection.on('text', async function (result: string) {
ws.on('text', async function (result: string) {
let resultObject = JSON.parse(result)
switch (resultObject.cmd) {
case 'DEBUG':
clientConnection = connection;
(server as any).debugging = true;
clientConnection = ws;
(ws as any).debugging = true;
console.log("Enter debugging");
contextId = resultObject.data.contextId;
const projectHome = '.';
@ -51,7 +50,7 @@ export async function createServer() {
}
sourceFile = tsFiles[0];
}
console.log(connection.key + " request debug, project home: " + projectHome);
console.log(thisDeviceId + " request debug, project home: " + projectHome);
await Shell.exec("code", [projectHome, sourceFile]);
await delay(1500);
break;
@ -68,28 +67,27 @@ export async function createServer() {
let logContent = resultObject.data.message as string
if (resultObject.data.type == 'DEFAULT') {
console.log(`${timeStr} Device ${thisDeviceId} ${"[I]".green} ${logContent.green}`.bgBlue);
console.log(`${timeStr} ${thisDeviceId} ${"[I]".green} ${logContent.green}`.bgBlue);
} else if (resultObject.data.type == 'ERROR') {
console.log(`${timeStr} Device ${thisDeviceId} ${"[E]".green} ${logContent.green}`.bgRed);
console.log(`${timeStr} ${thisDeviceId} ${"[E]".green} ${logContent.green}`.bgRed);
} else if (resultObject.data.type == 'WARN') {
console.log(`${timeStr.black} ${("Device " + thisDeviceId).black} ${"[W]".green} ${logContent.green}`.bgYellow);
console.log(`${timeStr.black} ${thisDeviceId.black} ${"[W]".green} ${logContent.green}`.bgYellow);
}
break
}
})
connection.on('connect', function (code: number) {
ws.on('connect', function (code: number) {
console.log('connect', code)
})
connection.on('close', function (code: number) {
console.log('close: code = ' + code, connection.key)
ws.on('close', function (code: number) {
console.log('close: code = ' + code, thisDeviceId)
console.log("quit debugging");
(server as any).debugging = false
(ws as any).debugging = false
})
connection.on('error', function (code: number) {
ws.on('error', function (code: number) {
console.log('error', code)
})
})
return server
}