布罗海姆是一个基于tomcat-websocket-api的二次封装。再原有websocket之上封装了基于心跳检查、断线重连特性。解决了原有websocket网络状况感知不明显的问题。同时也结合SpringBoot的特性封装了spring-boot-starter模块,方便快速集成SpringCloud项目。
在maven依赖中加入依赖项。
<dependencies>
<dependency>
<groupId>org.broheim</groupId>
<artifactId>broheim-websocket-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.broheim</groupId>
<artifactId>broheim-websocket-spring-boot</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
为了找到依赖项还需要配置仓库地址
<repositories>
<repository>
<id>github-broheim-core</id>
<name>github-broheim-core</name>
<url>https://raw.github.com/boyalearn/broheim-repository/core</url>
</repository>
<repository>
<id>github-broheim-starter</id>
<name>github-broheim-starter</name>
<url>https://raw.github.com/boyalearn/broheim-repository/starter</url>
</repository>
</repositories>
编写服务端启动类
@SpringBootApplication
@EnableWebSocketServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CommandReactor commandReactor(){
return new CommandReactor();
}
@Component
@WebSocketController("/ws")
public static class EndpointController {
@Command("hello")
public void doHandle(ChannelContext channelContext, String message) {
System.out.println(message);
}
}
}
这样一个服务端程序就已经开发好了。 当客户端发来一个{"cmd":"hello","body":"hello world"}消息时。服务端被注解@Command("hello")的方法会处理该消息。
编写客户端启动类
public class ClientTest {
public static void main(String[] args) throws InterruptedException {
String url = "ws://gitlab.simple.com:8000/ws";
ClientConfig clientConfig = new ClientConfig();
List<Handler> handlerList=new ArrayList<>();
handlerList.add(new ClientHandler());
clientConfig.setHandlers(handlerList);
new WebSocketClient(url, clientConfig);
}
public static class ClientHandler implements Handler{
@Override
public void handle(ChannelContext channelContext, String message) {
System.out.println("accept message :" +message);
}
}
}
其他语言客户端
负责对消息编码解码,以及协议层消息处理逻辑。
定义业务上的消息分发规则,将不同的消息分发给不同的Handler
Handler与业务相关。将业务上的处理逻辑在handle方法中处理。