GithubHelp home page GithubHelp logo

abhirockzz / websocket-chat Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 9.0 17 KB

Sample code for the 'Java WebSocket API Handbook'

Home Page: https://www.gitbook.com/book/abhirockzz/java-api-for-websocket-handbook/details

License: MIT License

Java 100.00%
websocket jsr356 tyrus

websocket-chat's Introduction

The is a chat application (a canonical use case for WebSocket) built using the Java WebSocket API (JSR 356). This is meant to help understand the API usage and try it hands on (this is not a full-blown chat service)

Components

  • Uses the Tyrus implementation for the core logic
  • Leverages the Grizzly container support available in Tyrus
  • Packaged as a standalone JAR

Build & run

The application is a Maven project

  • To build, just execute mvn clean install which will produce an independent (fat/uber) JAR
  • Run it using java -jar target/websocket-chat.jar

Features

Here is what you can do with the chat application

  • Join the chat room
  • Send public messages
  • Send private messages
  • Get notified about new users joining
  • Leave the chat room
  • Get notified another user leaves the chat room

Code

Before you explore the source code yourself, here is quick overview

Class(es) Category Description
ChatServer Core It contains the core business logic of the application
WebSocketServerManager Bootstrap Manages bootstrap and shutdown process of the WebSocket container
ChatMessage,
DuplicateUserNotification,
LogOutNotification,
NewJoineeNotification,
Reply,
WelcomeMessage
Domain objects Simple POJOs to model the application level entities
ChatMessageDecoder Decoder Converts chats sent by users into Java (domain) object which can be used within the application
DuplicateUserMessageEncoder
,LogOutMessageEncoder,
NewJoineeMessageEncoder,
ReplyEncoder,
WelcomeMessageEncoder
Encoder(s) Converts Java (domain) objects into native (text) payloads which can be sent over the wire using the WebSocket protocol

Unit tests: The unit tests use the Java client API implementation in Tyrus

Try it out

You would need a WebSocket client for this example - try the Simple WebSocket Client which is a Chrome browser plugin. Here is a transcript

  • Users foo and bar join the chatroom. To do so, you need to connect to the WebSocket endpoint URL e.g. ws://localhost:8080/chat/foo/ (do the same for user bar). foo gets notified about bar
  • User john joins (ws://localhost:8080/chat/john/). foo and bar are notified
  • foo sends a message to everyone (public). Both bar and john get the message
  • bar sends a private message to foo. Only foo gets it
  • In the meanwhile, john gets bored and decides to leave the chat room. Both foo and bar get notified

websocket-chat's People

Contributors

abhirockzz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.