1 Star 7 Fork 5

鈺术 / 替代传统api的轻量级解决方案websocket-channel

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 4.68 KB
一键复制 编辑 原始数据 按行查看 历史
lishuang 提交于 2021-01-02 18:56 . 更新了readme

替代传统api的轻量级框架websocket-channel

本项目基于springboot,抛弃原有的api接口化约束,全程利用定制化websocket通信,项目基于spring-boot-starter-websocket再封装,适用于实时业务交互,即时游戏等,该框架对其做了非常轻量级的封装。

传统springmvc架构

输入图片说明

Websocket-Channel架构

输入图片说明

项目基于kotlin1.3.61,jdk8

如何使用

引入 starter由于未上传maven中心仓库,请点击链接下载 websocket-channel-spring-boot-starter

         <dependency>
            <groupId>com.jiayou</groupId>
            <artifactId>websocket-channel-spring-boot-starter</artifactId>
            <version>1.2.RELEASE</version>
         </dependency>

日志开关 ,日志会输出连接信息,处理者列表,WebSocket的周边信息

websocket-channel:
  log: true            //日志开关

SpringBoot监听 :webscoket-channel需要扫描IOC容器中带有@SocketBusiness的Bean,配置如下

@SpringBootApplication
class DemoApplication : ApplicationListener<ContextRefreshedEvent> {

    @Bean        //服务端点
    fun serverEndpointExporter() = ServerEndpointExporter()

    @Autowired
    private lateinit var webSocketAutoConfig: WebSocketAutoConfig

    override fun onApplicationEvent(event: ContextRefreshedEvent) {
        event.applicationContext.getBeanNamesForAnnotation(SocketBusiness::class.java).forEach {
            webSocketAutoConfig.scan(event.applicationContext.getBean(it).javaClass)
        }
        WebSocketEndpoint.configurableApplicationContext = event.applicationContext as WebApplicationContext
    }

}

WebSocket端点配置 :端点类需要继承WebSocketEndpoint类,并注入IOC容器,且由@ServerEndpoint("/xx/xx/{id}")注解修饰

@Component
@ServerEndpoint("/server/{id}")
class Socket : WebSocketEndpoint()

所有业务类需要注入IOC容器并且类由@SocketBusiness注解修饰,其方法由@SocketWork注解修饰

@SocketBusiness相当于类级别的@RequestMapping

@SocketWork 相当于方法级别的@RequestMapping

举个例子

data class User(var id: Int?, var name: String?) {
    constructor() : this(null, null)
}
@Component
@SocketBusiness("人员")          //@RequstMapping('/人员')
class SelectService {

    @SocketWork("查询")          //@RequstMapping('/查询')
    fun select(user: User) = user

    @SocketWork("插入")          //@RequstMapping('/插入')
    fun insert(user: User) {
        println("插入数据成功!")
    }
}

对应下来就是:

springmvc:

http://localhost:8080/人员/查询

http://localhost:8080/人员/插入

websocket-channel: 因为是socket通信,我们是用的是json格式的数据进行交互,那么它封装下来是这样的

{
  "id": "8ec52519-6d69-491e-be34-64b7c89f0826",        //前端随机生成的UUID,用于服务端将结果与UUID封装在一起前端判断是哪一个结果
  "from": "userID",                                    //用户的ID,可以用户自定义
  "intent": "人员-查询",                                //这就是我们映射的业务路径
  "parameterType": "Json",                             //暂时只能传Json的数据,后续会更新
   "data":                                             //我们传的参数
   {                                           
    "id": 21,
    "name": "青釭"
   }                                          
}

验证

配置无误的话,你会看到控制台会打印这样一条日志和logo:

输入图片说明 没有报错后我们进行下一步测试:

利用在线工具我们连接websocket-channel: ws://localhost:8080/server/123

连接成功后我们发送一条JSon:

{
  "id": "8ec52519-6d69-491e-be34-64b7c89f0826",
  "from": "123",
  "intent": "人员-查询",
  "parameterType": "Json",
   "data":                                         
   {                                           
    "id": scp12001,
    "name": "Ming's Chen"
   } 
}

服务端就会给你返回交互信息。

意见反馈交流:

输入图片说明

Kotlin
1
https://gitee.com/slientes/websocket-channel.git
git@gitee.com:slientes/websocket-channel.git
slientes
websocket-channel
替代传统api的轻量级解决方案websocket-channel
master

搜索帮助