mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-06-08 22:15:56 +00:00
fix[ws]: json webSocket codec support attachmnt
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
|
||||
package com.zfoo.net.handler.codec.json;
|
||||
|
||||
import com.zfoo.net.router.attachment.IAttachment;
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
/**
|
||||
@@ -23,12 +24,18 @@ public class JsonPacket {
|
||||
|
||||
private short protocolId;
|
||||
|
||||
private IPacket packet;
|
||||
private short attachmentId;
|
||||
|
||||
public static JsonPacket valueOf(short protocolId, IPacket packet) {
|
||||
private IPacket packet;
|
||||
private IAttachment attachment;
|
||||
|
||||
|
||||
public static JsonPacket valueOf(short protocolId, IPacket packet, short attachmentId, IAttachment attachment) {
|
||||
var jsonPacket = new JsonPacket();
|
||||
jsonPacket.protocolId = protocolId;
|
||||
jsonPacket.attachmentId = attachmentId;
|
||||
jsonPacket.packet = packet;
|
||||
jsonPacket.attachment = attachment;
|
||||
return jsonPacket;
|
||||
}
|
||||
|
||||
@@ -47,4 +54,20 @@ public class JsonPacket {
|
||||
public void setPacket(IPacket packet) {
|
||||
this.packet = packet;
|
||||
}
|
||||
|
||||
public short getAttachmentId() {
|
||||
return attachmentId;
|
||||
}
|
||||
|
||||
public void setAttachmentId(short attachmentId) {
|
||||
this.attachmentId = attachmentId;
|
||||
}
|
||||
|
||||
public IAttachment getAttachment() {
|
||||
return attachment;
|
||||
}
|
||||
|
||||
public void setAttachment(IAttachment attachment) {
|
||||
this.attachment = attachment;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ package com.zfoo.net.handler.codec.json;
|
||||
|
||||
import com.zfoo.net.packet.DecodedPacketInfo;
|
||||
import com.zfoo.net.packet.EncodedPacketInfo;
|
||||
import com.zfoo.net.router.attachment.IAttachment;
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import com.zfoo.protocol.buffer.ByteBufUtils;
|
||||
@@ -41,9 +42,15 @@ public class JsonWebSocketCodecHandler extends MessageToMessageCodec<WebSocketFr
|
||||
var jsonMap = JsonUtils.getJsonMap(jsonStr);
|
||||
var protocolId = Short.parseShort(jsonMap.get("protocolId"));
|
||||
var packetStr = jsonMap.get("packet");
|
||||
var attachmentId = Short.parseShort(jsonMap.get("attachmentId"));
|
||||
IAttachment attachment = null;
|
||||
if (attachmentId >= 0) {
|
||||
var attachmentClass = ProtocolManager.getProtocol(attachmentId).protocolConstructor().getDeclaringClass();
|
||||
attachment = (IAttachment) JsonUtils.string2Object(jsonMap.get("attachment"), attachmentClass);
|
||||
}
|
||||
var protocolClass = ProtocolManager.getProtocol(protocolId).protocolConstructor().getDeclaringClass();
|
||||
var packet = JsonUtils.string2Object(packetStr, protocolClass);
|
||||
list.add(DecodedPacketInfo.valueOf((IPacket) packet, null));
|
||||
list.add(DecodedPacketInfo.valueOf((IPacket) packet, attachment));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -51,7 +58,8 @@ public class JsonWebSocketCodecHandler extends MessageToMessageCodec<WebSocketFr
|
||||
var byteBuf = channelHandlerContext.alloc().ioBuffer();
|
||||
|
||||
var packet = out.getPacket();
|
||||
var jsonPacket = JsonPacket.valueOf(packet.protocolId(), packet);
|
||||
var attachmentId = out.getAttachment() == null ? -1 : out.getAttachment().protocolId();
|
||||
var jsonPacket = JsonPacket.valueOf(packet.protocolId(), packet, attachmentId, out.getAttachment());
|
||||
var bytes = StringUtils.bytes(JsonUtils.object2String(jsonPacket));
|
||||
byteBuf.writeBytes(bytes);
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
package com.zfoo.net.router.attachment;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.zfoo.protocol.IPacket;
|
||||
import com.zfoo.scheduler.util.TimeUtils;
|
||||
|
||||
@@ -58,6 +59,7 @@ public class SignalAttachment implements IAttachment {
|
||||
* EN:The method of callback when the client receives a reply from the server
|
||||
* CN:客户端收到服务器回复的时候回调的方法
|
||||
*/
|
||||
@JsonIgnore
|
||||
private transient CompletableFuture<IPacket> responseFuture = new CompletableFuture<>();
|
||||
|
||||
public SignalAttachment() {
|
||||
|
||||
Reference in New Issue
Block a user