-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.go
41 lines (34 loc) · 892 Bytes
/
chat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"websocket-htmx/components"
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
)
type HTMXMessage struct {
ChatMessage string `json:"chat_message"`
Headers struct {
HXRequest string `json:"HX-Request"`
HXTrigger string `json:"HX-Trigger"`
HXTriggerName string `json:"HX-Trigger-Name"`
HXTarget string `json:"HX-Target"`
HXCurrentURL string `json:"HX-Current-URL"`
} `json:"HEADERS"`
}
func ChatLoop(ws *websocket.Conn, c echo.Context) {
_, p, err := ws.ReadMessage()
if err != nil {
fmt.Println(err)
}
var msg HTMXMessage
json.Unmarshal(p, &msg)
var buf bytes.Buffer
components.SentAndRecv(msg.ChatMessage, "You're an idiot").Render(context.Background(), &buf)
err = ws.WriteMessage(websocket.TextMessage, buf.Bytes())
if err != nil {
c.Logger().Error(err)
}
}