Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
O
on-site-service
Project
Project
Details
Activity
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
external
on-site-service
Commits
ddfd4488
Commit
ddfd4488
authored
Aug 12, 2022
by
shangtx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: SSE引入
parent
3b36b6b6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
0 deletions
+73
-0
SseHandler.java
...main/java/com/onsiteservice/admin/message/SseHandler.java
+73
-0
No files found.
admin/src/main/java/com/onsiteservice/admin/message/SseHandler.java
0 → 100644
View file @
ddfd4488
package
com
.
onsiteservice
.
admin
.
message
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.onsiteservice.core.exception.ServiceException
;
import
com.onsiteservice.core.result.ResultCodeEnum
;
import
com.onsiteservice.core.security.jwt.JwtManager
;
import
io.jsonwebtoken.Claims
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.servlet.mvc.method.annotation.SseEmitter
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* SSE(server sent event) 服务器到客户端单向传输数据
*/
@ConditionalOnProperty
(
prefix
=
"project.sse"
,
name
=
{
"enabled"
},
matchIfMissing
=
true
)
@Component
@Slf4j
public
class
SseHandler
{
private
static
final
Map
<
Long
,
SseEmitter
>
sseMap
;
static
{
sseMap
=
Collections
.
synchronizedMap
(
new
HashMap
<>());
}
public
SseEmitter
connect
(
HttpServletRequest
request
)
{
String
token
=
request
.
getParameter
(
"token"
);
if
(
token
==
null
)
{
throw
new
ServiceException
(
ResultCodeEnum
.
UNAUTHORIZED
.
getCode
(),
"认证信息为空"
);
}
Claims
claims
=
JwtManager
.
parseToken
(
token
.
replaceAll
(
JwtManager
.
BEARER
,
""
));
if
(
claims
==
null
)
{
throw
new
ServiceException
(
ResultCodeEnum
.
UNAUTHORIZED
.
getCode
(),
"认证错误"
);
}
long
userId
=
JSON
.
parseObject
(
claims
.
getSubject
()).
getLong
(
JwtManager
.
USER_ID
);
SseEmitter
emitter
=
new
SseEmitter
(
3600_000L
);
emitter
.
onError
((
e
)
->
{
log
.
error
(
"sse 连接异常 id="
+
userId
,
e
);
remove
(
userId
);
});
emitter
.
onCompletion
(()
->
remove
(
userId
));
emitter
.
onTimeout
(()
->
{
log
.
error
(
"sse 连接超时 id="
+
userId
);
remove
(
userId
);
});
sseMap
.
put
(
userId
,
emitter
);
return
emitter
;
}
public
void
send
(
Object
param
,
Long
userId
)
{
var
emitter
=
sseMap
.
get
(
userId
);
if
(
emitter
!=
null
)
{
try
{
emitter
.
send
(
SseEmitter
.
event
().
data
(
param
));
}
catch
(
Exception
e
)
{
log
.
error
(
"发送sse消息错误 userId="
+
userId
+
"; param="
+
JSONObject
.
toJSONString
(
param
),
e
);
}
}
}
public
void
remove
(
Long
userId
)
{
sseMap
.
remove
(
userId
);
}
}
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