mirror of
https://github.com/pure-admin/pure-admin-backend.git
synced 2025-04-24 15:27:17 +08:00
feat: 添加websocket
功能
This commit is contained in:
parent
bea6f13e71
commit
960ee31659
@ -8,16 +8,20 @@
|
||||
"author": "xiaoxian521",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/body-parser": "^1.19.2",
|
||||
"@types/express": "^4.17.14",
|
||||
"@types/express-ws": "^3.0.1",
|
||||
"@types/formidable": "^2.0.5",
|
||||
"@types/jsonwebtoken": "^8.5.9",
|
||||
"@types/node": "^18.11.9",
|
||||
"@types/ws": "^8.5.3",
|
||||
"body-parser": "^1.20.1",
|
||||
"dayjs": "^1.11.6",
|
||||
"dotenv": "^16.0.3",
|
||||
"esno": "^0.16.3",
|
||||
"express": "^4.18.2",
|
||||
"express-swagger-generator": "^1.1.17",
|
||||
"express-ws": "^5.0.2",
|
||||
"formidable": "^2.0.1",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"mysql2": "^2.3.3",
|
||||
|
60
pnpm-lock.yaml
generated
60
pnpm-lock.yaml
generated
@ -1,16 +1,20 @@
|
||||
lockfileVersion: 5.3
|
||||
lockfileVersion: 5.4
|
||||
|
||||
specifiers:
|
||||
'@types/body-parser': ^1.19.2
|
||||
'@types/express': ^4.17.14
|
||||
'@types/express-ws': ^3.0.1
|
||||
'@types/formidable': ^2.0.5
|
||||
'@types/jsonwebtoken': ^8.5.9
|
||||
'@types/node': ^18.11.9
|
||||
'@types/ws': ^8.5.3
|
||||
body-parser: ^1.20.1
|
||||
dayjs: ^1.11.6
|
||||
dotenv: ^16.0.3
|
||||
esno: ^0.16.3
|
||||
express: ^4.18.2
|
||||
express-swagger-generator: ^1.1.17
|
||||
express-ws: ^5.0.2
|
||||
formidable: ^2.0.1
|
||||
jsonwebtoken: ^8.5.1
|
||||
mysql2: ^2.3.3
|
||||
@ -22,23 +26,27 @@ specifiers:
|
||||
winston: ^3.8.2
|
||||
|
||||
devDependencies:
|
||||
'@types/body-parser': 1.19.2
|
||||
'@types/express': 4.17.14
|
||||
'@types/express-ws': 3.0.1
|
||||
'@types/formidable': 2.0.5
|
||||
'@types/jsonwebtoken': 8.5.9
|
||||
'@types/node': 18.11.9
|
||||
'@types/ws': 8.5.3
|
||||
body-parser: 1.20.1
|
||||
dayjs: 1.11.6
|
||||
dotenv: 16.0.3
|
||||
esno: 0.16.3
|
||||
express: 4.18.2
|
||||
express-swagger-generator: 1.1.17
|
||||
express-ws: 5.0.2_express@4.18.2
|
||||
formidable: 2.0.1
|
||||
jsonwebtoken: 8.5.1
|
||||
mysql2: 2.3.3
|
||||
nodemon: 2.0.20
|
||||
open: 8.4.0
|
||||
svg-captcha: 1.4.0
|
||||
ts-node: 10.9.1_1049f756a027b302b59330a6f33f12ef
|
||||
ts-node: 10.9.1_cbe7ovvae6zqfnmtgctpgpys54
|
||||
typescript: 4.8.4
|
||||
winston: 3.8.2
|
||||
|
||||
@ -156,6 +164,14 @@ packages:
|
||||
'@types/range-parser': 1.2.4
|
||||
dev: true
|
||||
|
||||
/@types/express-ws/3.0.1:
|
||||
resolution: {integrity: sha512-VguRXzcpPBF0IggIGpUoM65cZJDfMQxoc6dKoCz1yLzcwcXW7ft60yhq3ygKhyEhEIQFtLrWjyz4AJ1qjmzCFw==}
|
||||
dependencies:
|
||||
'@types/express': 4.17.14
|
||||
'@types/express-serve-static-core': 4.17.31
|
||||
'@types/ws': 8.5.3
|
||||
dev: true
|
||||
|
||||
/@types/express/4.17.14:
|
||||
resolution: {integrity: sha512-TEbt+vaPFQ+xpxFLFssxUDXj5cWCxZJjIcB7Yg0k0GMHGtgtQgpvx/MUQUeAkNbA9AAGrwkAsoeItdTgS7FMyg==}
|
||||
dependencies:
|
||||
@ -200,6 +216,12 @@ packages:
|
||||
'@types/node': 18.11.9
|
||||
dev: true
|
||||
|
||||
/@types/ws/8.5.3:
|
||||
resolution: {integrity: sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w==}
|
||||
dependencies:
|
||||
'@types/node': 18.11.9
|
||||
dev: true
|
||||
|
||||
/abbrev/1.1.1:
|
||||
resolution: {integrity: sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==}
|
||||
dev: true
|
||||
@ -736,7 +758,7 @@ packages:
|
||||
resolution: {integrity: sha512-6slSBEV1lMKcX13DBifvnDFpNno5WXhw4j/ff7RI0y51BZiDqEe5dNhhjhIQ3iCOQuzsm2MbVzmwqbN78BBhPg==}
|
||||
hasBin: true
|
||||
dependencies:
|
||||
tsx: 3.11.0
|
||||
tsx: 3.12.0
|
||||
dev: true
|
||||
|
||||
/esprima/4.0.1:
|
||||
@ -776,6 +798,19 @@ packages:
|
||||
- supports-color
|
||||
dev: true
|
||||
|
||||
/express-ws/5.0.2_express@4.18.2:
|
||||
resolution: {integrity: sha512-0uvmuk61O9HXgLhGl3QhNSEtRsQevtmbL94/eILaliEADZBHZOQUAiHFrGPrgsjikohyrmSG5g+sCfASTt0lkQ==}
|
||||
engines: {node: '>=4.5.0'}
|
||||
peerDependencies:
|
||||
express: ^4.0.0 || ^5.0.0-alpha.1
|
||||
dependencies:
|
||||
express: 4.18.2
|
||||
ws: 7.5.9
|
||||
transitivePeerDependencies:
|
||||
- bufferutil
|
||||
- utf-8-validate
|
||||
dev: true
|
||||
|
||||
/express/4.18.2:
|
||||
resolution: {integrity: sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==}
|
||||
engines: {node: '>= 0.10.0'}
|
||||
@ -1592,7 +1627,7 @@ packages:
|
||||
resolution: {integrity: sha512-XrHUvV5HpdLmIj4uVMxHggLbFSZYIn7HEWsqePZcI50pco+MPqJ50wMGY794X7AOOhxOBAjbkqfAbEe/QMp2Lw==}
|
||||
dev: true
|
||||
|
||||
/ts-node/10.9.1_1049f756a027b302b59330a6f33f12ef:
|
||||
/ts-node/10.9.1_cbe7ovvae6zqfnmtgctpgpys54:
|
||||
resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==}
|
||||
hasBin: true
|
||||
peerDependencies:
|
||||
@ -1623,8 +1658,8 @@ packages:
|
||||
yn: 3.1.1
|
||||
dev: true
|
||||
|
||||
/tsx/3.11.0:
|
||||
resolution: {integrity: sha512-q+q4xxu41+AafVwvAGqtNJ1ekPFd33ZhTMXvgIpHMqv/W89efwDRE9IyjhEAZm5iTHsshKaf1BYWSk789BrNCA==}
|
||||
/tsx/3.12.0:
|
||||
resolution: {integrity: sha512-Z9drA8U1/jRJRWVTS2rOwdsCfkAShwfYBlE+6BVZsZChDsgYsEZZe44MGNNK6V6wQTTtu1kRNV57MsKRMJfdpg==}
|
||||
hasBin: true
|
||||
dependencies:
|
||||
'@esbuild-kit/cjs-loader': 2.4.0
|
||||
@ -1710,6 +1745,19 @@ packages:
|
||||
resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==}
|
||||
dev: true
|
||||
|
||||
/ws/7.5.9:
|
||||
resolution: {integrity: sha512-F+P9Jil7UiSKSkppIiD94dN07AwvFixvLIj1Og1Rl9GGMuNipJnV9JzjD6XuqmAeiswGvUmNLjr5cFuXwNS77Q==}
|
||||
engines: {node: '>=8.3.0'}
|
||||
peerDependencies:
|
||||
bufferutil: ^4.0.1
|
||||
utf-8-validate: ^5.0.2
|
||||
peerDependenciesMeta:
|
||||
bufferutil:
|
||||
optional: true
|
||||
utf-8-validate:
|
||||
optional: true
|
||||
dev: true
|
||||
|
||||
/yallist/2.1.2:
|
||||
resolution: {integrity: sha512-ncTzHV7NvsQZkYe1DW7cbDLm0YpzHmZF5r/iyP3ZnQtMiJ+pjzisCiMNI+Sj+xQF5pXhSHxSB3uDbsBTzY/c2A==}
|
||||
dev: true
|
||||
|
@ -1,4 +1,5 @@
|
||||
import * as express from "express";
|
||||
import * as expressWs from "express-ws";
|
||||
import * as bodyParser from "body-parser";
|
||||
|
||||
class App {
|
||||
@ -8,6 +9,8 @@ class App {
|
||||
this.config();
|
||||
}
|
||||
private config(): void {
|
||||
// 支持websocket
|
||||
expressWs(this.app);
|
||||
// 支持json编码的主体
|
||||
this.app.use(bodyParser.json());
|
||||
// 支持编码的主体
|
||||
|
@ -1,14 +1,13 @@
|
||||
import * as dayjs from "dayjs";
|
||||
import * as mysql from "mysql2";
|
||||
import secret from "../../config";
|
||||
import secret from "../config";
|
||||
import * as jwt from "jsonwebtoken";
|
||||
import { createHash } from "crypto";
|
||||
import Logger from "../../loaders/logger";
|
||||
import { Message } from "../../utils/enums";
|
||||
import Logger from "../loaders/logger";
|
||||
import { Message } from "../utils/enums";
|
||||
import { Request, Response } from "express";
|
||||
// import { createMathExpr } from "svg-captcha";
|
||||
import getFormatDate from "../../utils/date";
|
||||
import { connection } from "../../utils/initMysql";
|
||||
import getFormatDate from "../utils/date";
|
||||
import { connection } from "../utils/mysql";
|
||||
// import { formidable } from "formidable";
|
||||
// let path = require("path");
|
||||
|
@ -1,9 +1,10 @@
|
||||
import app from "./app";
|
||||
// import * as open from "open";
|
||||
import config from "./config";
|
||||
import * as dayjs from "dayjs";
|
||||
import { user } from "./models/mysql";
|
||||
import Logger from "./loaders/logger";
|
||||
import { queryTable } from "./utils/initMysql";
|
||||
import { queryTable } from "./utils/mysql";
|
||||
const expressSwagger = require("express-swagger-generator")(app);
|
||||
expressSwagger(config.options);
|
||||
|
||||
@ -18,7 +19,7 @@ import {
|
||||
searchVague,
|
||||
// upload,
|
||||
// captcha,
|
||||
} from "./router/api/mysql";
|
||||
} from "./router/http";
|
||||
|
||||
app.post("/login", (req, res) => {
|
||||
login(req, res);
|
||||
@ -52,6 +53,30 @@ app.post("/searchVague", (req, res) => {
|
||||
// captcha(req, res);
|
||||
// });
|
||||
|
||||
app.ws("/socket", function (ws, req) {
|
||||
ws.send(
|
||||
`${dayjs(new Date()).format("YYYY年MM月DD日HH时mm分ss秒")}成功连接socket`
|
||||
);
|
||||
|
||||
// 监听客户端是否关闭socket
|
||||
ws.on("close", function (msg) {
|
||||
console.log("客户端已关闭socket", msg);
|
||||
ws.close();
|
||||
});
|
||||
|
||||
// 监听客户端发送的消息
|
||||
ws.on("message", function (msg) {
|
||||
// 如果客户端发送close,服务端主动关闭该socket
|
||||
if (msg === "close") ws.close();
|
||||
|
||||
ws.send(
|
||||
`${dayjs(new Date()).format(
|
||||
"YYYY年MM月DD日HH时mm分ss秒"
|
||||
)}接收到客户端发送的信息,服务端返回信息:${msg}`
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
app
|
||||
.listen(config.port, () => {
|
||||
Logger.info(`
|
||||
|
Loading…
x
Reference in New Issue
Block a user