Skip to content

Commit c1119e2

Browse files
committed
fix attachment lose #900
1 parent 3fe1735 commit c1119e2

File tree

6 files changed

+47
-18
lines changed

6 files changed

+47
-18
lines changed

src/main/java/com/corundumstudio/socketio/BroadcastOperations.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright (c) 2012-2019 Nikita Koksharov
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

src/main/java/com/corundumstudio/socketio/ClientOperations.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public interface ClientOperations {
2727
* Send custom packet.
2828
* But {@link ClientOperations#sendEvent} method
2929
* usage is enough for most cases.
30+
* {@link Packet#attachments} needs to be filled when sending byte[].
31+
* Using {@link io.netty.buffer.Unpooled#wrappedBuffer(byte[])} to
32+
* fill byte[] into {@link Packet#attachments} is the recommended way.
33+
* Before using {@link Packet#addAttachment(io.netty.buffer.ByteBuf)},
34+
* be sure to initialize the number of attachments with
35+
* {@link Packet#initAttachments(int)})}
3036
*
3137
* @param packet - packet to send
3238
*/

src/main/java/com/corundumstudio/socketio/SingleRoomBroadcastOperations.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright (c) 2012-2019 Nikita Koksharov
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,9 +22,14 @@
2222
import com.corundumstudio.socketio.store.StoreFactory;
2323
import com.corundumstudio.socketio.store.pubsub.DispatchMessage;
2424
import com.corundumstudio.socketio.store.pubsub.PubSubType;
25+
import io.netty.buffer.Unpooled;
26+
import org.springframework.lang.NonNull;
27+
import org.springframework.util.CollectionUtils;
2528

2629
import java.util.Arrays;
2730
import java.util.Collection;
31+
import java.util.List;
32+
import java.util.stream.Collectors;
2833

2934
/**
3035
* Author: liangjiaqi
@@ -97,11 +102,24 @@ public void sendEvent(String name, SocketIOClient excludedClient, Object... data
97102
}
98103

99104
@Override
100-
public void sendEvent(String name, Object... data) {
105+
public void sendEvent(String name, @NonNull Object... data) {
101106
Packet packet = new Packet(PacketType.MESSAGE, EngineIOVersion.UNKNOWN);
102107
packet.setSubType(PacketType.EVENT);
103108
packet.setName(name);
104109
packet.setData(Arrays.asList(data));
110+
111+
// handle byte[] data
112+
List<byte[]> bytes = Arrays.stream(data)
113+
.filter(o -> o instanceof byte[])
114+
.map(b -> (byte[]) b)
115+
.filter(b -> b.length > 0)
116+
.collect(Collectors.toList());
117+
118+
if (!CollectionUtils.isEmpty(bytes)) {
119+
packet.initAttachments(bytes.size());
120+
bytes.stream().peek(b -> packet.addAttachment(Unpooled.wrappedBuffer(b)));
121+
}
122+
105123
send(packet);
106124
}
107125

src/main/java/com/corundumstudio/socketio/handler/EncoderHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ private void handleWebsocket(final OutPacketMessage msg, ChannelHandlerContext c
259259
for (ByteBuf buf : packet.getAttachments()) {
260260
ByteBuf outBuf = encoder.allocateBuffer(ctx.alloc());
261261
outBuf.writeByte(4);
262-
outBuf.writeBytes(buf);
262+
outBuf.writeBytes(buf, 0, buf.readableBytes());
263263
if (log.isTraceEnabled()) {
264264
log.trace("Out attachment: {} sessionId: {}", ByteBufUtil.hexDump(outBuf), msg.getSessionId());
265265
}

src/main/java/com/corundumstudio/socketio/protocol/Packet.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
*/
1616
package com.corundumstudio.socketio.protocol;
1717

18+
import com.corundumstudio.socketio.namespace.Namespace;
1819
import io.netty.buffer.ByteBuf;
1920

2021
import java.io.Serializable;
2122
import java.util.ArrayList;
2223
import java.util.Collections;
2324
import java.util.List;
2425

25-
import com.corundumstudio.socketio.namespace.Namespace;
26-
2726
public class Packet implements Serializable {
2827

2928
private static final long serialVersionUID = 4560159536486711426L;
@@ -71,9 +70,9 @@ public void setData(Object data) {
7170

7271
/**
7372
* Get packet data
74-
*
73+
*
7574
* @param <T> the type data
76-
*
75+
*
7776
* <pre>
7877
* @return <b>json object</b> for PacketType.JSON type
7978
* <b>message</b> for PacketType.MESSAGE type
@@ -145,6 +144,16 @@ public void initAttachments(int attachmentsCount) {
145144
this.attachmentsCount = attachmentsCount;
146145
this.attachments = new ArrayList<ByteBuf>(attachmentsCount);
147146
}
147+
148+
/**
149+
*
150+
* It needs to be called when transferring the byte[].
151+
* Recommended Use{@link io.netty.buffer.Unpooled#wrappedBuffer(byte[])}.
152+
* Before using {@link #addAttachment(ByteBuf)},
153+
* be sure to initialize the number of attachments with {@link #initAttachments(int)})}
154+
*
155+
* @param attachment
156+
*/
148157
public void addAttachment(ByteBuf attachment) {
149158
if (this.attachments.size() < attachmentsCount) {
150159
this.attachments.add(attachment);

src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,6 @@ public void encodePacket(Packet packet, ByteBuf buffer, ByteBufAllocator allocat
277277
jsonSupport.writeValue(out, values);
278278

279279
if (!jsonSupport.getArrays().isEmpty()) {
280-
packet.initAttachments(jsonSupport.getArrays().size());
281-
for (byte[] array : jsonSupport.getArrays()) {
282-
packet.addAttachment(Unpooled.wrappedBuffer(array));
283-
}
284280
packet.setSubType(packet.getSubType() == PacketType.ACK
285281
? PacketType.BINARY_ACK : PacketType.BINARY_EVENT);
286282
}

0 commit comments

Comments
 (0)