From the command line do:
git clone https://github.com/modorigoon/netty-websocket.git
cd netty-websocket
mvn clean package
java -jar service/target/*.jar
{
"mapper": "MAPPER_NAME",
"body": {
... Request body
}
}
{
"stauts": "response status", // (OK, ERROR, BAD_REQUEST)
"identifier": "target identifier",
"message": "message",
"body": "response body",
"time": "response time"
}
Controller class (≒ @RestController of SpringMVC)
@WebSocketController
public class Groupcontroller() {
...
}
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) {
...
}
}
≒ ControllerAdvice of SpringMVC
@WebSocketControllerAdvice
public class WebSocketExceptionAdvice {
@WebSocketExceptionHandler(throwables = { GroupProcessingException.class })
public ResponseEntity groupProcessingExceptionHandler(GroupProcessingException e) {
return new ResponseEntity(ResponseEntity.ResponseStatus.ERROR, e.getMessage());
}
}
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
- send
- join
- leave
- sendGroup
- joinGroup
- leaveGroup
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)