|
1 | | -const express = require("express") |
2 | | -const websocket = require("nodejs-websocket") |
3 | | -const fs = require('fs') |
4 | | - |
5 | | -var addr = "127.0.0.1" |
6 | | -var port_express = 8919 |
7 | | -var port_ws = 9897 |
8 | | - |
9 | | - |
10 | | - |
11 | | -try { |
12 | | - var f = fs.readFile("config/config.json",(err,data) => { |
13 | | - var json_cfg = JSON.parse(data) |
14 | | - addr = json_cfg.server[0].addr |
15 | | - port_express = json_cfg.server[0].web_port |
16 | | - port_ws = json_cfg.server[0].ws_port |
17 | | - console.log("根据配置," + "端口号:" + port_express + " WS端口号:" + port_ws + " 设置的地址:" + addr) |
18 | | - console.log("=======") |
19 | | - app_start() |
20 | | - }) |
21 | | -}catch { |
22 | | - console.log("Error 不过没关系,以按照默认配置启动") |
23 | | -} |
24 | | - |
25 | | -console.log("SB Chat 启动中...") |
26 | | - |
27 | | -const app = express() |
28 | | - |
29 | | - |
30 | | -app.use('/',express.static('pages')) |
31 | | - |
32 | | -app.get("/",(req,res) => { |
33 | | - res.sendFile(__dirname + "/pages/index.html") |
34 | | -}) |
35 | | - |
36 | | -function app_start() { |
37 | | - const wss = websocket.createServer((coon) => { |
38 | | - console.log("[WS]有一个新连接") |
39 | | - coon.on("text",(data) => { |
40 | | - console.log("[message]"+data) |
41 | | - coon.sendText(data) |
42 | | - }) |
43 | | - coon.on("close", function (code, reason) { |
44 | | - console.log("[WS]有一个连接取消了") |
45 | | - }) |
46 | | - coon.on("error",() => { |
47 | | - }) |
48 | | - }).listen(port_ws) |
49 | | - var server = app.listen(port_express,addr,() => { |
50 | | - console.log("网页服务器已启动 位于端口号"+ port_express + "WS端口号" + port_ws + "设置的地址:" + addr) |
51 | | - }) |
52 | | -} |
| 1 | +const express = require("express") |
| 2 | +const websocket = require("nodejs-websocket") |
| 3 | +const fs = require('fs') |
| 4 | +//const db = require('./db.js') /* 未完成的部分 */ |
| 5 | +const tools = require('./tools.js') |
| 6 | + |
| 7 | +//初始化变量 |
| 8 | +var addr = "127.0.0.1" |
| 9 | +//默认地址为127.0.0.1 |
| 10 | +var port_express = 8919 |
| 11 | +//网页服务器端口 |
| 12 | +var port_ws = 9897 |
| 13 | +//websocket端口 |
| 14 | + |
| 15 | +var tool = new tools() |
| 16 | +//初始化工具 |
| 17 | + |
| 18 | +//启动文本 |
| 19 | +console.log("SB Chat 启动中...") |
| 20 | +tool.pass() |
| 21 | + |
| 22 | +try { |
| 23 | + //尝试读取配置文件 |
| 24 | + var f = fs.readFile("config/config.json"/* 读取配置文件 */,(err,data) => { |
| 25 | + //解析配置文件 |
| 26 | + var json_cfg = tool.jsonto(data) |
| 27 | + //设置地址、端口 |
| 28 | + addr = json_cfg.server[0].addr |
| 29 | + port_express = json_cfg.server[0].web_port |
| 30 | + port_ws = json_cfg.server[0].ws_port |
| 31 | + //输出配置 |
| 32 | + console.log("根据配置," + "端口号:" + port_express + " WS端口号:" + port_ws + " 设置的地址:" + addr) |
| 33 | + tool.pass() |
| 34 | + //执行app_start |
| 35 | + app_start() |
| 36 | + }) |
| 37 | +}catch { |
| 38 | + //错误提示,不过并无大碍,一般是读取文件失败 |
| 39 | + console.log("Error了 不过没关系,以按照默认配置启动") |
| 40 | + tool.pass() |
| 41 | + //启动 |
| 42 | + app_start(); |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +//初始化网页文件 |
| 47 | +const app = express() |
| 48 | + |
| 49 | +//未来可能会更新的数据库,用于用户注册登录,注册部分以完成,登录仍需打磨,目前我做不出来,但你可以试试,我很支持,目前是匿名聊天 |
| 50 | +//app.use('/',db) /* 未完成部分 */ |
| 51 | + |
| 52 | +//引用静态文件 |
| 53 | +app.use('/',express.static('pages')) |
| 54 | + |
| 55 | +//请求主页 |
| 56 | +app.get("/",(req,res) => { |
| 57 | + console.log("[GET]请求主页 / ") |
| 58 | + tool.pass() |
| 59 | + res.sendFile(__dirname + "/pages/index.html") |
| 60 | +}) |
| 61 | + |
| 62 | +//程序后端部分,我太懒了,就不注释了 |
| 63 | +function app_start() { |
| 64 | + const wss = websocket.createServer((coon) => { |
| 65 | + console.log("[WS]有一个新连接") |
| 66 | + tool.pass() |
| 67 | + coon.on("text",(data) => { |
| 68 | + console.log("[message]"+data) |
| 69 | + coon.sendText(data) |
| 70 | + }) |
| 71 | + coon.on("close", function (code, reason) { |
| 72 | + console.log("[WS]有一个连接取消了") |
| 73 | + tool.pass() |
| 74 | + }) |
| 75 | + coon.on("error",() => {}) |
| 76 | + }).listen(port_ws) |
| 77 | + var server = app.listen(port_express,addr,() => { |
| 78 | + console.log("网页服务器已启动 位于端口号"+ port_express + "WS端口号" + port_ws + "设置的地址:" + addr) |
| 79 | + tool.pass() |
| 80 | + console.log("你的聊天服务器地址: ws://"+addr+":"+port_ws+" 这会在网页内用到") |
| 81 | + tool.pass() |
| 82 | + }) |
| 83 | +} |
| 84 | + |
0 commit comments