Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Springboot netty websocket example

NotificationsYou must be signed in to change notification settings

CodeIslet/spring-netty-websocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quick start

From the command line do:

git clone https://github.com/CodeIslet/spring-netty-websocket.gitcd spring-boot-netty-websocketmvn clean packagejava -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)

@WebSocketControllerpublic class Groupcontroller() {    ...}

WebSocketRequestMapping

Websocket request handler mapping (≒ @RequestMapping of SpringMVC)

@WebSocketControllerpublic 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

@WebSocketControllerAdvicepublic 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 idmessage: websocket message stringdata: 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)

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp