Skip to content

Commit

Permalink
✨ spring-boot-demo-websocket 完成
Browse files Browse the repository at this point in the history
  • Loading branch information
xkcoding committed Dec 19, 2018
1 parent d575bf5 commit 4ece8df
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ public interface Event {
*/
String CHAT = "chat" ;

/**
* 收到消息
*/
String CHAT_RECEIVED = "chat_received" ;

/**
* 拒收消息
*/
String CHAT_REFUSED = "chat_refused" ;

/**
* 广播消息
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

/**
* <p>
* 服务器配置
* websocket服务器配置
* </p>
*
* @package: com.xkcoding.websocket.socketio.config
* @description: 服务器配置
* @description: websocket服务器配置
* @author: yangkai.shen
* @date: Created in 2018-12-18 16:42
* @copyright: Copyright (c) 2018
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xkcoding.websocket.socketio.handler;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
Expand Down Expand Up @@ -106,9 +107,11 @@ public void onChatEvent(SocketIOClient client, AckRequest request, SingleMessage
if (toUser.isPresent()) {
log.info("用户 {} 刚刚私信了用户 {}:{}", data.getFromUid(), data.getToUid(), data.getMessage());
sendToSingle(toUser.get(), data);
client.sendEvent(Event.CHAT_RECEIVED, "发送成功");
request.sendAckData(Dict.create().set("flag", true).set("message", "发送成功"));
} else {
client.sendEvent(Event.CHAT_REFUSED, "发送失败,对方不想理你");
request.sendAckData(Dict.create()
.set("flag", false)
.set("message", "发送失败,对方不想理你(" + data.getToUid() + "不在线)"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@
output(`<span class="username-msg">系统通知: 收到来自 ${data.fromUid} 的悄悄话: ${data.message}</span>`);
});

socket.on('chat_received', function (data) {
output(`<span class="username-msg">系统通知: 悄悄话, ${data}</span>`);
});

socket.on('chat_refused', function (data) {
output(`<span class="username-msg">系统通知: 悄悄话, ${data}</span>`);
});

socket.on('group', function (data) {
output(`<span class="username-msg">${data.groupId} 群消息: ${data.fromUid} 说: ${data.message}</span>`);
});
Expand Down Expand Up @@ -153,8 +145,13 @@
toUid: toUserId,
message: message
};
socket.emit('chat', singleRequest, function () {
socket.emit('chat', singleRequest, function (data) {
output(`<span class="username-msg">系统通知: 你刚刚和 ${singleRequest.toUid} 说了句悄悄话</span>`);
if (data && data.flag) {
output(`<span class="username-msg">系统通知: 悄悄话, ${data.message}</span>`);
} else {
output(`<span class="disconnect-msg">系统通知: 悄悄话, ${data.message}</span>`);
}
});
}

Expand Down

0 comments on commit 4ece8df

Please sign in to comment.