-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix attachment lose #900 #918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
/** | ||
* Copyright (c) 2012-2019 Nikita Koksharov | ||
* | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
|
@@ -22,9 +22,14 @@ | |
import com.corundumstudio.socketio.store.StoreFactory; | ||
import com.corundumstudio.socketio.store.pubsub.DispatchMessage; | ||
import com.corundumstudio.socketio.store.pubsub.PubSubType; | ||
import io.netty.buffer.Unpooled; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Author: liangjiaqi | ||
|
@@ -80,12 +85,15 @@ public void disconnect() { | |
} | ||
|
||
@Override | ||
public void sendEvent(String name, SocketIOClient excludedClient, Object... data) { | ||
public void sendEvent(String name, SocketIOClient excludedClient, @NonNull Object... data) { | ||
Packet packet = new Packet(PacketType.MESSAGE, EngineIOVersion.UNKNOWN); | ||
packet.setSubType(PacketType.EVENT); | ||
packet.setName(name); | ||
packet.setData(Arrays.asList(data)); | ||
|
||
// handle byte[] data | ||
handleBytes(packet, data); | ||
|
||
for (SocketIOClient client : clients) { | ||
packet.setEngineIOVersion(client.getEngineIOVersion()); | ||
if (client.getSessionId().equals(excludedClient.getSessionId())) { | ||
|
@@ -97,14 +105,31 @@ public void sendEvent(String name, SocketIOClient excludedClient, Object... data | |
} | ||
|
||
@Override | ||
public void sendEvent(String name, Object... data) { | ||
public void sendEvent(String name, @NonNull Object... data) { | ||
Packet packet = new Packet(PacketType.MESSAGE, EngineIOVersion.UNKNOWN); | ||
packet.setSubType(PacketType.EVENT); | ||
packet.setName(name); | ||
packet.setData(Arrays.asList(data)); | ||
|
||
// handle byte[] data | ||
handleBytes(packet, data); | ||
|
||
send(packet); | ||
} | ||
|
||
private static void handleBytes(Packet packet, Object[] data) { | ||
List<byte[]> bytes = Arrays.stream(data) | ||
.filter(o -> o instanceof byte[]) | ||
.map(b -> (byte[]) b) | ||
.filter(b -> b.length > 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are no empty byte arrays |
||
.collect(Collectors.toList()); | ||
|
||
if (!CollectionUtils.isEmpty(bytes)) { | ||
packet.initAttachments(bytes.size()); | ||
bytes.stream().peek(b -> packet.addAttachment(Unpooled.wrappedBuffer(b))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the same is done in encodePacket() method |
||
} | ||
} | ||
|
||
@Override | ||
public <T> void sendEvent(String name, Object data, BroadcastAckCallback<T> ackCallback) { | ||
for (SocketIOClient client : clients) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,7 +259,7 @@ private void handleWebsocket(final OutPacketMessage msg, ChannelHandlerContext c | |
for (ByteBuf buf : packet.getAttachments()) { | ||
ByteBuf outBuf = encoder.allocateBuffer(ctx.alloc()); | ||
outBuf.writeByte(4); | ||
outBuf.writeBytes(buf); | ||
outBuf.writeBytes(buf, 0, buf.readableBytes()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is what outBuf.writeBytes(buf) exactly does |
||
if (log.isTraceEnabled()) { | ||
log.trace("Out attachment: {} sessionId: {}", ByteBufUtil.hexDump(outBuf), msg.getSessionId()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spring package can't be used