fix: 解决json无法正常解析的问题
This commit is contained in:
@ -2,8 +2,8 @@ package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.jinshen.cn/remilia/push-server/interval/protocol"
|
||||
"git.jinshen.cn/remilia/push-server/interval/server/ws"
|
||||
@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
type PublishRequest struct {
|
||||
Content string `json:"content"`
|
||||
Payload json.RawMessage `json:"payload"`
|
||||
}
|
||||
|
||||
func PushHandler(hub *ws.Hub) http.HandlerFunc {
|
||||
@ -24,23 +24,25 @@ func PushHandler(hub *ws.Hub) http.HandlerFunc {
|
||||
}
|
||||
|
||||
var req PublishRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
dec := json.NewDecoder(r.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if req.Content == "" {
|
||||
|
||||
if len(req.Payload) == 0 {
|
||||
http.Error(w, "content cannot be empty", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
msg := protocol.BroadcastMessage{
|
||||
Type: protocol.MsgBroadcast,
|
||||
Topic: topic,
|
||||
Payload: json.RawMessage(req.Content),
|
||||
Type: protocol.MsgBroadcast,
|
||||
Topic: topic,
|
||||
Payload: req.Payload,
|
||||
Timestamp: time.Now().Unix(),
|
||||
}
|
||||
|
||||
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