66import java .util .logging .Level ;
77import java .util .logging .Logger ;
88import javafx .application .Application ;
9+ import javafx .application .Platform ;
910import javafx .beans .value .ChangeListener ;
1011import javafx .beans .value .ObservableValue ;
1112import javafx .scene .Scene ;
1213import javafx .scene .control .TextArea ;
1314import javafx .stage .Stage ;
15+
16+ import javax .websocket .ClientEndpoint ;
1417import javax .websocket .ContainerProvider ;
1518import javax .websocket .DeploymentException ;
19+ import javax .websocket .OnMessage ;
20+ import javax .websocket .OnOpen ;
1621import javax .websocket .Session ;
1722import javax .websocket .WebSocketContainer ;
1823
1924/**
2025 * @author Arun Gupta
2126 */
27+ @ ClientEndpoint
2228public class GoogleDocClient extends Application {
2329
30+ static TextArea textarea ;
31+
2432 public static void main (String [] args ) {
2533 launch (args );
2634 }
@@ -30,22 +38,18 @@ public void start(Stage stage) throws Exception {
3038 final Session session = connectToServer ();
3139 System .out .println ("Connected to server: " + session .getId ());
3240 stage .setTitle ("Google Docs Emulator using WebSocket" );
33- TextArea textarea = new TextArea ();
41+ textarea = new TextArea ();
3442 textarea .textProperty ().addListener (
3543 new ChangeListener <String >() {
3644
3745 @ Override
3846 public void changed (ObservableValue <? extends String > observable , String oldValue , String newValue ) {
3947 System .out .println ("New value: " + newValue );
40- for (Session peer : session .getOpenSessions ()) {
41- System .out .println ("Trying to send data..." );
42- if (!peer .equals (session )) {
43- try {
44- peer .getBasicRemote ().sendText (newValue );
45- } catch (IOException ex ) {
46- Logger .getLogger (GoogleDocClient .class .getName ()).log (Level .SEVERE , null , ex );
47- }
48- }
48+
49+ try {
50+ session .getBasicRemote ().sendText (newValue );
51+ } catch (IOException ex ) {
52+ Logger .getLogger (GoogleDocClient .class .getName ()).log (Level .SEVERE , null , ex );
4953 }
5054 }
5155
@@ -59,9 +63,27 @@ public void changed(ObservableValue<? extends String> observable, String oldValu
5963 stage .show ();
6064 }
6165
66+ @ OnOpen
67+ public void onOpen (Session session ) {
68+ System .out .println ("Connected to endpoint: " + session .getBasicRemote ());
69+ }
70+
71+ @ OnMessage
72+ public void onMessage (String message , Session session ) throws IOException {
73+ final String newMessage = message ;
74+ System .out .println ("Received message in client: " + message );
75+ Platform .runLater (new Runnable () {
76+ @ Override
77+ public void run () {
78+ textarea .setText (newMessage );
79+ }
80+ });
81+
82+ }
83+
6284 private Session connectToServer () throws URISyntaxException , DeploymentException , IOException {
6385 WebSocketContainer container = ContainerProvider .getWebSocketContainer ();
64- return container .connectToServer (MyClient .class , new URI ("ws://localhost:8080/server/websocket" ));
86+ return container .connectToServer (GoogleDocClient .class , new URI ("ws://localhost:8080/server/websocket" ));
6587 }
6688
6789}
0 commit comments