Coder Social home page Coder Social logo

spring-netty-websocket's Introduction

SPRINGBOOT NETTY WEBSOCKET

Quick start

From the command line do:

git clone https://github.com/CodeIslet/spring-netty-websocket.git
cd spring-boot-netty-websocket
mvn clean package
java -jar service/target/*.jar

WebSocket Request & Response

Request

{
    "mapper": "MAPPER_NAME",
    "body": {
        ... Request body
    }
}

Response

{
    "stauts": "response status", // (OK, ERROR, BAD_REQUEST)
    "identifier": "target identifier",
    "message": "message",
    "body": "response body",
    "time": "response time"
}

Annotations

WebSocketController

Controller class (≒ @RestController of SpringMVC)

@WebSocketController
public class Groupcontroller() {
    ...
}

WebSocketRequestMapping

Websocket request handler mapping (≒ @RequestMapping of SpringMVC)

@WebSocketController
public class GroupController() {

    /**
     * Websocket request object
     * {
     *   "mapper": "join",
     *   "body": {
     *     "name": "Joe"
     *   }
     * }
     */
     @WebSocketRequestMapping(value = "join")
     public void join(JoinRequest req, ChannelHandlerContext ctx, CompletableFuture<ResponseEntity> future) {
        ...
     }
     
    /**
     * Websocket request object
     * {
     *   "mapper": "leave",
     *   "body": null (or pass)
     * } 
     @WebSocketRequestMapping(value = "leave")
     public void leave(ChannelHandlerContext ctx, CompletableFuture<ResponseEntity> future) {
        ...
     }
}     

WebSocketControllerAdvice, WebSocketExceptionHandler

≒ ControllerAdvice of SpringMVC

@WebSocketControllerAdvice
public class WebSocketExceptionAdvice {
    
    @WebSocketExceptionHandler(throwables = { GroupProcessingException.class })
    public ResponseEntity groupProcessingExceptionHandler(GroupProcessingException e) {
        return new ResponseEntity(ResponseEntity.ResponseStatus.ERROR, e.getMessage());
    }
}

WebSocket template

Provides helper methods for websocket control.

MessageFrame object

channelType: GROUP(Channel group), SINGLE(Channel)
destination: group name or channel id
message: websocket message string
data: websocket data body object

Single channel helper

  • send
  • join
  • leave

Channel group helper

  • sendGroup
  • joinGroup
  • leaveGroup

PubSub for multiple websocket server instances

You can implement and provide a Publisher or Subscriber interface. this example uses the redis pub/sub provider by default.

(client) <--> [LB] <---> server instances <---> pub/sub channel (default: redis)

spring-netty-websocket's People

Contributors

codeislet avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

raofei

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.