Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
vehicle-quality-review
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
王飞
vehicle-quality-review
Commits
7b3d752b
Commit
7b3d752b
authored
Jan 26, 2024
by
王飞
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1、整合websocket。
parent
a16f6728
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
359 additions
and
0 deletions
+359
-0
pom.xml
ruoyi-framework/pom.xml
+6
-0
SemaphoreUtils.java
...in/java/com/ruoyi/framework/websocket/SemaphoreUtils.java
+59
-0
WebSocketConfig.java
...n/java/com/ruoyi/framework/websocket/WebSocketConfig.java
+20
-0
WebSocketServer.java
...n/java/com/ruoyi/framework/websocket/WebSocketServer.java
+104
-0
WebSocketUsers.java
...in/java/com/ruoyi/framework/websocket/WebSocketUsers.java
+170
-0
No files found.
ruoyi-framework/pom.xml
View file @
7b3d752b
...
...
@@ -71,6 +71,12 @@
<version>
9.0.0
</version>
</dependency>
<!-- SpringBoot Websocket -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-websocket
</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/SemaphoreUtils.java
0 → 100644
View file @
7b3d752b
package
com
.
ruoyi
.
framework
.
websocket
;
import
java.util.concurrent.Semaphore
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* 信号量相关处理
*
* @author ruoyi
*/
public
class
SemaphoreUtils
{
/**
* SemaphoreUtils 日志控制器
*/
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
SemaphoreUtils
.
class
);
/**
* 获取信号量
*
* @param semaphore
* @return
*/
public
static
boolean
tryAcquire
(
Semaphore
semaphore
)
{
boolean
flag
=
false
;
try
{
flag
=
semaphore
.
tryAcquire
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"获取信号量异常"
,
e
);
}
return
flag
;
}
/**
* 释放信号量
*
* @param semaphore
*/
public
static
void
release
(
Semaphore
semaphore
)
{
try
{
semaphore
.
release
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"释放信号量异常"
,
e
);
}
}
}
ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/WebSocketConfig.java
0 → 100644
View file @
7b3d752b
package
com
.
ruoyi
.
framework
.
websocket
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.socket.server.standard.ServerEndpointExporter
;
/**
* websocket 配置
*
* @author ruoyi
*/
@Configuration
public
class
WebSocketConfig
{
@Bean
public
ServerEndpointExporter
serverEndpointExporter
()
{
return
new
ServerEndpointExporter
();
}
}
ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/WebSocketServer.java
0 → 100644
View file @
7b3d752b
package
com
.
ruoyi
.
framework
.
websocket
;
import
java.util.concurrent.Semaphore
;
import
javax.websocket.OnClose
;
import
javax.websocket.OnError
;
import
javax.websocket.OnMessage
;
import
javax.websocket.OnOpen
;
import
javax.websocket.Session
;
import
javax.websocket.server.PathParam
;
import
javax.websocket.server.ServerEndpoint
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Component
;
/**
* websocket 消息处理
*
* @author ruoyi
*/
@Component
@ServerEndpoint
(
"/websocket/{userid}"
)
public
class
WebSocketServer
{
/**
* WebSocketServer 日志控制器
*/
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
WebSocketServer
.
class
);
/**
* 默认最多允许同时在线人数100
*/
public
static
int
socketMaxOnlineCount
=
100
;
private
static
Semaphore
socketSemaphore
=
new
Semaphore
(
socketMaxOnlineCount
);
/**
* 连接建立成功调用的方法
*/
@OnOpen
public
void
onOpen
(
Session
session
,
@PathParam
(
"userid"
)
Long
userid
)
throws
Exception
{
boolean
semaphoreFlag
=
false
;
// 尝试获取信号量
semaphoreFlag
=
SemaphoreUtils
.
tryAcquire
(
socketSemaphore
);
if
(!
semaphoreFlag
)
{
// 未获取到信号量
LOGGER
.
error
(
"\n 当前在线人数超过限制数- {}"
,
socketMaxOnlineCount
);
WebSocketUsers
.
sendMessageToUserByText
(
session
,
"当前在线人数超过限制数:"
+
socketMaxOnlineCount
);
session
.
close
();
}
else
{
// 添加用户
WebSocketUsers
.
put
(
session
.
getId
(),
session
,
userid
);
LOGGER
.
info
(
"\n 建立连接 - {}"
,
session
);
LOGGER
.
info
(
"\n 当前人数 - {}"
,
WebSocketUsers
.
getUsers
().
size
());
WebSocketUsers
.
sendMessageToUserByText
(
session
,
"连接成功"
);
}
}
/**
* 连接关闭时处理
*/
@OnClose
public
void
onClose
(
Session
session
)
{
LOGGER
.
info
(
"\n 关闭连接 - {}"
,
session
);
// 移除用户
WebSocketUsers
.
remove
(
session
.
getId
());
// 获取到信号量则需释放
SemaphoreUtils
.
release
(
socketSemaphore
);
}
/**
* 抛出异常时处理
*/
@OnError
public
void
onError
(
Session
session
,
Throwable
exception
)
throws
Exception
{
if
(
session
.
isOpen
())
{
// 关闭连接
session
.
close
();
}
String
sessionId
=
session
.
getId
();
LOGGER
.
info
(
"\n 连接异常 - {}"
,
sessionId
);
LOGGER
.
info
(
"\n 异常信息 - {}"
,
exception
);
// 移出用户
WebSocketUsers
.
remove
(
sessionId
);
// 获取到信号量则需释放
SemaphoreUtils
.
release
(
socketSemaphore
);
}
/**
* 服务器接收到客户端消息时调用的方法
*/
@OnMessage
public
void
onMessage
(
String
message
,
Session
session
)
{
String
msg
=
message
.
replace
(
"你"
,
"我"
).
replace
(
"吗"
,
""
);
WebSocketUsers
.
sendMessageToUserByText
(
session
,
msg
);
}
}
ruoyi-framework/src/main/java/com/ruoyi/framework/websocket/WebSocketUsers.java
0 → 100644
View file @
7b3d752b
package
com
.
ruoyi
.
framework
.
websocket
;
import
java.io.IOException
;
import
java.util.Collection
;
import
java.util.Map
;
import
java.util.Objects
;
import
java.util.Set
;
import
java.util.concurrent.ConcurrentHashMap
;
import
javax.websocket.Session
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* websocket 客户端用户集
*
* @author ruoyi
*/
public
class
WebSocketUsers
{
/**
* WebSocketUsers 日志控制器
*/
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
WebSocketUsers
.
class
);
/**
* 用户集
*/
private
static
Map
<
String
,
WebSocketSession
>
USERS
=
new
ConcurrentHashMap
();
/**
* 存储用户
*
* @param key 唯一键
* @param session 用户信息
*/
public
static
void
put
(
String
key
,
Session
session
,
Long
userId
)
{
USERS
.
put
(
key
,
new
WebSocketSession
(
userId
,
session
));
}
/**
* 移出用户
*
* @param key 键
*/
public
static
boolean
remove
(
String
key
)
{
LOGGER
.
info
(
"\n 正在移出用户 - {}"
,
key
);
WebSocketSession
remove
=
USERS
.
remove
(
key
);
if
(
remove
!=
null
)
{
boolean
containsValue
=
USERS
.
containsValue
(
remove
);
LOGGER
.
info
(
"\n 移出结果 - {}"
,
containsValue
?
"失败"
:
"成功"
);
return
containsValue
;
}
else
{
return
true
;
}
}
/**
* 获取在线用户列表
*
* @return 返回用户集合
*/
public
static
Map
<
String
,
WebSocketSession
>
getUsers
()
{
return
USERS
;
}
/**
* 群发消息文本消息
*
* @param message 消息内容
*/
public
static
void
sendMessageToUsersByText
(
String
message
)
{
Collection
<
WebSocketSession
>
values
=
USERS
.
values
();
for
(
WebSocketSession
value
:
values
)
{
sendMessageToUserByText
(
value
.
getSession
(),
message
);
}
}
/**
* 发送文本消息
*
* @param session 用户session
* @param message 消息内容
*/
public
static
void
sendMessageToUserByText
(
Session
session
,
String
message
)
{
if
(
session
!=
null
)
{
try
{
session
.
getBasicRemote
().
sendText
(
message
);
}
catch
(
IOException
e
)
{
LOGGER
.
error
(
"\n[发送消息异常]"
,
e
);
}
}
else
{
LOGGER
.
info
(
"\n[你已离线]"
);
}
}
/**
* 发送文本消息
*
* @param userId 用户id
* @param message 消息内容
*/
public
static
void
sendMessageToUserByUserId
(
Long
userId
,
String
message
)
{
Map
<
String
,
WebSocketSession
>
users
=
getUsers
();
for
(
String
key
:
users
.
keySet
())
{
WebSocketSession
webSocketSession
=
users
.
get
(
key
);
if
(
webSocketSession
.
getUserId
().
equals
(
userId
))
{
sendMessageToUserByText
(
webSocketSession
.
getSession
(),
message
);
}
}
}
private
static
class
WebSocketSession
{
private
Long
userId
;
private
Session
session
;
@Override
public
boolean
equals
(
Object
o
)
{
if
(
this
==
o
)
return
true
;
if
(
o
==
null
||
getClass
()
!=
o
.
getClass
())
return
false
;
WebSocketSession
that
=
(
WebSocketSession
)
o
;
return
Objects
.
equals
(
userId
,
that
.
userId
)
&&
Objects
.
equals
(
session
,
that
.
session
);
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
userId
,
session
);
}
public
WebSocketSession
(
Long
userId
,
Session
session
)
{
this
.
userId
=
userId
;
this
.
session
=
session
;
}
public
Long
getUserId
()
{
return
userId
;
}
public
void
setUserId
(
Long
userId
)
{
this
.
userId
=
userId
;
}
public
Session
getSession
()
{
return
session
;
}
public
void
setSession
(
Session
session
)
{
this
.
session
=
session
;
}
}
}
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