Skip to content

Commit a2b3548

Browse files
committed
Example on how to add additional header serverside
Fixes TooTallNate#490
1 parent ddb673d commit a2b3548

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.WebSocket;
27+
import org.java_websocket.WebSocketImpl;
28+
import org.java_websocket.drafts.Draft;
29+
import org.java_websocket.exceptions.InvalidDataException;
30+
import org.java_websocket.framing.CloseFrame;
31+
import org.java_websocket.handshake.ClientHandshake;
32+
import org.java_websocket.handshake.ServerHandshakeBuilder;
33+
34+
import java.io.BufferedReader;
35+
import java.io.IOException;
36+
import java.io.InputStreamReader;
37+
import java.net.InetSocketAddress;
38+
39+
/**
40+
* This example shows how to add additional headers to your server handshake response
41+
*
42+
* For this you have to override onWebsocketHandshakeReceivedAsServer in your WebSocketServer class
43+
*
44+
* We are simple adding the additional header "Access-Control-Allow-Origin" to our server response
45+
*/
46+
public class ServerAdditionalHeaderExample extends ChatServer {
47+
48+
public ServerAdditionalHeaderExample( int port ) {
49+
super( new InetSocketAddress( port ));
50+
}
51+
52+
@Override
53+
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer( WebSocket conn, Draft draft, ClientHandshake request) throws InvalidDataException {
54+
ServerHandshakeBuilder builder = super.onWebsocketHandshakeReceivedAsServer( conn, draft, request );
55+
builder.put( "Access-Control-Allow-Origin" , "*");
56+
return builder;
57+
}
58+
59+
60+
public static void main( String[] args ) throws InterruptedException , IOException {
61+
WebSocketImpl.DEBUG = true;
62+
int port = 8887; // 843 flash policy port
63+
try {
64+
port = Integer.parseInt( args[ 0 ] );
65+
} catch ( Exception ex ) {
66+
}
67+
ServerAdditionalHeaderExample s = new ServerAdditionalHeaderExample( port );
68+
s.start();
69+
System.out.println( "Server started on port: " + s.getPort() );
70+
71+
BufferedReader sysin = new BufferedReader( new InputStreamReader( System.in ) );
72+
while ( true ) {
73+
String in = sysin.readLine();
74+
s.broadcast( in );
75+
if( in.equals( "exit" ) ) {
76+
s.stop(1000);
77+
break;
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)