Skip to content

Commit d598ce8

Browse files
authored
Merge pull request TooTallNate#656 from marci4/master
Added example for custom header
2 parents 23a0223 + e59362f commit d598ce8

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2010-2018 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
import org.java_websocket.WebSocketImpl;
27+
28+
import java.net.URI;
29+
import java.net.URISyntaxException;
30+
import java.util.HashMap;
31+
import java.util.Map;
32+
33+
/**
34+
* This class shows how to add additional http header like "Origin" or "Cookie".
35+
*
36+
* To see it working, start ServerRejectHandshakeExample and then start this example.
37+
*/
38+
public class CustomHeaderClientExample {
39+
40+
public static void main( String[] args ) throws URISyntaxException, InterruptedException {
41+
WebSocketImpl.DEBUG = true;
42+
Map<String,String> httpHeaders = new HashMap<String, String>();
43+
httpHeaders.put( "Cookie", "test" );
44+
ExampleClient c = new ExampleClient( new URI( "ws://localhost:8887" ), httpHeaders);
45+
//We expect no successful connection
46+
c.connectBlocking();
47+
httpHeaders.put( "Cookie", "username=nemo" );
48+
c = new ExampleClient( new URI( "ws://localhost:8887" ) , httpHeaders);
49+
//Wer expect a successful connection
50+
c.connectBlocking();
51+
c.closeBlocking();
52+
httpHeaders.put( "Access-Control-Allow-Origin", "*" );
53+
c = new ExampleClient( new URI( "ws://localhost:8887" ) , httpHeaders);
54+
//We expect no successful connection
55+
c.connectBlocking();
56+
c.closeBlocking();
57+
httpHeaders.clear();
58+
httpHeaders.put( "Origin", "localhost:8887" );
59+
httpHeaders.put( "Cookie", "username=nemo" );
60+
c = new ExampleClient( new URI( "ws://localhost:8887" ) , httpHeaders);
61+
//We expect a successful connection
62+
c.connectBlocking();
63+
c.closeBlocking();
64+
httpHeaders.clear();
65+
httpHeaders.put( "Origin", "localhost" );
66+
httpHeaders.put( "cookie", "username=nemo" );
67+
c = new ExampleClient( new URI( "ws://localhost:8887" ) , httpHeaders);
68+
//We expect no successful connection
69+
c.connectBlocking();
70+
}
71+
}

src/main/example/ExampleClient.java

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

2626
import java.net.URI;
2727
import java.net.URISyntaxException;
28+
import java.util.Map;
2829

2930
import org.java_websocket.client.WebSocketClient;
3031
import org.java_websocket.drafts.Draft;
@@ -43,6 +44,10 @@ public ExampleClient( URI serverURI ) {
4344
super( serverURI );
4445
}
4546

47+
public ExampleClient( URI serverUri, Map<String, String> httpHeaders ) {
48+
super(serverUri, httpHeaders);
49+
}
50+
4651
@Override
4752
public void onOpen( ServerHandshake handshakedata ) {
4853
send("Hello, it is me. Mario :)");

src/main/example/ServerRejectHandshakeExample.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer( WebSocket co
6262
if (!request.getFieldValue( "Cookie" ).equals( "username=nemo" )) {
6363
throw new InvalidDataException( CloseFrame.POLICY_VALIDATION, "Not accepted!");
6464
}
65+
//If there is a Origin Field, it has to be localhost:8887
66+
if (request.hasFieldValue( "Origin" )) {
67+
if (!request.getFieldValue( "Origin" ).equals( "localhost:8887" )) {
68+
throw new InvalidDataException( CloseFrame.POLICY_VALIDATION, "Not accepted!");
69+
}
70+
}
6571
return builder;
6672
}
6773

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,54 @@ public abstract class WebSocketClient extends AbstractWebSocket implements Runna
6767
*/
6868
protected URI uri = null;
6969

70+
/**
71+
* The underlying engine
72+
*/
7073
private WebSocketImpl engine = null;
7174

75+
/**
76+
* The socket for this WebSocketClient
77+
*/
7278
private Socket socket = null;
7379

80+
/**
81+
* The used OutputStream
82+
*/
7483
private OutputStream ostream;
7584

85+
/**
86+
* The used proxy, if any
87+
*/
7688
private Proxy proxy = Proxy.NO_PROXY;
7789

90+
/**
91+
* The thread to write outgoing message
92+
*/
7893
private Thread writeThread;
7994

95+
/**
96+
* The draft to use
97+
*/
8098
private Draft draft;
8199

100+
/**
101+
* The additional headers to use
102+
*/
82103
private Map<String,String> headers;
83104

105+
/**
106+
* The latch for connectBlocking()
107+
*/
84108
private CountDownLatch connectLatch = new CountDownLatch( 1 );
85109

110+
/**
111+
* The latch for closeBlocking()
112+
*/
86113
private CountDownLatch closeLatch = new CountDownLatch( 1 );
87114

115+
/**
116+
* The socket timeout value to be used in milliseconds.
117+
*/
88118
private int connectTimeout = 0;
89119

90120
/**
@@ -109,6 +139,31 @@ public WebSocketClient( URI serverUri , Draft protocolDraft ) {
109139
this( serverUri, protocolDraft, null, 0 );
110140
}
111141

142+
/**
143+
* Constructs a WebSocketClient instance and sets it to the connect to the
144+
* specified URI. The channel does not attampt to connect automatically. The connection
145+
* will be established once you call <var>connect</var>.
146+
* @param serverUri the server URI to connect to
147+
* @param httpHeaders Additional HTTP-Headers
148+
* @since 1.3.8
149+
*/
150+
public WebSocketClient( URI serverUri, Map<String,String> httpHeaders) {
151+
this(serverUri, new Draft_6455(), httpHeaders);
152+
}
153+
154+
/**
155+
* Constructs a WebSocketClient instance and sets it to the connect to the
156+
* specified URI. The channel does not attampt to connect automatically. The connection
157+
* will be established once you call <var>connect</var>.
158+
* @param serverUri the server URI to connect to
159+
* @param protocolDraft The draft which should be used for this connection
160+
* @param httpHeaders Additional HTTP-Headers
161+
* @since 1.3.8
162+
*/
163+
public WebSocketClient( URI serverUri , Draft protocolDraft , Map<String,String> httpHeaders) {
164+
this(serverUri, protocolDraft, httpHeaders, 0);
165+
}
166+
112167
/**
113168
* Constructs a WebSocketClient instance and sets it to the connect to the
114169
* specified URI. The channel does not attampt to connect automatically. The connection
@@ -180,7 +235,6 @@ public boolean reconnectBlocking() throws InterruptedException {
180235

181236
/**
182237
* Reset everything relevant to allow a reconnect
183-
*
184238
* @since 1.3.8
185239
*/
186240
private void reset() {

0 commit comments

Comments
 (0)