Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
konke-ai-alibaba-mcp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
何金镒
konke-ai-alibaba-mcp
Commits
154b1284
Commit
154b1284
authored
Sep 16, 2025
by
何金镒
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化WebSocket
parent
62964e3d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
28 deletions
+78
-28
src/main/java/com/ikonke/konkeaialibabamcp/ws/MsgWebSocket.java
...in/java/com/ikonke/konkeaialibabamcp/ws/MsgWebSocket.java
+78
-28
No files found.
src/main/java/com/ikonke/konkeaialibabamcp/ws/MsgWebSocket.java
View file @
154b1284
...
@@ -71,27 +71,29 @@ public class MsgWebSocket {
...
@@ -71,27 +71,29 @@ public class MsgWebSocket {
*/
*/
@OnOpen
@OnOpen
public
void
onOpen
(
Session
session
)
{
public
void
onOpen
(
Session
session
)
{
log
.
info
(
"===>APP新连接:session-{},online-count-{},url={}"
,
session
.
getId
(),
onlineCount
.
get
(),
session
.
getRequestURI
());
String
sn
=
null
;
String
sn
=
null
;
String
ccu
=
null
;
String
ccu
=
null
;
try
{
log
.
info
(
"===>APP新连接:session-{},online-count-{},url={}"
,
session
.
getId
(),
onlineCount
.
get
(),
session
.
getRequestURI
());
if
(
session
.
getRequestParameterMap
().
containsKey
(
"sn"
))
{
if
(
session
.
getRequestParameterMap
().
containsKey
(
"sn"
))
{
sn
=
session
.
getRequestParameterMap
().
get
(
"sn"
).
get
(
0
);
sn
=
session
.
getRequestParameterMap
().
get
(
"sn"
).
get
(
0
);
}
}
if
(
session
.
getRequestParameterMap
().
containsKey
(
"ccu"
))
{
if
(
session
.
getRequestParameterMap
().
containsKey
(
"ccu"
))
{
ccu
=
session
.
getRequestParameterMap
().
get
(
"ccu"
).
get
(
0
);
ccu
=
session
.
getRequestParameterMap
().
get
(
"ccu"
).
get
(
0
);
}
}
if
(
getWebSocket
(
sn
)){
log
.
error
(
"连接被拒绝:sn已存在 - {}"
,
sn
);
session
.
getBasicRemote
().
sendText
(
"SN already connected"
);
session
.
close
();
return
;
}
// 验证 sn 是否符合要求
// 验证 sn 是否符合要求
if
(!
this
.
isValidSn
(
sn
))
{
if
(!
this
.
isValidSn
(
sn
))
{
try
{
// 发送错误信息给客户端
// 发送错误信息给客户端
session
.
getBasicRemote
().
sendText
(
"Invalid SN parameter"
);
session
.
getBasicRemote
().
sendText
(
"Invalid SN parameter"
);
// 关闭连接
// 关闭连接
session
.
close
();
session
.
close
();
log
.
warn
(
"连接被拒绝:无效的SN - {}"
,
sn
);
log
.
warn
(
"连接被拒绝:无效的SN - {}"
,
sn
);
}
catch
(
Exception
e
)
{
log
.
error
(
"关闭无效连接时出错"
,
e
);
}
}
else
{
}
else
{
// 加入Set中
// 加入Set中
sessions
.
put
(
sn
,
session
);
sessions
.
put
(
sn
,
session
);
...
@@ -99,6 +101,19 @@ public class MsgWebSocket {
...
@@ -99,6 +101,19 @@ public class MsgWebSocket {
onlineCount
.
getAndIncrement
();
onlineCount
.
getAndIncrement
();
//校验登录权限
//校验登录权限
}
}
}
catch
(
Exception
e
)
{
log
.
error
(
"建立连接时发生错误:SN={}"
,
sn
,
e
);
if
(
sn
!=
null
)
{
sessions
.
remove
(
sn
);
}
try
{
if
(
session
.
isOpen
())
{
session
.
close
();
}
}
catch
(
Exception
closeException
)
{
log
.
error
(
"关闭连接时发生错误"
,
closeException
);
}
}
}
}
/**
/**
...
@@ -129,21 +144,56 @@ public class MsgWebSocket {
...
@@ -129,21 +144,56 @@ public class MsgWebSocket {
*/
*/
@OnClose
@OnClose
public
void
onClose
(
Session
session
)
{
public
void
onClose
(
Session
session
)
{
String
sn
=
session
.
getRequestParameterMap
().
get
(
"sn"
).
get
(
0
);
String
sn
=
null
;
try
{
if
(
session
.
getRequestParameterMap
().
containsKey
(
"sn"
))
{
sn
=
session
.
getRequestParameterMap
().
get
(
"sn"
).
get
(
0
);
}
if
(
sn
!=
null
)
{
// 从Set中删除
// 从Set中删除
sessions
.
remove
(
sn
);
sessions
.
remove
(
sn
);
// 在线数减少
// 在线数减少
onlineCount
.
getAndDecrement
();
int
currentCount
=
onlineCount
.
decrementAndGet
();
log
.
info
(
"session-{},down-line-count-{}"
,
session
.
getId
(),
onlineCount
.
get
());
log
.
info
(
"连接关闭: session-{}, SN={}, 当前在线数={}"
,
session
.
getId
(),
sn
,
currentCount
);
}
else
{
log
.
warn
(
"关闭未知连接: session-{}"
,
session
.
getId
());
}
}
catch
(
Exception
e
)
{
log
.
error
(
"处理连接关闭时发生错误: session-{}"
,
session
.
getId
(),
e
);
}
}
}
/**
/**
* 发生错误调用的方法
* 发生错误调用的方法
*/
*/
@OnError
@OnError
public
void
onError
(
Session
session
,
Throwable
throwable
)
throws
Exception
{
public
void
onError
(
Session
session
,
Throwable
throwable
)
{
log
.
error
(
"Web Stock Error"
,
throwable
);
String
sn
=
null
;
session
.
getBasicRemote
().
sendText
(
throwable
.
getMessage
());
try
{
log
.
error
(
"WebSocket发生错误: session-{}"
,
session
.
getId
(),
throwable
);
// 获取SN信息
if
(
session
.
getRequestParameterMap
().
containsKey
(
"sn"
))
{
sn
=
session
.
getRequestParameterMap
().
get
(
"sn"
).
get
(
0
);
}
// 尝试发送错误信息给客户端(如果会话仍打开)
if
(
session
.
isOpen
())
{
try
{
session
.
getBasicRemote
().
sendText
(
"Connection error occurred"
);
}
catch
(
Exception
sendException
)
{
log
.
error
(
"发送错误信息失败: session-{}"
,
session
.
getId
(),
sendException
);
}
}
}
catch
(
Exception
e
)
{
log
.
error
(
"处理错误时发生异常"
,
e
);
}
finally
{
// 确保资源清理和计数器更新
if
(
sn
!=
null
)
{
sessions
.
remove
(
sn
);
int
currentCount
=
onlineCount
.
decrementAndGet
();
log
.
info
(
"因错误清理连接: SN={}, 当前在线数={}"
,
sn
,
currentCount
);
}
}
}
}
}
}
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