Skip to content

Commit cf55a49

Browse files
committed
Example how to reject a handshake as a server
1 parent 0ce902c commit cf55a49

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
importorg.java_websocket.WebSocket;
27+
importorg.java_websocket.WebSocketImpl;
28+
importorg.java_websocket.drafts.Draft;
29+
importorg.java_websocket.exceptions.InvalidDataException;
30+
importorg.java_websocket.framing.CloseFrame;
31+
importorg.java_websocket.handshake.ClientHandshake;
32+
importorg.java_websocket.handshake.ServerHandshakeBuilder;
33+
34+
importjava.io.BufferedReader;
35+
importjava.io.IOException;
36+
importjava.io.InputStreamReader;
37+
importjava.net.InetSocketAddress;
38+
39+
/**
40+
* This example shows how to reject a handshake as a server from a client.
41+
*
42+
* For this you have to override onWebsocketHandshakeReceivedAsServer in your WebSocketServer class
43+
*/
44+
publicclassServerRejectHandshakeExampleextendsChatServer{
45+
46+
publicServerRejectHandshakeExample( intport ){
47+
super( newInetSocketAddress( port ));
48+
}
49+
50+
@Override
51+
publicServerHandshakeBuilderonWebsocketHandshakeReceivedAsServer( WebSocketconn, Draftdraft, ClientHandshakerequest) throwsInvalidDataException{
52+
ServerHandshakeBuilderbuilder = super.onWebsocketHandshakeReceivedAsServer( conn, draft, request );
53+
//In this example we don't allow any resource descriptor ( "ws://localhost:8887/?roomid=1 will be rejected but ws://localhost:8887 is fine)
54+
if (! request.getResourceDescriptor().equals( "/" )){
55+
thrownewInvalidDataException( CloseFrame.POLICY_VALIDATION, "Not accepted!");
56+
}
57+
//If there are no cookies set reject it as well.
58+
if (!request.hasFieldValue( "Cookie" )){
59+
thrownewInvalidDataException( CloseFrame.POLICY_VALIDATION, "Not accepted!");
60+
}
61+
//If the cookie does not contain a specific value
62+
if (!request.getFieldValue( "Cookie" ).equals( "username=nemo" )){
63+
thrownewInvalidDataException( CloseFrame.POLICY_VALIDATION, "Not accepted!");
64+
}
65+
returnbuilder;
66+
}
67+
68+
69+
publicstaticvoidmain( String[] args ) throwsInterruptedException , IOException{
70+
WebSocketImpl.DEBUG = true;
71+
intport = 8887; // 843 flash policy port
72+
try{
73+
port = Integer.parseInt( args[ 0 ] );
74+
} catch ( Exceptionex ){
75+
}
76+
ServerRejectHandshakeExamples = newServerRejectHandshakeExample( port );
77+
s.start();
78+
System.out.println( "Server started on port: " + s.getPort() );
79+
80+
BufferedReadersysin = newBufferedReader( newInputStreamReader( System.in ) );
81+
while ( true ){
82+
Stringin = sysin.readLine();
83+
s.broadcast( in );
84+
if( in.equals( "exit" ) ){
85+
s.stop();
86+
break;
87+
}
88+
}
89+
}
90+
}

0 commit comments

Comments
(0)