Skip to content

Commit dc6dd6c

Browse files
committed
✨ 完善逻辑,先加群,才能群聊
1 parent 2e34b52 commit dc6dd6c

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/handler/MessageEventHandler.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xkcoding.websocket.socketio.handler;
22

3+
import cn.hutool.core.util.ObjectUtil;
34
import com.corundumstudio.socketio.AckRequest;
45
import com.corundumstudio.socketio.SocketIOClient;
56
import com.corundumstudio.socketio.SocketIOServer;
@@ -16,6 +17,7 @@
1617
import org.springframework.beans.factory.annotation.Autowired;
1718
import org.springframework.stereotype.Component;
1819

20+
import java.util.Collection;
1921
import java.util.Optional;
2022
import java.util.UUID;
2123

@@ -112,8 +114,21 @@ public void onChatEvent(SocketIOClient client, AckRequest request, SingleMessage
112114

113115
@OnEvent(value = Event.GROUP)
114116
public void onGroupEvent(SocketIOClient client, AckRequest request, GroupMessageRequest data) {
115-
log.info("群号 {} 收到来自 {} 的群聊消息:{}", data.getGroupId(), data.getFromUid(), data.getMessage());
116-
sendToGroup(data);
117+
Collection<SocketIOClient> clients = server.getRoomOperations(data.getGroupId()).getClients();
118+
119+
boolean inGroup = false;
120+
for (SocketIOClient socketIOClient : clients) {
121+
if (ObjectUtil.equal(socketIOClient.getSessionId(), client.getSessionId())) {
122+
inGroup = true;
123+
break;
124+
}
125+
}
126+
if (inGroup) {
127+
log.info("群号 {} 收到来自 {} 的群聊消息:{}", data.getGroupId(), data.getFromUid(), data.getMessage());
128+
sendToGroup(data);
129+
} else {
130+
request.sendAckData("请先加群!");
131+
}
117132
}
118133

119134
/**

spring-boot-demo-websocket-socketio/src/main/resources/static/index.html

+7-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
socket.emit('join', joinRequest);
113113
}
114114

115-
function sendMessage() {
115+
function sendGroup() {
116116
let message = $('#msg').val();
117117

118118
if (message === '') {
@@ -126,7 +126,11 @@
126126
groupId: "666",
127127
message: message
128128
};
129-
socket.emit('group', groupRequest);
129+
socket.emit('group', groupRequest, function (data) {
130+
if (data) {
131+
layer.msg(data, {icon: 5});
132+
}
133+
});
130134
}
131135

132136
function sendChat() {
@@ -171,7 +175,7 @@ <h1>spring-boot-demo-websocket-socketio</h1>
171175
<input id="msg" class="input-xlarge" type="text" placeholder="随便输点啥"/>
172176
<input id="to" class="input-xlarge" type="text" placeholder="私聊发给谁"/>
173177
<button type="button" onClick="sendJoin()" class="btn" id="join">加入群聊</button>
174-
<button type="button" onClick="sendMessage()" class="btn" id="send">群聊</button>
178+
<button type="button" onClick="sendGroup()" class="btn" id="send">群聊</button>
175179
<button type="button" onClick="sendChat()" class="btn" id="chat">私聊</button>
176180
<button type="button" onClick="sendBroadcast()" class="btn">广播消息</button>
177181
<button type="button" onClick="sendConnect()" class="btn">连接</button>

0 commit comments

Comments
 (0)