12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package handler
- import (
- "city_chips/internal/service"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/spf13/viper"
- "time"
- )
- type AccessControlHandler struct {
- *Handler
- accessControlService service.AccessControlService
- conf *viper.Viper
- }
- func NewAccessControlHandler(handler *Handler, accessControlService service.AccessControlService, conf *viper.Viper) *AccessControlHandler {
- return &AccessControlHandler{
- Handler: handler,
- accessControlService: accessControlService,
- conf: conf,
- }
- }
- // GetAccessControl SSE实时推送消息
- func (h *AccessControlHandler) GetAccessControl(c *gin.Context) {
- // 设置响应头
- c.Header("Content-Type", "text/event-stream")
- c.Header("Cache-Control", "no-cache")
- c.Header("Connection", "keep-alive")
- // 监听客户端断开连接
- conn := true
- notify := c.Writer.CloseNotify()
- type Response struct {
- RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
- Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
- Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
- Data any `json:"data"`
- }
- var response Response
- count := 0
- for conn {
- select {
- case <-notify:
- conn = false
- fmt.Println("断开连接")
- return
- default:
- count = count + 1
- sprintf := fmt.Sprintf("循环次数%v", count)
- response.Code = 200
- response.Msg = "查询成功"
- response.Data = sprintf
- res, _ := json.Marshal(&response)
- fmt.Fprintf(c.Writer, "data: %s\n\n", string(res))
- c.Writer.Flush()
- time.Sleep(5 * time.Second)
- }
- getString := h.conf.GetString("lifang.baseUrl.Login")
- h.logger.Info("测试配置文件")
- fmt.Println(getString)
- }
- }
|