Skip to content

Commit 814cb94

Browse files
authored
Merge pull request TooTallNate#640 from marci4/master
Added example for Sec-WebSocket-Protocol
2 parents 12684ff + 1558381 commit 814cb94

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2010-2017 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+
import org.java_websocket.drafts.Draft_6455;
28+
import org.java_websocket.extensions.IExtension;
29+
import org.java_websocket.protocols.IProtocol;
30+
import org.java_websocket.protocols.Protocol;
31+
32+
import java.net.URI;
33+
import java.net.URISyntaxException;
34+
import java.util.ArrayList;
35+
import java.util.Collections;
36+
37+
/**
38+
* This example demonstrates how to use a specific Sec-WebSocket-Protocol for your connection.
39+
*/
40+
public class SecWebSocketProtocolClientExample {
41+
42+
public static void main( String[] args ) throws URISyntaxException {
43+
WebSocketImpl.DEBUG = true;
44+
// This draft only allows you to use the specific Sec-WebSocket-Protocol without a fallback.
45+
Draft_6455 draft_ocppOnly = new Draft_6455(Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList(new Protocol("ocpp2.0")));
46+
47+
// This draft allows the specific Sec-WebSocket-Protocol and also provides a fallback, if the other endpoint does not accept the specific Sec-WebSocket-Protocol
48+
ArrayList<IProtocol> protocols = new ArrayList<IProtocol>();
49+
protocols.add(new Protocol("ocpp2.0"));
50+
protocols.add(new Protocol(""));
51+
Draft_6455 draft_ocppAndFallBack = new Draft_6455(Collections.<IExtension>emptyList(), protocols);
52+
53+
ExampleClient c = new ExampleClient( new URI( "ws://echo.websocket.org" ), draft_ocppAndFallBack );
54+
c.connect();
55+
}
56+
}

0 commit comments

Comments
 (0)