Skip to content

Commit 2f20b59

Browse files
authored
update 1.0.1
[+] index.html 可以自定义ws地址 [+] 增加了 start.bat [U] 更新了部分注释
1 parent 3df8b9d commit 2f20b59

File tree

7 files changed

+1509
-231
lines changed

7 files changed

+1509
-231
lines changed

config/users.db

12 KB
Binary file not shown.

db.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//[WARN]请注意,此JS仅用于测试,未完成正式开发!
2+
//此文件不做注释!
3+
4+
const sqlite3 = require('sqlite3')
5+
const express = require('express')
6+
const fs = require('fs')
7+
const path = require('path')
8+
const { randomInt } = require('crypto')
9+
const tools = require('./tools.js')
10+
11+
var router = express.Router()
12+
var tool = new tools()
13+
14+
var db = new sqlite3.Database(path.join(__dirname,'config/users.db'),(err) => {
15+
if(err) {
16+
console.log("[数据库]数据库错误,检查数据库users.db是否存在")
17+
tool.pass()
18+
}else {
19+
console.log("[数据库]数据库连接成功")
20+
tool.pass()
21+
}
22+
})
23+
24+
//需要请求参数 user pwd
25+
router.get('/user/reg',(req,res) => {
26+
console.log("[GET]/user/reg 请求参数:")
27+
tool.pass()
28+
29+
var req_json = tool.objto(req.query)
30+
req_json = tool.jsonto(req_json)
31+
console.log(req_json)
32+
tool.pass()
33+
if(req_json.user == "" | req_json.pwd == "" ) {
34+
res.send("用户名或密码为空")
35+
}else if(req_json.user == undefined | req_json.pwd == undefined) {
36+
res.send("所需参数为空!")
37+
}else if(req_json.user != undefined | req_json.pwd != undefined){
38+
var id = randomInt(100000,1000000000)
39+
console.log("为用户随机出来的id是:" + id)
40+
tool.pass()
41+
db.run(`INSERT INTO USERS VALUES(?,?,?,"USER")`,id,req_json.user,req_json.pwd)
42+
db.all(`SELECT * FROM USERS WHERE ID = ?;`,id,(err,rows) => {
43+
console.log("[数据库]数据库返回的结果:" + tool.objto(rows))
44+
tool.pass()
45+
res.send("完成:" + tool.objto(rows) + "<br><h1>请谨记id与密码!</h1>")
46+
})
47+
}
48+
49+
})
50+
51+
52+
//需要请求参数 id pwd
53+
router.get('/user/login',(req,res) => {
54+
console.log("[GET]/user/login 请求参数:")
55+
tool.pass()
56+
57+
var req_json = tool.objto(req.query)
58+
req_json = tool.jsonto(req_json)
59+
60+
console.log(req_json)
61+
tool.pass()
62+
63+
if(req_json.id == "" | req_json.pwd == "" ) {
64+
res.send("ID或密码为空")
65+
}else if(req_json.id == undefined | req_json.pwd == undefined) {
66+
res.send("所需参数为空")
67+
}else if(req_json.id != "" | req_json.pwd != "") {
68+
db.all(`SELECT ID, PWD FROM USERS WHERE ID = ?`,req_json.id,(err,rows) => {
69+
var ret = tool.objto(rows)
70+
ret = tool.jsonto(ret)
71+
console.log("[数据库]数据库返回参数:"+ret[0].PWD)
72+
tool.pass()
73+
if(ret[0].PWD == req_json.pwd) {
74+
res.send("登录成功!ID:" + req_json.id)
75+
}
76+
})
77+
78+
}
79+
80+
})
81+
82+
83+
module.exports = router

index.js

Lines changed: 84 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,84 @@
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

Comments
 (0)