WebSocket Tutorial 04 (Enhanced chatroom web application)

WebSocket Tutorial 04 (Enhanced chatroom web application)


in this tutorial i’m gonna start by doing
a demo of the application that i’m gonna be building and than i’m gonna step by step build
that application i’ll start by creating a new project i call it WebSocketPrj04 it’s
gonna be runing on GlassFish 4 and i’m gonna be reusing the code from the WebSocketPrj02
so i’ll start from there an build on that and here i’m gonna add a usersTextArea it’s
gonna be a readonly and i’m gonna be passing that as users in the jsonData passed in so
i’ll check if that is different than null in that case i’m gonna clear out the usersTextArea
and repopulate it so i’ll do a while loop and populate it with the different users and
i’m gonna add this code to handle the browser closing and triggering a websocket close event
and next going to the ChatroomServerEndpoint let me change this guy to buildJsomMessageData
and i need to add a function getUserNames() that is gonna give me back the usernames for
the various chatroom users so i’m gonna instantiate a HashSet and return it and i’m gonna iterate
through all the sessions in the chatroomUsers Set and i’m gonna be picking up the username
of each and storing it in my returnSet than returning that Set and i’m also gonna have
a buildJsonUsersData function that is gonna return the different users in as Json data
so i’m gonna use this getUserNames function and pickup an interator from there and i’m
gonna instantiate a JsonArrayBuilder gonna be picking it up through createArrayBuilder
static method on json and than i’m gonna go through all the elements of all usernames
and add them to that Json array and finally i’m gonna put that Json array in a json object
and i’m gonna call it ‘users’ and i’m gonna return that json object as a String
next when a browser for a certain user is closed i need to remove that user from the
list of users in the textarea of the various users in the chatroom so in the handleClose
method here i’m gonna pickup an iterator on the chatroomUsers and iterate over those sessions
sending them the user’s data i’m gonna call buildJsonUsersData which is gonna give me
back those users as a String Json String and i’m gonna do the same thing for the onopen
event in the handleOpen method next in the handleMessage method i’m gonna
move this one to here so if this user has just entered the chatroom so if username is
null i’m gonna now also update the users text area
for the various users to reflect the new user that just logged in next i’m gonna go ahead
and test run the application so i’m gonna run it on chrome

Danny Hutson

4 thoughts on “WebSocket Tutorial 04 (Enhanced chatroom web application)

  1. I found a serous bug: If someone joins / logged in and another user has the window just open but did not logged in, the server will raise exceptions!

  2. This is quite old but for anyone in the future watching this: This code will produce a nullpointer exception (unless it's just me making a mistake every time I try to copy the code). The cause for me at least was the getUsernames() method, specifically the call of toString() on the result of the map operation get(). The exception occured when the method is called from handleMessage(), which is strange, because just before the call, we added the message to the map with a valid username. I did not find out why that is because I expected this to happen during handleOpen(), which makes a call to buildJsonUsersData(), which in turn calls getUsernames() without a valid username residing in the map. That would explain the nullpointer exception to the toString() call. Working with GlassFish has been proven to be especially deceptive, as a normal program would instantly throw an exception and it's over. I advice anyone to always check the log file for your GlassFish server to find errors because without it, I would have never seen my server basically going up in flames while I can't seem to find the issue.

    Long story short: To avoid the exception, I simply rewrote getUsernames() as follows to check for null:

    private Set<String> getUsernames() {

    HashSet<String> returnSet = new HashSet<String>();

    Iterator<Session> iterator = chatroomUsers.iterator();

    while (iterator.hasNext()) {

    String string = (String) iterator.next().getUserProperties().get("username");

    if(string != null) {

    returnSet.add(string);

    }

    }

    return returnSet;

    }

  3. 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 *