明树Git Lab
Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
L
letian_backend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zengfanpei
letian_backend
Commits
5cc65650
Commit
5cc65650
authored
Jul 03, 2025
by
zfp1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
cb180f26
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
65 additions
and
47 deletions
+65
-47
package.json
package.json
+1
-0
tcpserver.js
tcpserver.js
+64
-47
No files found.
package.json
View file @
5cc65650
...
...
@@ -20,6 +20,7 @@
"
exceljs
"
:
"
4.4.0
"
,
"
express
"
:
"
4.19.2
"
,
"
form-data
"
:
"
4.0.0
"
,
"
iconv-lite
"
:
"
^0.6.3
"
,
"
ioredis
"
:
"
5.4.1
"
,
"
joi
"
:
"
17.13.1
"
,
"
lodash
"
:
"
4.17.21
"
,
...
...
tcpserver.js
View file @
5cc65650
const
net
=
require
(
'net'
);
const
{
MongoClient
}
=
require
(
'mongodb'
);
const
iconv
=
require
(
'iconv-lite'
);
let
mongourl
=
`mongodb://root:letian2024.@172.16.0.16:27017/letian?authSource=admin`
;
//内网
const
server
=
net
.
createServer
((
socket
)
=>
{
socket
.
on
(
'data'
,
(
data
)
=>
{
console
.
log
(
`收到消息:
${
data
}
`
);
socket
.
write
(
`
${
data
}
`
);
// connectAndInsert(data);
const
gbkString
=
iconv
.
decode
(
data
,
'gbk'
);
console
.
log
(
`收到消息:
${
gbkString
}
`
);
if
(
gbkString
.
includes
(
'LetaleLetale123'
))
{
//登录
socket
.
write
(
'0x20 0x02 0x00 0x00'
)
}
if
(
gbkString
.
includes
(
'C0'
))
{
socket
.
write
(
'0xD0 0x00'
);
}
});
socket
.
on
(
'N82V3VWYU84K36UH'
,
(
data
)
=>
{
const
gbkString
=
iconv
.
decode
(
data
,
'gbk'
);
console
.
log
(
`N82V3VWYU84K36UH
${
gbkString
}
`
);
});
socket
.
on
(
'WO6936WQZ0L1Q4A1'
,
(
data
)
=>
{
const
gbkString
=
iconv
.
decode
(
data
,
'gbk'
);
console
.
log
(
`WO6936WQZ0L1Q4A1
${
gbkString
}
`
);
});
socket
.
on
(
'end'
,
()
=>
{
console
.
log
(
"客户端断开连接"
);
});
});
server
.
listen
(
3001
,
()
=>
{
server
.
listen
(
3001
,
()
=>
{
console
.
log
(
`tcp server启动,监听端口3001`
);
})
async
function
connectAndInsert
()
{
// 创建 MongoDB 客户端
const
client
=
new
MongoClient
(
mongourl
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
,
serverSelectionTimeoutMS
:
5000
,
// 5秒连接超时
});
try
{
// 连接到 MongoDB 服务器
console
.
log
(
' 正在连接 MongoDB...'
);
await
client
.
connect
();
console
.
log
(
'✅ MongoDB 连接成功'
);
// 获取数据库和集合
const
db
=
client
.
db
(
"letian"
);
const
collection
=
db
.
collection
(
"COLLECTION_NAME"
);
// 创建示例数据
const
userData
=
{
name
:
'张三'
,
email
:
'zhangsan@example.com'
,
age
:
30
,
createdAt
:
new
Date
(),
tags
:
[
'nodejs'
,
'developer'
],
address
:
{
city
:
'北京'
,
country
:
'中国'
}
};
// 插入单个文档
console
.
log
(
' 正在插入数据...'
);
const
insertResult
=
await
collection
.
insertOne
(
userData
);
console
.
log
(
`✅ 数据插入成功,文档ID:
${
insertResult
.
insertedId
}
`
);
}
catch
(
err
)
{
console
.
error
(
'❌ 数据库操作出错:'
,
err
);
}
finally
{
// 关闭连接
await
client
.
close
();
console
.
log
(
'🔌 MongoDB 连接已关闭'
);
}
// 创建 MongoDB 客户端
const
client
=
new
MongoClient
(
mongourl
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
,
serverSelectionTimeoutMS
:
5000
,
// 5秒连接超时
});
try
{
// 连接到 MongoDB 服务器
console
.
log
(
' 正在连接 MongoDB...'
);
await
client
.
connect
();
console
.
log
(
'✅ MongoDB 连接成功'
);
// 获取数据库和集合
const
db
=
client
.
db
(
"letian"
);
const
collection
=
db
.
collection
(
"COLLECTION_NAME"
);
// 创建示例数据
const
userData
=
{
name
:
'张三'
,
email
:
'zhangsan@example.com'
,
age
:
30
,
createdAt
:
new
Date
(),
tags
:
[
'nodejs'
,
'developer'
],
address
:
{
city
:
'北京'
,
country
:
'中国'
}
};
// 插入单个文档
console
.
log
(
' 正在插入数据...'
);
const
insertResult
=
await
collection
.
insertOne
(
userData
);
console
.
log
(
`✅ 数据插入成功,文档ID:
${
insertResult
.
insertedId
}
`
);
}
catch
(
err
)
{
console
.
error
(
'❌ 数据库操作出错:'
,
err
);
}
finally
{
// 关闭连接
await
client
.
close
();
console
.
log
(
'🔌 MongoDB 连接已关闭'
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment