diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/GenerateJsUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/GenerateJsUtils.java index 3f33a17b..f53b7375 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/GenerateJsUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/javascript/GenerateJsUtils.java @@ -124,7 +124,7 @@ public abstract class GenerateJsUtils { var readObject = readObject(registration); protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName - , fieldDefinition.trim(), protocolClazzName, protocolId, protocolClazzName + , fieldDefinition.trim(), protocolClazzName, protocolId, protocolClazzName, protocolClazzName, protocolClazzName , writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim(), protocolClazzName); var outputPath = StringUtils.format("{}/{}/{}.js", protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName); var file = new File(outputPath); @@ -144,7 +144,7 @@ public abstract class GenerateJsUtils { var fieldName = field.getName(); // 生成注释 var fieldNotes = GenerateProtocolNote.fieldNotes(protocolId, fieldName, CodeLanguage.JavaScript); - for(var fieldNote : fieldNotes) { + for (var fieldNote : fieldNotes) { fieldDefinitionBuilder.append(TAB).append(fieldNote).append(LS); } var triple = jsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration); diff --git a/protocol/src/main/resources/javascript/ProtocolTemplate.js b/protocol/src/main/resources/javascript/ProtocolTemplate.js index 14af85df..efa11f9d 100644 --- a/protocol/src/main/resources/javascript/ProtocolTemplate.js +++ b/protocol/src/main/resources/javascript/ProtocolTemplate.js @@ -3,8 +3,10 @@ const {} = function() { {} }; +{}.PROTOCOL_ID = {}; + {}.prototype.protocolId = function() { - return {}; + return {}.PROTOCOL_ID; }; {}.write = function(buffer, packet) { diff --git a/protocol/src/test/javascript/zfoojs/buffer/ByteBuffer.js b/protocol/src/test/javascript/zfoojs/buffer/ByteBuffer.js index bcc684d6..834344ce 100644 --- a/protocol/src/test/javascript/zfoojs/buffer/ByteBuffer.js +++ b/protocol/src/test/javascript/zfoojs/buffer/ByteBuffer.js @@ -20,6 +20,10 @@ const decoder = new TextDecoder('utf-8'); // const encoder = new util.TextEncoder('utf-8'); // const decoder = new util.TextDecoder('utf-8'); +// 现在所有主流浏览器都支持TextDecoder,只有微信小程序不支持TextDecoder(微信浏览器也支持,微信的小程序和浏览器不是同一个js环境) +// https://developers.weixin.qq.com/community/develop/doc/000ca85023ce78c8484e0d1d256400 +// 如果在微信小程序中使用,需要按照上面的链接全局引入TextEncoder相关依赖 + // 在js中long可以支持的最大值 // const maxLong = 9007199254740992; // const minLong = -9007199254740992; diff --git a/protocol/src/test/javascript/zfoojs/packet/ComplexObject.js b/protocol/src/test/javascript/zfoojs/packet/ComplexObject.js index f1fb8edb..9b9b7c2c 100644 --- a/protocol/src/test/javascript/zfoojs/packet/ComplexObject.js +++ b/protocol/src/test/javascript/zfoojs/packet/ComplexObject.js @@ -50,13 +50,15 @@ const ComplexObject = function() { this.sss = new Set(); // Set> this.ssss = new Set(); // Set this.sssss = new Set(); // Set> - // 如果要修改协议并且兼容老协议,需要加上Compatible注解,按照增加的顺序添加order + // 如果要修改协议并且兼容老协议,需要加上Compatible注解,保持Compatible注解的value自增 this.myCompatible = 0; // number this.myObject = null; // ObjectA | null }; +ComplexObject.PROTOCOL_ID = 100; + ComplexObject.prototype.protocolId = function() { - return 100; + return ComplexObject.PROTOCOL_ID; }; ComplexObject.write = function(buffer, packet) { diff --git a/protocol/src/test/javascript/zfoojs/packet/EmptyObject.js b/protocol/src/test/javascript/zfoojs/packet/EmptyObject.js index b6a97892..0680216c 100644 --- a/protocol/src/test/javascript/zfoojs/packet/EmptyObject.js +++ b/protocol/src/test/javascript/zfoojs/packet/EmptyObject.js @@ -3,8 +3,10 @@ const EmptyObject = function() { }; +EmptyObject.PROTOCOL_ID = 0; + EmptyObject.prototype.protocolId = function() { - return 0; + return EmptyObject.PROTOCOL_ID; }; EmptyObject.write = function(buffer, packet) { diff --git a/protocol/src/test/javascript/zfoojs/packet/NormalObject.js b/protocol/src/test/javascript/zfoojs/packet/NormalObject.js index 502cc808..cf5e9a2a 100644 --- a/protocol/src/test/javascript/zfoojs/packet/NormalObject.js +++ b/protocol/src/test/javascript/zfoojs/packet/NormalObject.js @@ -18,11 +18,12 @@ const NormalObject = function() { this.mm = new Map(); // Map this.s = new Set(); // Set this.ssss = new Set(); // Set - this.outCompatibleValue = 0; // number }; +NormalObject.PROTOCOL_ID = 101; + NormalObject.prototype.protocolId = function() { - return 101; + return NormalObject.PROTOCOL_ID; }; NormalObject.write = function(buffer, packet) { @@ -30,8 +31,7 @@ NormalObject.write = function(buffer, packet) { buffer.writeInt(0); return; } - const beforeWriteIndex = buffer.getWriteOffset(); - buffer.writeInt(854); + buffer.writeInt(-1); buffer.writeByte(packet.a); buffer.writeByteArray(packet.aaa); buffer.writeShort(packet.b); @@ -50,8 +50,6 @@ NormalObject.write = function(buffer, packet) { buffer.writeIntPacketMap(packet.mm, 102); buffer.writeIntSet(packet.s); buffer.writeStringSet(packet.ssss); - buffer.writeInt(packet.outCompatibleValue); - buffer.adjustPadding(854, beforeWriteIndex); }; NormalObject.read = function(buffer) { @@ -97,10 +95,6 @@ NormalObject.read = function(buffer) { packet.s = set16; const set17 = buffer.readStringSet(); packet.ssss = set17; - if (buffer.compatibleRead(beforeReadIndex, length)) { - const result18 = buffer.readInt(); - packet.outCompatibleValue = result18; - } if (length > 0) { buffer.setReadOffset(beforeReadIndex + length); } diff --git a/protocol/src/test/javascript/zfoojs/packet/ObjectA.js b/protocol/src/test/javascript/zfoojs/packet/ObjectA.js index ff50a823..4d9cb031 100644 --- a/protocol/src/test/javascript/zfoojs/packet/ObjectA.js +++ b/protocol/src/test/javascript/zfoojs/packet/ObjectA.js @@ -3,11 +3,12 @@ const ObjectA = function() { this.a = 0; // number this.m = new Map(); // Map this.objectB = null; // ObjectB | null - this.innerCompatibleValue = 0; // number }; +ObjectA.PROTOCOL_ID = 102; + ObjectA.prototype.protocolId = function() { - return 102; + return ObjectA.PROTOCOL_ID; }; ObjectA.write = function(buffer, packet) { @@ -15,13 +16,10 @@ ObjectA.write = function(buffer, packet) { buffer.writeInt(0); return; } - const beforeWriteIndex = buffer.getWriteOffset(); - buffer.writeInt(201); + buffer.writeInt(-1); buffer.writeInt(packet.a); buffer.writeIntStringMap(packet.m); buffer.writePacket(packet.objectB, 103); - buffer.writeInt(packet.innerCompatibleValue); - buffer.adjustPadding(201, beforeWriteIndex); }; ObjectA.read = function(buffer) { @@ -37,10 +35,6 @@ ObjectA.read = function(buffer) { packet.m = map1; const result2 = buffer.readPacket(103); packet.objectB = result2; - if (buffer.compatibleRead(beforeReadIndex, length)) { - const result3 = buffer.readInt(); - packet.innerCompatibleValue = result3; - } if (length > 0) { buffer.setReadOffset(beforeReadIndex + length); } diff --git a/protocol/src/test/javascript/zfoojs/packet/ObjectB.js b/protocol/src/test/javascript/zfoojs/packet/ObjectB.js index d3b94eb1..455c6b01 100644 --- a/protocol/src/test/javascript/zfoojs/packet/ObjectB.js +++ b/protocol/src/test/javascript/zfoojs/packet/ObjectB.js @@ -1,11 +1,12 @@ const ObjectB = function() { this.flag = false; // boolean - this.innerCompatibleValue = 0; // number }; +ObjectB.PROTOCOL_ID = 103; + ObjectB.prototype.protocolId = function() { - return 103; + return ObjectB.PROTOCOL_ID; }; ObjectB.write = function(buffer, packet) { @@ -13,11 +14,8 @@ ObjectB.write = function(buffer, packet) { buffer.writeInt(0); return; } - const beforeWriteIndex = buffer.getWriteOffset(); - buffer.writeInt(4); + buffer.writeInt(-1); buffer.writeBoolean(packet.flag); - buffer.writeInt(packet.innerCompatibleValue); - buffer.adjustPadding(4, beforeWriteIndex); }; ObjectB.read = function(buffer) { @@ -29,10 +27,6 @@ ObjectB.read = function(buffer) { const packet = new ObjectB(); const result0 = buffer.readBoolean(); packet.flag = result0; - if (buffer.compatibleRead(beforeReadIndex, length)) { - const result1 = buffer.readInt(); - packet.innerCompatibleValue = result1; - } if (length > 0) { buffer.setReadOffset(beforeReadIndex + length); } diff --git a/protocol/src/test/javascript/zfoojs/packet/SimpleObject.js b/protocol/src/test/javascript/zfoojs/packet/SimpleObject.js index ac839531..fbb81f8e 100644 --- a/protocol/src/test/javascript/zfoojs/packet/SimpleObject.js +++ b/protocol/src/test/javascript/zfoojs/packet/SimpleObject.js @@ -4,8 +4,10 @@ const SimpleObject = function() { this.g = false; // boolean }; +SimpleObject.PROTOCOL_ID = 104; + SimpleObject.prototype.protocolId = function() { - return 104; + return SimpleObject.PROTOCOL_ID; }; SimpleObject.write = function(buffer, packet) { diff --git a/protocol/src/test/javascript/zfoojs/packet/VeryBigObject.js b/protocol/src/test/javascript/zfoojs/packet/VeryBigObject.js index 8761adf0..7f78aa97 100644 --- a/protocol/src/test/javascript/zfoojs/packet/VeryBigObject.js +++ b/protocol/src/test/javascript/zfoojs/packet/VeryBigObject.js @@ -3346,8 +3346,10 @@ const VeryBigObject = function() { this.ssss88 = new Set(); // Set }; +VeryBigObject.PROTOCOL_ID = 1; + VeryBigObject.prototype.protocolId = function() { - return 1; + return VeryBigObject.PROTOCOL_ID; }; VeryBigObject.write = function(buffer, packet) {