Coder Social home page Coder Social logo

end888 / netty-websocket-spring-boot Goto Github PK

View Code? Open in Web Editor NEW

This project forked from niezhiliang/netty-websocket-spring-boot

0.0 0.0 0.0 3.07 MB

♨️基于Netty实现的websocket消息推送,单独发送信息,群发信息

JavaScript 2.08% Java 77.25% HTML 20.67%

netty-websocket-spring-boot's Introduction

netty-websocket-spring-boot

前言

该项目之前是基于spring-boot-starter-websocket做的一个Demo,start还不错, 偶然间看到大佬基于 Netty实现了一个轻量级高性能的Netty-Websocket框架, 而且用法和spring-boot-starter-websocket一样,注解驱动,看到后感触很大, 觉得他很牛逼,看了下他的源码, 大致原理和技术知道了以后,我之前也看过很多spring相关源码,觉得我应该也能写出来,于是就强迫自己重写了该项目,也算是 对源码的巩固,太久不看,确实很多又忘了。

介绍

基于Netty实现了大部分spring-websocket的功能,从下图能知道Spring-Websocket使用起来非常方便,但是性能方面相较于Netty实现的WebScoket性能会有 些差距,但是Netty做Websocket复杂度更高,为了让Netty实现WebSocket使用更简单,于是开发了这个starter,该starter使用起来可以和 spring-websocket一样简单,而且使用语法尽可能的和Spring-Websocket一致。

演示gif

使用

支持事件类型

事件 事件注解 应用场景
握手前 @HandshakeBefore 认证、鉴权
连接成功 @OnOpen --
收到消息 @OnMessage --
连接关闭 @OnClose
异常 @OnError 异常捕获
心跳超时 @OnEvent 剔除失效连接

注解方法支持的参数类型

参数 @HandshakeBefore @OnOpen @OnClose @OnMessage @OnEvent @OnError
HttpHeaders
Session
@PathParam
String(消息内容) × × × × ×
Throwable × × × × ×
Object(事件) × × × × ×

添加maven依赖

<dependency>
    <groupId>com.niezhiliang</groupId>
    <artifactId>netty-websocket-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
@WsServerEndpoint(value = "/websocket/{uid}/{arg}")
public class ServerEndpoint {

    @HandshakeBefore
    public void before (HttpHeaders headers) {
        System.out.println("before");
    }

    /**
     * 用户连接时触发
     * @param session
     */
    @OnOpen
    public void open(Session session, @PathParam (value="uid") String uid, @PathParam String arg){
        System.out.println("open");
        session.sendText("hello client");
    }

    /**
     * 收到信息时触发
     * @param message
     */
    @OnMessage
    public void onMessage(Session session,String message){
        System.out.println("message:" + message);
        session.sendText("server: " + message);
    }

    /**
     * 连接关闭触发
     */
    @OnClose
    public void onClose(){
        System.out.println("close  " + LocalDateTime.now());
    }

    /**
     * 发生错误时触发
     * @param session
     * @param e
     */
    @OnError
    public void onError(Session session, Throwable e) {
        System.out.println("onError");
    }

    /**
     * 发生事件时触发
     * @param session
     * @param evt
     */
    @OnEvent
    public void onEvent(Session session, Object evt) {
        if (evt instanceof IdleStateEvent) {
            // 心跳事件处理
        }
    }
}

收获

  • Netty
  • 自定义注解修饰Bean,并注入容器
  • 扫描并回调自定义注解修饰方法
  • 自动装配Starter
  • 后置处理器SmartInitializingSingleton
  • 适配器模式
  • 方法参数解析
  • 路由匹配、请求路由参数获取

演示gif

netty-websocket-spring-boot's People

Contributors

niezhiliang avatar

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.