CzRger il y a 7 mois
Parent
commit
4eab11144c

+ 3 - 0
src/app.js

@@ -34,3 +34,6 @@ const WebSocket = require('ws');
 // 创建WebSocket服务器
 const {wsShipPlayback} = require('./ship-playback/routers/ws-ship-playback');
 wsShipPlayback(new WebSocket.Server({ port: 18062 }))
+// 创建WebSocket服务器
+const {wsShipHistory} = require('./seat/routers/ws-ship-history');
+wsShipHistory(new WebSocket.Server({ port: 8010 }))

+ 19 - 0
src/seat/config/api.js

@@ -0,0 +1,19 @@
+const resultCode = {
+    200: '请求成功',
+    500: '请求失败'
+}
+module.exports = class ApiResult {
+    constructor(code, data, msg) {
+        this.code = code
+        this.data = data
+        this.msg = msg
+    }
+    success(data, msg) {
+        const c = 200
+        return new ApiResult(c, data, msg || resultCode[c])
+    }
+    error(data, msg) {
+        const c = 500
+        return new ApiResult(c, data, msg || resultCode[c])
+    }
+}

+ 48 - 0
src/seat/config/db.js

@@ -0,0 +1,48 @@
+const mysql = require('mysql')
+const fs = require("fs");
+const configFile = fs.readFileSync('./config.json');
+const globalConfig = JSON.parse(configFile)
+const shipTestConfig = globalConfig['ship-test'][globalConfig['ship-test']['version']]
+const db_config = shipTestConfig.db
+// 进行数据库交互
+const sqlHandle = (sql, values) => {
+    let connect = mysql.createConnection(db_config)
+    // 开始链接数据库
+    connect.connect(function (err) {
+        if (err) {
+            console.log(`mysql连接失败: ${err}!`)
+        } else {
+            console.log('mysql连接成功!')
+        }
+    })
+    // 返回一个Promise承诺对象
+    return new Promise((resolve, reject) => {
+        connect.query(sql, values, (err, result) => {
+            if (err) {
+                reject(err)
+            } else {
+                // 此处需要将返回数据转为JSON再转回来,否则原数据不为任何数据类型
+                let res = JSON.parse(JSON.stringify(result))
+                console.log('执行:', sql)
+                console.log('参数:', values)
+                console.log('结果:', res)
+                closeMysql(connect)
+                resolve(res)
+            }
+        });
+    })
+}
+// 查询成功后关闭mysql
+const closeMysql = (connect) => {
+    connect.end((err) => {
+        if (err) {
+            console.log(`mysql关闭失败:${err}!`)
+        } else {
+            console.log('mysql关闭成功!')
+        }
+    })
+}
+// 导出方法
+module.exports = {
+    sqlHandle
+}

+ 10 - 0
src/seat/config/page.js

@@ -0,0 +1,10 @@
+const {sqlHandle} = require("./db")
+
+const PageResult = async (sql, pageNum, pageSize, Entity) => {
+    const result = await sqlHandle(sql)
+    return {
+        data: result.slice((pageNum - 1) * pageSize, pageNum * pageSize).map(v => Entity ? new Entity(v) : v),
+        total: result.length
+    }
+}
+module.exports = PageResult

+ 3 - 0
src/seat/routers/index.js

@@ -0,0 +1,3 @@
+const express = require("express")
+const seatRouter = express.Router()
+module.exports = seatRouter

Fichier diff supprimé car celui-ci est trop grand
+ 1550 - 0
src/seat/routers/ws-ship-history.js


+ 1 - 1
src/ship-playback/routers/ws-ship-playback.js

@@ -52,7 +52,7 @@ const sendResult = async (ws, params) => {
       })
     }
     step++
-    if (step === 3600) {
+    if (step === 30) {
       // await delayLoop(3)
       ws.send(JSON.stringify(result))
       result = []