|
| 1 | +package org.java_websocket.example; |
| 2 | + |
| 3 | +import org.java_websocket.WebSocket; |
| 4 | +import org.java_websocket.WebSocketImpl; |
| 5 | +import org.java_websocket.drafts.Draft; |
| 6 | +import org.java_websocket.drafts.Draft_6455; |
| 7 | +import org.java_websocket.framing.Framedata; |
| 8 | +import org.java_websocket.framing.FramedataImpl1; |
| 9 | +import org.java_websocket.handshake.ClientHandshake; |
| 10 | +import org.java_websocket.server.DefaultSSLWebSocketServerFactory; |
| 11 | +import org.java_websocket.server.WebSocketServer; |
| 12 | + |
| 13 | +import javax.net.ssl.KeyManagerFactory; |
| 14 | +import javax.net.ssl.SSLContext; |
| 15 | +import javax.net.ssl.TrustManagerFactory; |
| 16 | +import java.io.File; |
| 17 | +import java.io.FileInputStream; |
| 18 | +import java.net.InetSocketAddress; |
| 19 | +import java.net.UnknownHostException; |
| 20 | +import java.nio.ByteBuffer; |
| 21 | +import java.security.KeyStore; |
| 22 | +import java.security.spec.ECField; |
| 23 | +import java.util.Collections; |
| 24 | + |
| 25 | +public class AutobahnSSLServerTest extends WebSocketServer { |
| 26 | + private static int counter = 0; |
| 27 | + |
| 28 | + public AutobahnSSLServerTest(int port, Draft d ) throws UnknownHostException { |
| 29 | + super( new InetSocketAddress( port ), Collections.singletonList( d ) ); |
| 30 | + } |
| 31 | + |
| 32 | + public AutobahnSSLServerTest(InetSocketAddress address, Draft d ) { |
| 33 | + super( address, Collections.singletonList( d ) ); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void onOpen( WebSocket conn, ClientHandshake handshake ) { |
| 38 | + counter++; |
| 39 | + System.out.println( "///////////Opened connection number" + counter ); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onClose( WebSocket conn, int code, String reason, boolean remote ) { |
| 44 | + System.out.println( "closed" ); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onError( WebSocket conn, Exception ex ) { |
| 49 | + System.out.println( "Error:" ); |
| 50 | + ex.printStackTrace(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void onStart() { |
| 55 | + System.out.println( "Server started!" ); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void onMessage( WebSocket conn, String message ) { |
| 60 | + conn.send( message ); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onMessage( WebSocket conn, ByteBuffer blob ) { |
| 65 | + conn.send( blob ); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onWebsocketMessageFragment( WebSocket conn, Framedata frame ) { |
| 70 | + FramedataImpl1 builder = ( FramedataImpl1 ) frame; |
| 71 | + builder.setTransferemasked( false ); |
| 72 | + conn.sendFrame( frame ); |
| 73 | + } |
| 74 | + |
| 75 | + public static void main( String[] args ) throws UnknownHostException { |
| 76 | + WebSocketImpl.DEBUG = false; |
| 77 | + int port; |
| 78 | + try { |
| 79 | + port = new Integer( args[0] ); |
| 80 | + } catch ( Exception e ) { |
| 81 | + System.out.println( "No port specified. Defaulting to 9003" ); |
| 82 | + port = 9003; |
| 83 | + } |
| 84 | + AutobahnSSLServerTest test = new AutobahnSSLServerTest( port, new Draft_6455() ); |
| 85 | + try { |
| 86 | + // load up the key store |
| 87 | + String STORETYPE = "JKS"; |
| 88 | + String KEYSTORE = "keystore.jks"; |
| 89 | + String STOREPASSWORD = "storepassword"; |
| 90 | + String KEYPASSWORD = "keypassword"; |
| 91 | + |
| 92 | + KeyStore ks = KeyStore.getInstance(STORETYPE); |
| 93 | + File kf = new File(KEYSTORE); |
| 94 | + ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray()); |
| 95 | + |
| 96 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
| 97 | + kmf.init(ks, KEYPASSWORD.toCharArray()); |
| 98 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
| 99 | + tmf.init(ks); |
| 100 | + |
| 101 | + SSLContext sslContext = null; |
| 102 | + sslContext = SSLContext.getInstance("TLS"); |
| 103 | + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
| 104 | + |
| 105 | + test.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext)); |
| 106 | + } catch (Exception e) { |
| 107 | + e.printStackTrace(); |
| 108 | + } |
| 109 | + test.setConnectionLostTimeout( 0 ); |
| 110 | + test.start(); |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments