From 0c1b8f8d99c1ba4cff6476fad6f498e105a917c6 Mon Sep 17 00:00:00 2001 From: yuan_yi <1650192445@qq.com> Date: Fri, 6 May 2022 14:52:34 +0800 Subject: [PATCH] websocket --- .../api/app/lib/controllers/nvr/index.js | 10 ++++-- code/VideoAccess-VCMP/api/app/lib/index.js | 3 ++ .../api/app/lib/service/socket.js | 26 ++++++++++++++++ code/VideoAccess-VCMP/web/client/src/app.jsx | 2 +- .../web/client/src/layout/actions/global.js | 5 --- .../web/client/src/layout/actions/index.js | 9 ++++++ .../client/src/layout/actions/webSocket.js | 31 +++++++++++++++++++ .../src/layout/components/header/index.jsx | 10 ++++-- .../web/client/src/layout/index.jsx | 5 +-- .../src/layout/reducers/ajaxResponse.js | 8 ++--- .../web/client/src/layout/reducers/global.js | 15 +++++---- .../web/client/src/layout/reducers/index.js | 4 ++- .../client/src/layout/reducers/webSocket.js | 21 +++++++++++++ .../client/src/sections/auth/actions/auth.js | 4 +-- .../client/src/sections/auth/actions/index.js | 5 +-- .../src/sections/auth/containers/login.jsx | 13 +++++--- .../client/src/sections/auth/reducers/auth.js | 4 +-- .../sections/example/containers/example.jsx | 19 ++++++++++-- 18 files changed, 154 insertions(+), 40 deletions(-) create mode 100644 code/VideoAccess-VCMP/api/app/lib/service/socket.js create mode 100644 code/VideoAccess-VCMP/web/client/src/layout/actions/index.js create mode 100644 code/VideoAccess-VCMP/web/client/src/layout/actions/webSocket.js create mode 100644 code/VideoAccess-VCMP/web/client/src/layout/reducers/webSocket.js diff --git a/code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js b/code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js index 1c00233..56b58ba 100644 --- a/code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js +++ b/code/VideoAccess-VCMP/api/app/lib/controllers/nvr/index.js @@ -46,8 +46,12 @@ async function edit (ctx, next) { async function get (ctx) { const models = ctx.fs.dc.models; try { - const { limit, offset, orderBy, orderDirection } = ctx.query + const { limit, page, orderBy, orderDirection } = ctx.query let findOption = { + attributes: { exclude: ['delete'] }, + where: { + delete: false, + }, order: [ [orderBy || 'id', orderDirection || 'DESC'] ] @@ -55,8 +59,8 @@ async function get (ctx) { if (limit) { findOption.limit = limit } - if (offset) { - findOption.offset = offset + if (page && limit) { + findOption.offset = page * limit } const res = await models.Nvr.findAll(findOption) diff --git a/code/VideoAccess-VCMP/api/app/lib/index.js b/code/VideoAccess-VCMP/api/app/lib/index.js index d5668f6..c135a43 100644 --- a/code/VideoAccess-VCMP/api/app/lib/index.js +++ b/code/VideoAccess-VCMP/api/app/lib/index.js @@ -2,6 +2,7 @@ const routes = require('./routes'); const redisConnect = require('./service/redis') +const socketConect = require('./service/socket') const authenticator = require('./middlewares/authenticator'); // const apiLog = require('./middlewares/api-log'); const businessRest = require('./middlewares/business-rest'); @@ -13,7 +14,9 @@ module.exports.entry = function (app, router, opts) { app.fs.api.authAttr = app.fs.api.authAttr || {}; app.fs.api.logAttr = app.fs.api.logAttr || {}; + // 顺序固定 ↓ redisConnect(app, opts) + socketConect(app, opts) router.use(authenticator(app, opts)); // router.use(businessRest(app, router, opts)); diff --git a/code/VideoAccess-VCMP/api/app/lib/service/socket.js b/code/VideoAccess-VCMP/api/app/lib/service/socket.js new file mode 100644 index 0000000..28957f3 --- /dev/null +++ b/code/VideoAccess-VCMP/api/app/lib/service/socket.js @@ -0,0 +1,26 @@ +'use strict'; + +module.exports = async function factory (app, opts) { + + app.socket.on('connection', async (socket) => { + console.info('WEB_SOCKET ' + socket.handshake.query.token + ' 已连接'); + + socket.on('disconnecting', async (reason) => { + console.info('WEB_SOCKET ' + socket.handshake.query.token + ' 已断开连接:' + reason); + }) + }) + + // 使用测试 + // setInterval(async () => { + // const { connected } = app.socket.sockets + // const roomId = 'ROOM_' + Math.random() + // if (connected) { + // for (let c in connected) { + // connected[c].join(roomId) + // } + // app.socket.to(roomId).emit('TEST', { someProperty: `【星域 ROOM:${roomId}】呼叫自然选择号!!!`, }) + // } + + // app.socket.emit('TEST', { someProperty: '【广播】呼叫青铜时代号!!!', }) + // }, 1000) +} diff --git a/code/VideoAccess-VCMP/web/client/src/app.jsx b/code/VideoAccess-VCMP/web/client/src/app.jsx index dfd09e7..f6d9bb7 100644 --- a/code/VideoAccess-VCMP/web/client/src/app.jsx +++ b/code/VideoAccess-VCMP/web/client/src/app.jsx @@ -3,7 +3,7 @@ import React, { useEffect } from 'react'; import Layout from './layout'; import Auth from './sections/auth'; -// import Example from './sections/example'; +import Example from './sections/example'; import EquipmentWarehouse from './sections/equipmentWarehouse'; const App = props => { diff --git a/code/VideoAccess-VCMP/web/client/src/layout/actions/global.js b/code/VideoAccess-VCMP/web/client/src/layout/actions/global.js index 71b5c97..0548a95 100644 --- a/code/VideoAccess-VCMP/web/client/src/layout/actions/global.js +++ b/code/VideoAccess-VCMP/web/client/src/layout/actions/global.js @@ -2,8 +2,6 @@ import { RouteRequest } from '@peace/utils'; import { RouteTable } from '$utils' -// import io from 'socket.io-client'; - export const INIT_LAYOUT = 'INIT_LAYOUT'; export function initLayout (title, copyright, sections, actions) { return { @@ -35,9 +33,6 @@ export function initApiRoot () { return dispatch => { RouteRequest.get(RouteTable.apiRoot).then(res => { localStorage.setItem('apiRoot', res.root); - - // const socket = io('ws://127.0.0.1:4000', () => { }); - dispatch({ type: INIT_API_ROOT, payload: { diff --git a/code/VideoAccess-VCMP/web/client/src/layout/actions/index.js b/code/VideoAccess-VCMP/web/client/src/layout/actions/index.js new file mode 100644 index 0000000..4e98b4d --- /dev/null +++ b/code/VideoAccess-VCMP/web/client/src/layout/actions/index.js @@ -0,0 +1,9 @@ +'use strict'; + +import * as global from './global' +import * as socket from './webSocket'; + +export default { + ...global, + ...socket, +}; \ No newline at end of file diff --git a/code/VideoAccess-VCMP/web/client/src/layout/actions/webSocket.js b/code/VideoAccess-VCMP/web/client/src/layout/actions/webSocket.js new file mode 100644 index 0000000..8a5fe12 --- /dev/null +++ b/code/VideoAccess-VCMP/web/client/src/layout/actions/webSocket.js @@ -0,0 +1,31 @@ +'use strict'; +import io from 'socket.io-client'; + +export const INIT_WEB_SOCKET = 'INIT_WEB_SOCKET' +export function initWebSocket ({ ioUrl, token }) { + if (!ioUrl) { + ioUrl = localStorage.getItem('apiRoot') + } + if (!token) { + const user = sessionStorage.getItem('user') + token = JSON.parse(user).token + } + if (!ioUrl || !token) { + return { + type: '', + } + } + return dispatch => { + const socket = io(ioUrl, { + query: { + token: token + } + }); + dispatch({ + type: INIT_WEB_SOCKET, + payload: { + socket: socket + } + }) + } +} \ No newline at end of file diff --git a/code/VideoAccess-VCMP/web/client/src/layout/components/header/index.jsx b/code/VideoAccess-VCMP/web/client/src/layout/components/header/index.jsx index 2954b53..457c815 100644 --- a/code/VideoAccess-VCMP/web/client/src/layout/components/header/index.jsx +++ b/code/VideoAccess-VCMP/web/client/src/layout/components/header/index.jsx @@ -4,7 +4,7 @@ import { connect } from 'react-redux'; import { Nav } from '@douyinfe/semi-ui'; const Header = props => { - const { dispatch, history, user, actions } = props + const { dispatch, history, user, actions, socket } = props return (