|
| 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.WebSocket; |
| 27 | +import org.java_websocket.WebSocketImpl; |
| 28 | +import org.java_websocket.framing.Framedata; |
| 29 | +import org.java_websocket.handshake.ClientHandshake; |
| 30 | +import org.java_websocket.server.WebSocketServer; |
| 31 | + |
| 32 | +import java.io.BufferedReader; |
| 33 | +import java.io.IOException; |
| 34 | +import java.io.InputStreamReader; |
| 35 | +import java.net.InetSocketAddress; |
| 36 | +import java.net.UnknownHostException; |
| 37 | +import java.nio.ByteBuffer; |
| 38 | + |
| 39 | +/** |
| 40 | + * A simple WebSocketServer implementation. Keeps track of a "chatroom". |
| 41 | + */ |
| 42 | +public class ChatServerAttachmentExample extends WebSocketServer { |
| 43 | + Integer index = 0; |
| 44 | + |
| 45 | + public ChatServerAttachmentExample( int port ) throws UnknownHostException { |
| 46 | + super( new InetSocketAddress( port ) ); |
| 47 | + } |
| 48 | + |
| 49 | + public ChatServerAttachmentExample( InetSocketAddress address ) { |
| 50 | + super( address ); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void onOpen( WebSocket conn, ClientHandshake handshake ) { |
| 55 | + conn.setAttachment( index ); |
| 56 | + index++; |
| 57 | + System.out.println( conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room! ID: " + conn.<Integer>getAttachment() ); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void onClose( WebSocket conn, int code, String reason, boolean remote ) { |
| 62 | + System.out.println( conn + " has left the room! ID: " + conn.<Integer>getAttachment() ); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onMessage( WebSocket conn, String message ) { |
| 67 | + System.out.println( conn + ": " + message ); |
| 68 | + } |
| 69 | + @Override |
| 70 | + public void onMessage( WebSocket conn, ByteBuffer message ) { |
| 71 | + System.out.println( conn + ": " + message ); |
| 72 | + } |
| 73 | + |
| 74 | + public static void main( String[] args ) throws InterruptedException , IOException { |
| 75 | + WebSocketImpl.DEBUG = true; |
| 76 | + int port = 8887; // 843 flash policy port |
| 77 | + try { |
| 78 | + port = Integer.parseInt( args[ 0 ] ); |
| 79 | + } catch ( Exception ex ) { |
| 80 | + } |
| 81 | + ChatServerAttachmentExample s = new ChatServerAttachmentExample( port ); |
| 82 | + s.start(); |
| 83 | + System.out.println( "ChatServer started on port: " + s.getPort() ); |
| 84 | + |
| 85 | + BufferedReader sysin = new BufferedReader( new InputStreamReader( System.in ) ); |
| 86 | + while ( true ) { |
| 87 | + String in = sysin.readLine(); |
| 88 | + s.broadcast( in ); |
| 89 | + if( in.equals( "exit" ) ) { |
| 90 | + s.stop(1000); |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + @Override |
| 96 | + public void onError( WebSocket conn, Exception ex ) { |
| 97 | + ex.printStackTrace(); |
| 98 | + if( conn != null ) { |
| 99 | + // some errors like port binding failed may not be assignable to a specific websocket |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onStart() { |
| 105 | + System.out.println("Server started!"); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments