diff --git a/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/config/Event.java b/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/config/Event.java index 99ee6a7..75caa7f 100644 --- a/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/config/Event.java +++ b/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/config/Event.java @@ -19,16 +19,6 @@ public interface Event { */ String CHAT = "chat" ; - /** - * 收到消息 - */ - String CHAT_RECEIVED = "chat_received" ; - - /** - * 拒收消息 - */ - String CHAT_REFUSED = "chat_refused" ; - /** * 广播消息 */ diff --git a/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/config/ServerConfig.java b/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/config/ServerConfig.java index d61be0d..8baccc1 100644 --- a/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/config/ServerConfig.java +++ b/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/config/ServerConfig.java @@ -9,11 +9,11 @@ import org.springframework.context.annotation.Configuration; /** *

- * 服务器配置 + * websocket服务器配置 *

* * @package: com.xkcoding.websocket.socketio.config - * @description: 服务器配置 + * @description: websocket服务器配置 * @author: yangkai.shen * @date: Created in 2018-12-18 16:42 * @copyright: Copyright (c) 2018 diff --git a/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/handler/MessageEventHandler.java b/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/handler/MessageEventHandler.java index 3f7b530..3610115 100644 --- a/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/handler/MessageEventHandler.java +++ b/spring-boot-demo-websocket-socketio/src/main/java/com/xkcoding/websocket/socketio/handler/MessageEventHandler.java @@ -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; @@ -106,9 +107,11 @@ public class MessageEventHandler { 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() + "不在线)")); } } diff --git a/spring-boot-demo-websocket-socketio/src/main/resources/static/index.html b/spring-boot-demo-websocket-socketio/src/main/resources/static/index.html index 48a16ff..1e6db31 100644 --- a/spring-boot-demo-websocket-socketio/src/main/resources/static/index.html +++ b/spring-boot-demo-websocket-socketio/src/main/resources/static/index.html @@ -62,14 +62,6 @@ output(`系统通知: 收到来自 ${data.fromUid} 的悄悄话: ${data.message}`); }); - socket.on('chat_received', function (data) { - output(`系统通知: 悄悄话, ${data}`); - }); - - socket.on('chat_refused', function (data) { - output(`系统通知: 悄悄话, ${data}`); - }); - socket.on('group', function (data) { output(`${data.groupId} 群消息: ${data.fromUid} 说: ${data.message}`); }); @@ -153,8 +145,13 @@ toUid: toUserId, message: message }; - socket.emit('chat', singleRequest, function () { + socket.emit('chat', singleRequest, function (data) { output(`系统通知: 你刚刚和 ${singleRequest.toUid} 说了句悄悄话`); + if (data && data.flag) { + output(`系统通知: 悄悄话, ${data.message}`); + } else { + output(`系统通知: 悄悄话, ${data.message}`); + } }); }