feat: 基本广播服务
- 由Hub接收/push/{topic}的请求并解析信息体广播到对应的Client
This commit is contained in:
@ -1,2 +0,0 @@
|
||||
// Package dto contains data transfer objects used in the interval API.
|
||||
package dto
|
||||
@ -1,17 +0,0 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"git.jinshen.cn/remilia/push-server/interval/server/model"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
Topic string `json:"topic"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func MessageFromModel(m model.Message) Message {
|
||||
return Message{
|
||||
Topic: string(m.Topic),
|
||||
Content: string(m.Content),
|
||||
}
|
||||
}
|
||||
@ -1,5 +0,0 @@
|
||||
package dto
|
||||
|
||||
type PublishRequest struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"git.jinshen.cn/remilia/push-server/interval/server/model"
|
||||
)
|
||||
|
||||
type Subscription struct {
|
||||
Topic string `json:"topic"`
|
||||
ClientID string `json:"client_id"`
|
||||
}
|
||||
|
||||
func SubscriptionFromModel(s model.Subscription) Subscription {
|
||||
return Subscription{
|
||||
Topic: string(s.Topic),
|
||||
ClientID: string(s.ClientID),
|
||||
}
|
||||
}
|
||||
@ -2,25 +2,28 @@ package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.jinshen.cn/remilia/push-server/interval/server/api/dto"
|
||||
"git.jinshen.cn/remilia/push-server/interval/server/model"
|
||||
"git.jinshen.cn/remilia/push-server/interval/protocol"
|
||||
"git.jinshen.cn/remilia/push-server/interval/server/ws"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type PublishRequest struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func PushHandler(hub *ws.Hub) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
topicStr := chi.URLParam(r, "topic")
|
||||
topic := model.Topic(topicStr)
|
||||
topic := protocol.Topic(topicStr)
|
||||
if !topic.Valid() {
|
||||
http.Error(w, "invalid topic", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.PublishRequest
|
||||
var req PublishRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
@ -30,12 +33,14 @@ func PushHandler(hub *ws.Hub) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
msg := model.Message{
|
||||
Topic: topic,
|
||||
Content: []byte(req.Content),
|
||||
Timestamp: time.Now().Unix(),
|
||||
msg := protocol.BroadcastMessage{
|
||||
Type: protocol.MsgBroadcast,
|
||||
Topic: topic,
|
||||
Payload: json.RawMessage(req.Content),
|
||||
}
|
||||
|
||||
log.Printf("Received push request for topic %s: %s", topic, req.Content)
|
||||
|
||||
if err := hub.BroadcastMessage(r.Context(), msg); err != nil {
|
||||
http.Error(w, "request cancelled", http.StatusRequestTimeout)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user