1- const https = require ( 'https' ) ; // http
2- const socketIO = require ( 'socket.io' ) ; //socket
3- const app = require ( "express" ) ( ) ; //express
4- const fs = require ( 'fs' ) ; // fs
5- const db = require ( "./src/tables/db" ) ; //db
6- const conf = require ( "./conf/cfg.json" ) ; //conf
7- const utils = require ( "./utils/utils" ) ; //utils
8- const socket = require ( "./src/socket/index" ) //socket handler
9-
10- let tables = { } ;
11- let sql = { } ;
12- if ( conf . db . open ) {
13- // db init
14- let dbData = db . excute ( conf ) ;
15- tables = dbData . tables ;
16- sql = dbData . sql ;
17- app . use ( async function ( req , res , next ) {
18- req . ctx = { } ;
19- req . ctx . tables = tables ;
20- req . ctx . sql = sql ;
21- req . ctx . Sql = Sql ;
22- await next ( ) ;
23- } )
24- console . log ( "db init..." )
25- }
26-
27-
28- //log flow init --日志流水初始
29- app . use ( async function ( req , res , next ) {
30- res . tl = { } ;
31- res . tl . flowId = utils . genFlow ( ) ;
32- await next ( ) ;
33- } )
34- console . log ( "flow init..." )
35-
1+ const https = require ( 'https' ) ;
2+ const socketIO = require ( 'socket.io' ) ;
3+ const fs = require ( 'fs' ) ;
4+ const db = require ( "./src/tables/db" ) ;
5+ const conf = require ( "./conf/cfg.json" ) ;
6+ const socket = require ( "./src/socket/index" )
367
378//Socket连接监听
389let options = {
39- key : fs . readFileSync ( './conf/keys/server.key' ) ,
40- cert : fs . readFileSync ( './conf/keys/server.crt' )
10+ key : fs . readFileSync ( './conf/keys/server.key' ) ,
11+ cert : fs . readFileSync ( './conf/keys/server.crt' )
4112}
4213let io = socketIO . listen (
43- https . createServer ( options ) . listen ( conf . ws . ssl_port )
14+ https . createServer ( options ) . listen ( conf . ws . ssl_port )
4415) ;
45- conf . ws . io = io ;
46- socket . excute ( tables , sql , conf ) ;
47- console . log ( "socket init..." )
4816
17+ if ( ! conf . db . open ) { // 没开db
18+
19+ console . log ( "db not open ..." )
20+ socket . excute ( { } , { } , io ) ;
21+ console . log ( "socket init done ..." )
22+ console . log ( "socket server listen on " , conf . ws . port , " successful" ) ;
23+
24+ } else { // 开了db
4925
50- console . log ( "socket listen on " , conf . ws . ssl_port , " successful" ) ;
26+ ( async ( ) => {
27+ let { tables, dbClient } = await db . excute ( conf )
28+ console . log ( "db init done ..." )
29+ socket . excute ( tables , dbClient , io ) ;
30+ console . log ( "socket init done ..." )
31+ console . log ( "socket server listen on " , conf . ws . port , " successful" ) ;
32+ } ) ( ) ;
33+ }
0 commit comments