# 替代传统api的轻量级解决方案websocket-channel
**Repository Path**: slientes/websocket-channel
## Basic Information
- **Project Name**: 替代传统api的轻量级解决方案websocket-channel
- **Description**: 基于websocket的通讯方案,适用于实时通讯业务,用于渐进的替换传统的api接口。
- **Primary Language**: Kotlin
- **License**: MulanPSL-1.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 7
- **Forks**: 5
- **Created**: 2020-07-18
- **Last Updated**: 2023-06-01
## Categories & Tags
**Categories**: api-gateway
**Tags**: None
## 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](http://jiayou.art/websocket-channel-spring-boot-starter-1.2.RELEASE.jar)
```xml
com.jiayou
websocket-channel-spring-boot-starter
1.2.RELEASE
```
**日志开关** ,日志会输出连接信息,处理者列表,WebSocket的周边信息
```yaml
websocket-channel:
log: true //日志开关
```
**SpringBoot监听** :webscoket-channel需要扫描IOC容器中带有@SocketBusiness的Bean,配置如下
```kotlin
@SpringBootApplication
class DemoApplication : ApplicationListener {
@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}")注解修饰
```kotlin
@Component
@ServerEndpoint("/server/{id}")
class Socket : WebSocketEndpoint()
```
所有业务类需要注入IOC容器并且类由@SocketBusiness注解修饰,其方法由@SocketWork注解修饰
@SocketBusiness相当于类级别的@RequestMapping
@SocketWork 相当于方法级别的@RequestMapping
**举个例子** :
```kotlin
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格式的数据进行交互,那么它封装下来是这样的
```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:
```json
{
"id": "8ec52519-6d69-491e-be34-64b7c89f0826",
"from": "123",
"intent": "人员-查询",
"parameterType": "Json",
"data":
{
"id": scp12001,
"name": "Ming's Chen"
}
}
```
服务端就会给你返回交互信息。
意见反馈交流:
