WebSocket Tutorial 07 – ClientEndpoint Java Application

WebSocket Tutorial 07 – ClientEndpoint Java Application

i’ll start by creating a new project it’sgonna
be WebSocketPrj07 and here i’m gonna create a ChatroomClientEndpoint it’s gonna be in
this package and i’m gonna create a Client class that’s gonna have a main method and
i’m gonna go here and to download and i’m gonna download Tyrus Standalone Client so i’m gonna pickup this guy and i’m gonna
drop it here gonna copy it and drop it here and i’m also gonna go to where i have my glassfish
unziped gonna go glassfish and modules and gonna pickup javax.json and drop it here so
to properties and ‘Add JARs’ and i’m gonna annotate this
one as a ClientEndpoint and here i’m gonna have a default constructor and a processOpen
method that is going to be invoked when the OnOpen event happens
and a processMessage that is gonna be invoked when a message is received and the websocket
server that i am going to be using is gonna be this guy WebSocketPrj02 so chatroomServerEndpoint
with the project name so here in the default constructor i’m gonna pickup so localhost:8080/WebSocketPrj02
and chatroomServerEndpoint this URI and i’m gonna use the ContainerProvider in order to
connect to this websocket server with this uri
and i wanna keep a reference to this session and be able to use it to send messages fromthis
class so i’m gonna define this instance variable ‘session’ and i’m gonna initialize it here
so this.session is equal session and going back to the server the message is gonna be
coming in as JSON so here i’m using buildJsonData passing the username and the message
so that’s the reason actually that i have the javax.json.jar so in processMessage i
need to process this JSON message so i’m gonna do Json.createReader and pass it (new StringReader(message)).readObject.getString(“message”)
i’m gonna get the message here and i’m gonna print it out so System.out.println and the
message is gonna be coming in as ‘username’ and than the message itself so username: and
message and the initial one is gonna be ‘System’ and ‘you are now connected as ‘ the first
message so i’m gonna have a sendMessage method that’s
gonna be using this session to send the message that’s passed in here so session.getBasicRemote().sendText
and it’s gonna be the message sent and going to the Client in the main method i’m gonna
start by instantiating the ChatroomClientEndpoint and i want to be able to read from the command
prompt so i’m gonna pickup a BufferedReader passing in an InputStreamReader which is gonna
take in ‘System.in’ soi’m gonna promt the user to enter their username and gonna sit
in a while loop reading and sending the read messages i’m gonna define this String message
and store the read message inside it and than i am going to use this chatroomClientEndpoint
instance and the sendMessage method that i defined here to send that message
next let me go ahead and start the server ok and here i’m gonna start a new console
view and let me pin this one so gonna run the Client so run as Java Application and
this is the server and this is the client console so let’s say bbb and we’re connected
and let me check here if this guy sends a message so i’m receiving it in the application
client and if the application client send the message
i’m also receiving it in the browser client

4 thoughts on “WebSocket Tutorial 07 – ClientEndpoint Java Application”

  1. With tyrus-standalone-client-1.10.jar it dose not work but 1.3.3 working fine.
    In tyrus Getting Started all the same beetwen 1.3.3 and 1.10.
    What's wrong?

  2. WebSocket Security Tutorial – Setup and run on both GlassFish 4.1 & Tomcat 8 @ https://youtu.be/Am5eTQ-sQn4
    WebSocket Tutorial 15 – ServerEndpoint + CDI + Stateless, Stateful, and Singleton Session Beans @ https://youtu.be/McifXIboiRk
    WebSocket Tutorial 14 – WebSocket Path Mapping + Java Servlet + HttpSession + glassFish 4 + tomcat 8 @ https://youtu.be/92fM1JPqDVI
    WebSocket Tutorial 13 – ServerEndpoint + Java Servlet + HttpSession + glassFish 4 + tomcat 8 @ https://youtu.be/YE8my5AF67s
    WebSocket Java Swing Sketch App. Tutorial (Tomcat 8 + Java 8) @ https://youtu.be/BzQ9KUbfLek
    WebSocket JavaScript Sketch App. Tutorial @ https://youtu.be/wU-FRsLky3Q
    WebSocket JavaFX Sketch App. Tutorial (Tomcat 8 + Java 8) @ https://youtu.be/2xgnSpcAVzE
    WebSocket Tutorial 09 (ClientEndpoint JavaFX chat app w/ encoders & decoders) @ https://youtu.be/0nTqlVOI2Rc
    WebSocket Tutorial 08 (JavaFX Programmatic ClientEndpoint Chat App.) @ https://youtu.be/EVL7fUTrFl4
    WebSocket Tutorial 07 – ClientEndpoint Java Application @ https://youtu.be/KVPXQWJagDg
    WebSocket Tutorial 06 (roundtrip JSON + Message handling) @ https://youtu.be/KmSpk8XWeQY
    WebSocket Tutorial 05 (Programmatic Endpoint chatroom web app.) @ https://youtu.be/nLe8vuxW8b4
    WebSocket Tutorial 04 (Enhanced chatroom web application) @ https://youtu.be/NjUbKcEcLQA
    WebSocket Tutorial 03 (encoders & decoders in a simple chatroom web app.) @ https://youtu.be/4nWZ2Oog_w8
    WebSocket Tutorial 02 (Simple chatroom web app. – Java ServerEndpoint + JavaScript Client) @ https://youtu.be/BikL52HYaZg
    WebSocket (Tutorial 01 – Java Server + JavaScript Client + GlassFish 4.0 + JDK 1.7) @ https://youtu.be/_Fi4vz6oUio

Leave a Reply

Your email address will not be published. Required fields are marked *