Skip to content

Commit 8fb2302

Browse files
committed
💡 websocket 改为可配置手动开启的功能
1 parent 8669276 commit 8fb2302

File tree

7 files changed

+59
-7
lines changed

7 files changed

+59
-7
lines changed

blog-admin/src/main/java/com/zyd/blog/core/websocket/server/ZydWebsocketServer.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.zyd.blog.core.websocket.server;
22

3+
import com.zyd.blog.business.service.SysConfigService;
34
import com.zyd.blog.core.websocket.util.WebSocketUtil;
5+
import com.zyd.blog.framework.holder.SpringContextHolder;
46
import lombok.extern.slf4j.Slf4j;
57
import org.springframework.stereotype.Component;
68

@@ -38,9 +40,15 @@ public class ZydWebsocketServer {
3840
*/
3941
@OnOpen
4042
public void onOpen(Session session) {
43+
SysConfigService sysConfigService = SpringContextHolder.getBean(SysConfigService.class);
44+
if (!sysConfigService.enabledWebSocket()) {
45+
webSocketSet.clear();
46+
onlineCount.set(0);
47+
return;
48+
}
4149
webSocketSet.add(session);
4250
int count = onlineCount.incrementAndGet();
43-
log.info("[Socket] 有链接加入,当前在线人数为: {}", count);
51+
log.debug("[Socket] 有链接加入,当前在线人数为: {}", count);
4452

4553
WebSocketUtil.sendOnlineMsg(Integer.toString(count), webSocketSet);
4654
}
@@ -50,8 +58,14 @@ public void onOpen(Session session) {
5058
*/
5159
@OnClose
5260
public void onClose() {
61+
SysConfigService sysConfigService = SpringContextHolder.getBean(SysConfigService.class);
62+
if (!sysConfigService.enabledWebSocket()) {
63+
webSocketSet.clear();
64+
onlineCount.set(0);
65+
return;
66+
}
5367
int count = onlineCount.decrementAndGet();
54-
log.info("[Socket] 有链接关闭,当前在线人数为: {}", count);
68+
log.debug("[Socket] 有链接关闭,当前在线人数为: {}", count);
5569
WebSocketUtil.sendOnlineMsg(Integer.toString(count), webSocketSet);
5670
}
5771

@@ -63,7 +77,13 @@ public void onClose() {
6377
*/
6478
@OnMessage
6579
public void onMessage(String message, Session session) {
66-
log.info("[Socket] {}来自客户端的消息:{}", session.getId(), message);
80+
SysConfigService sysConfigService = SpringContextHolder.getBean(SysConfigService.class);
81+
if (!sysConfigService.enabledWebSocket()) {
82+
webSocketSet.clear();
83+
onlineCount.set(0);
84+
return;
85+
}
86+
log.debug("[Socket] {}来自客户端的消息:{}", session.getId(), message);
6787
}
6888

6989
/**

blog-admin/src/main/resources/templates/config.ftl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,16 @@
649649
</ul>
650650
</div>
651651
</div>
652+
<div class="item form-group">
653+
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="maintenance">启用 WebSocket 通知服务
654+
<i class="fa fa-question-circle" title="当启用时,用户侧会自动链接到服务端的 Websocket 服务上,管理员可以在控制台通过“实验室-推送消息”向用户侧广播消息"></i> </label>
655+
<div class="col-md-6 col-sm-6 col-xs-12 fixed-radio-checkbox">
656+
<ul class="list-unstyled list-inline">
657+
<li><label for="maintenance" class="pointer"> <input type="radio" class="square" checked name="enabledWebSocket" value="1"> 启用 </label> </li>
658+
<li><label for="maintenance" class="pointer"> <input type="radio" class="square" name="enabledWebSocket" value="0"> 禁用 </label></li>
659+
</ul>
660+
</div>
661+
</div>
652662
<div class="item form-group">
653663
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="maintenance">维护通知
654664
<i class="fa fa-question-circle" title="网站在更新前, 可以通过开启该功能,通知用户"></i> </label>
@@ -700,13 +710,13 @@
700710
</div>
701711
</div>
702712
<div class="item form-group">
703-
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="wxGzhAppId">公众号APP_ID <i class="fa fa-question-circle" title="配置微信公众号的appId"></i></label>
713+
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="wxGzhAppId">公众号APP_ID <i class="fa fa-question-circle" title="如果需要将文章分享到微信侧并要求显示封面,则必须配置"></i></label>
704714
<div class="col-md-6 col-sm-6 col-xs-12">
705715
<input type='text' class="form-control" id="wxGzhAppId" name="wxGzhAppId" placeholder="请输入信公众号的appId"/>
706716
</div>
707717
</div>
708718
<div class="item form-group">
709-
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="wxGzhAppSecret">公众号APP_SECRET <i class="fa fa-question-circle" title="配置微信公众号的appSecret"></i></label>
719+
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="wxGzhAppSecret">公众号APP_SECRET <i class="fa fa-question-circle" title="如果需要将文章分享到微信侧并要求显示封面,则必须配置"></i></label>
710720
<div class="col-md-6 col-sm-6 col-xs-12">
711721
<input type='text' class="form-control" id="wxGzhAppSecret" name="wxGzhAppSecret" placeholder="请输入信公众号的appSecret"/>
712722
</div>

blog-core/src/main/java/com/zyd/blog/business/enums/ConfigKeyEnum.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ public enum ConfigKeyEnum {
253253
* 微信公众号AppSecret
254254
*/
255255
WX_GZH_APP_SECRET("wxGzhAppSecret"),
256+
/**
257+
* 是否开启 webSocket
258+
*/
259+
ENABLED_WEBSOCKET("enabledWebSocket"),
256260
;
257261

258262
private final String key;

blog-core/src/main/java/com/zyd/blog/business/service/SysConfigService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,10 @@ public interface SysConfigService {
6565
*/
6666
List<String> getRandomUserAvatar();
6767

68+
/**
69+
* 获取系统是否开启 WebSocket 的配置
70+
* @return true: 开启,false: 关闭
71+
*/
72+
boolean enabledWebSocket();
73+
6874
}

blog-core/src/main/java/com/zyd/blog/business/service/impl/SysConfigServiceImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cn.hutool.core.date.DatePattern;
44
import cn.hutool.core.date.DateUnit;
55
import cn.hutool.core.date.DateUtil;
6+
import cn.hutool.core.util.BooleanUtil;
67
import com.alibaba.fastjson.JSONArray;
78
import com.zyd.blog.business.annotation.RedisCache;
89
import com.zyd.blog.business.consts.DateConst;
@@ -174,4 +175,14 @@ public List<String> getRandomUserAvatar() {
174175
}
175176
return null;
176177
}
178+
179+
@Override
180+
public boolean enabledWebSocket() {
181+
SysConfig sysConfig = this.getByKey(ConfigKeyEnum.ENABLED_WEBSOCKET.getKey());
182+
// 默认开启
183+
if (null == sysConfig) {
184+
return true;
185+
}
186+
return BooleanUtil.toBoolean(sysConfig.getConfigValue());
187+
}
177188
}

blog-web/src/main/resources/static/js/zhyd.core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
_initialized: false,
9393
init: function (options) {
9494
if (!this.isSupported()) {
95-
// console.error('Not support websocket');
95+
console.log('Not supported websocket');
9696
return;
9797
}
9898
var op = $.extend({
@@ -159,7 +159,7 @@
159159
}
160160
},
161161
isSupported: function () {
162-
return 'WebSocket' in window;
162+
return '1' === appConfig.enabledWebSocket && 'WebSocket' in window;
163163
},
164164
send: function (message) {
165165
if (!this._this) {

blog-web/src/main/resources/templates/layout/footer.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
editorAlert: "${config.editorAlert}",
188188
dynamicTitle: "${config.dynamicTitle}",
189189
bubbleWord: "${config.bubbleWord}",
190+
enabledWebSocket: "${config.enabledWebSocket}"
190191
};
191192
192193
var oauthConfig = {

0 commit comments

Comments
 (0)