Skip to content

Commit a771e0f

Browse files
committed
Add WebSocket.getLocal/RemoteAddress, close AsyncHttpClient#855
1 parent dd3c735 commit a771e0f

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

api/src/main/java/org/asynchttpclient/ws/WebSocket.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,32 @@
1313
package org.asynchttpclient.ws;
1414

1515
import java.io.Closeable;
16+
import java.net.SocketAddress;
1617

1718
/**
1819
* A Websocket client
1920
*/
2021
public interface WebSocket extends Closeable {
2122

23+
/**
24+
* Get remote address client initiated request to.
25+
*
26+
* @return remote address client initiated request to, may be {@code null}
27+
* if asynchronous provider is unable to provide the remote address
28+
*/
29+
SocketAddress getRemoteAddress();
30+
31+
/**
32+
* Get local address client initiated request from.
33+
*
34+
* @return local address client initiated request from, may be {@code null}
35+
* if asynchronous provider is unable to provide the local address
36+
*/
37+
SocketAddress getLocalAddress();
38+
2239
/**
2340
* Send a byte message.
41+
*
2442
* @param message a byte message
2543
* @return this
2644
*/
@@ -30,75 +48,78 @@ public interface WebSocket extends Closeable {
3048
* Allows streaming of multiple binary fragments.
3149
*
3250
* @param fragment binary fragment.
33-
* @param last flag indicating whether or not this is the last fragment.
34-
*
51+
* @param last flag indicating whether or not this is the last fragment.
52+
*
3553
* @return this.
3654
*/
3755
WebSocket stream(byte[] fragment, boolean last);
3856

3957
/**
4058
* Allows streaming of multiple binary fragments.
41-
*
59+
*
4260
* @param fragment binary fragment.
43-
* @param offset starting offset.
44-
* @param len length.
45-
* @param last flag indicating whether or not this is the last fragment.
61+
* @param offset starting offset.
62+
* @param len length.
63+
* @param last flag indicating whether or not this is the last fragment.
4664
* @return this.
4765
*/
4866
WebSocket stream(byte[] fragment, int offset, int len, boolean last);
4967

5068
/**
5169
* Send a text message
70+
*
5271
* @param message a text message
5372
* @return this.
5473
*/
5574
WebSocket sendMessage(String message);
5675

5776
/**
5877
* Allows streaming of multiple text fragments.
59-
*
78+
*
6079
* @param fragment text fragment.
61-
* @param last flag indicating whether or not this is the last fragment.
80+
* @param last flag indicating whether or not this is the last fragment.
6281
* @return this.
6382
*/
6483
WebSocket stream(String fragment, boolean last);
6584

6685
/**
6786
* Send a <code>ping</ping> with an optional payload
6887
* (limited to 125 bytes or less).
69-
*
88+
*
7089
* @param payload the ping payload.
71-
*
90+
*
7291
* @return this.
7392
*/
7493
WebSocket sendPing(byte[] payload);
7594

7695
/**
7796
* Send a <code>ping</ping> with an optional payload
7897
* (limited to 125 bytes or less).
79-
*
98+
*
8099
* @param payload the pong payload.
81100
* @return this.
82101
*/
83102
WebSocket sendPong(byte[] payload);
84103

85104
/**
86105
* Add a {@link WebSocketListener}
106+
*
87107
* @param l a {@link WebSocketListener}
88108
* @return this
89109
*/
90110
WebSocket addWebSocketListener(WebSocketListener l);
91111

92112
/**
93113
* Add a {@link WebSocketListener}
114+
*
94115
* @param l a {@link WebSocketListener}
95116
* @return this
96117
*/
97118
WebSocket removeWebSocketListener(WebSocketListener l);
98119

99120
/**
100121
* Returns <code>true</code> if the WebSocket is open/connected.
101-
*
122+
*
102123
* @return <code>true</code> if the WebSocket is open/connected.
103124
*/
104125
boolean isOpen();
@@ -108,4 +129,3 @@ public interface WebSocket extends Closeable {
108129
*/
109130
void close();
110131
}
111-

providers/netty3/src/main/java/org/asynchttpclient/providers/netty3/ws/NettyWebSocket.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.asynchttpclient.providers.netty3.util.ChannelBufferUtils.channelBuffer2bytes;
1818
import static org.jboss.netty.buffer.ChannelBuffers.wrappedBuffer;
1919

20+
import java.net.SocketAddress;
2021
import java.util.ArrayList;
2122
import java.util.Collection;
2223
import java.util.List;
@@ -67,6 +68,16 @@ public NettyWebSocket(Channel channel, NettyAsyncHttpProviderConfig nettyConfig,
6768
maxBufferSize = nettyConfig.getWebSocketMaxBufferSize();
6869
}
6970

71+
@Override
72+
public SocketAddress getRemoteAddress() {
73+
return channel.getRemoteAddress();
74+
}
75+
76+
@Override
77+
public SocketAddress getLocalAddress() {
78+
return channel.getLocalAddress();
79+
}
80+
7081
@Override
7182
public WebSocket sendMessage(byte[] message) {
7283
channel.write(new BinaryWebSocketFrame(wrappedBuffer(message)));

providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/ws/NettyWebSocket.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.io.ByteArrayOutputStream;
2727
import java.io.IOException;
28+
import java.net.SocketAddress;
2829
import java.util.ArrayList;
2930
import java.util.Collection;
3031
import java.util.List;
@@ -67,6 +68,16 @@ public NettyWebSocket(Channel channel, NettyAsyncHttpProviderConfig nettyConfig,
6768
maxBufferSize = nettyConfig.getWebSocketMaxBufferSize();
6869
}
6970

71+
@Override
72+
public SocketAddress getRemoteAddress() {
73+
return channel.remoteAddress();
74+
}
75+
76+
@Override
77+
public SocketAddress getLocalAddress() {
78+
return channel.localAddress();
79+
}
80+
7081
@Override
7182
public WebSocket sendMessage(byte[] message) {
7283
channel.writeAndFlush(new BinaryWebSocketFrame(wrappedBuffer(message)));

0 commit comments

Comments
 (0)