net-websocket-spring-boot-autoconfigure

a Java WebSocket Framework based on Netty with Spring Boot

License

License

Categories

Categories

Spring Boot Container Microservices Net Auto Application Layer Libs Code Generators config Configuration
GroupId

GroupId

org.onevroad
ArtifactId

ArtifactId

net-websocket-spring-boot-autoconfigure
Last Version

Last Version

0.4.1
Release Date

Release Date

Type

Type

jar
Description

Description

net-websocket-spring-boot-autoconfigure
a Java WebSocket Framework based on Netty with Spring Boot

Download net-websocket-spring-boot-autoconfigure

How to add to project

<!-- https://jarcasting.com/artifacts/org.onevroad/net-websocket-spring-boot-autoconfigure/ -->
<dependency>
    <groupId>org.onevroad</groupId>
    <artifactId>net-websocket-spring-boot-autoconfigure</artifactId>
    <version>0.4.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.onevroad/net-websocket-spring-boot-autoconfigure/
implementation 'org.onevroad:net-websocket-spring-boot-autoconfigure:0.4.1'
// https://jarcasting.com/artifacts/org.onevroad/net-websocket-spring-boot-autoconfigure/
implementation ("org.onevroad:net-websocket-spring-boot-autoconfigure:0.4.1")
'org.onevroad:net-websocket-spring-boot-autoconfigure:jar:0.4.1'
<dependency org="org.onevroad" name="net-websocket-spring-boot-autoconfigure" rev="0.4.1">
  <artifact name="net-websocket-spring-boot-autoconfigure" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.onevroad', module='net-websocket-spring-boot-autoconfigure', version='0.4.1')
)
libraryDependencies += "org.onevroad" % "net-websocket-spring-boot-autoconfigure" % "0.4.1"
[org.onevroad/net-websocket-spring-boot-autoconfigure "0.4.1"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar
org.onevroad : net-websocket-core jar 0.4.1
org.springframework.boot : spring-boot-configuration-processor Optional jar

Project Modules

There are no modules declared in this project.

net-websocket-spring-boot-starter License

The WebSocket is based on netty. It's easier to use for somebody who want to build a WebSocket in a short time.

Support jdk version 1.8 or 1.8+

中文文档

Request Data Format

{
  "e": "event type",
  "t": ["topic name"],
  "d": "data"
}
  • e:event, t:topic, d:data
  • support event type:subscribe, message, cancel, heartbeat
  • Except for the heartbeat event, the topic is a required param.You can send multiple topics at the same time.
  • You can customize the response data format.

Heartbeat Event

  • The server will send a heartbeat event when the client and server have no data interaction within 1 minute. The data format that the server send is like this:
{
  "e": "heartbeat",
  "d": "ping"
}
  • If the client receive a heartbeat event, please send a data to the server. The data format is like this:
{
  "e": "heartbeat",
  "d": "pong"
}
  • The connection will be disconnected when the server who sent the heartbeat event twice din't receive any response.

Quick Start

  • add maven dependency
<dependency>
    <groupId>org.onevroad</groupId>
    <artifactId>net-websocket-spring-boot-starter</artifactId>
    <version>0.4.1</version>
</dependency>
  • You have two choices to handle event, add annotation or implement WebSocketEventHandler<Request, Response> or WebSocketCustomizeEventHandler<Request, Response> for every topic that you have.

Add annotation

Add the annotation on the method to handle event.
You can define the topic for every annotation. The priority of topic is: method annotation topic > class annotation topic The supporting params is: topic and data. You need add RequestTopic annotation for the topic and add RequestData annotation for the data.

@WebSocketListener("test-annotation")
public class SampleMessageAnnotationEventHandler {

    @OnSubscribe("test-annotation-subscribe")
    public String onSubscribe(@RequestTopic String topic, @RequestData String data) {
        return "subscribe success!";
    }

    @OnMessage
    public String onMessage(@RequestTopic String topic, @RequestData String data) {
        return "message received!";
    }

    @OnCancel("test-annotation-cancel")
    public String onCancel(@RequestTopic String topic, @RequestData String data) {
        return "cancel success!";
    }
}

Implement interface

For the definite topics, you can implement WebSocketEventHandler<Request, Response> with the WebsocketListener annotation.

@WebsocketListener("test")
public class SampleMessageEventHandler implements WebSocketEventHandler<String, String> {
    @Override
    public String onSubscribe(String topic, String data) {
        return "subscribe success!";
    }

    @Override
    public String onMessage(String topic, String data) {
        return "message received!";
    }

    @Override
    public String onCancel(String topic, String data) {
        return "cancel success!";
    }
}

For the dynamic topics, you can implement WebSocketCustomizeEventHandler<Request, Response> and override the equalsTopic method.

@WebsocketListener
public class SampleMessageCustomizeEventHandler implements WebSocketCustomizeEventHandler<String, String> {
    @Override
    public boolean equalsTopic(String topic) {
        return "test2".equals(topic);
    }

    @Override
    public String onSubscribe(String topic, String data) {
        return "subscribe success!";
    }

    @Override
    public String onMessage(String topic, String data) {
        return "message received!";
    }

    @Override
    public String onCancel(String topic, String data) {
        return "cancel success!";
    }
}
  • configure the scan package where your implementers are.
@EnableAutoConfiguration
@WebSocketScan(basePackages = {"org.net.websocket.samples.handler"})
public class WebSocketApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(WebSocketApplication.class).run(args);
    }
}
  • configure parameter
net:
  websocket:
    # Listening port
    port: 80
    # The number of listening threads, default 1 thread
    boss-group-threads: 1
    # The number of working threads, default 0 is the number of CPU cores
    worker-group-threads: 0
    # Request path
    end-point: /ws
  • send message

You can use the method of the WebSocketMessagePublisher class to send your message.

public class SendMessageHandler {
    public static void send(String topic, String message) {
        WebSocketMessagePublisher.publish(topic, message);
    }
}

Versions

Version
0.4.1
0.3.0
0.1.5