This commit is contained in:
Anbraten 2024-02-16 17:50:49 +01:00
parent 9ace44c6b1
commit 78b94ff85b
3 changed files with 12 additions and 7 deletions

View File

@ -4,8 +4,7 @@
package websocket
import (
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/olahol/melody"
"code.gitea.io/gitea/modules/context"
@ -17,7 +16,7 @@ var m *melody.Melody
type websocketMessage struct {
Action string `json:"action"`
Data string `json:"data"`
Data any `json:"data"`
}
type subscribeMessageData struct {
@ -68,9 +67,10 @@ func handleMessage(s *melody.Session, _msg []byte) {
}
func handleSubscribeMessage(data *sessionData, _data any) error {
msgData, ok := _data.(*subscribeMessageData)
if !ok {
return fmt.Errorf("invalid message data")
msgData := &subscribeMessageData{}
err := mapstructure.Decode(_data, &msgData)
if err != nil {
return err
}
data.onURL = msgData.URL

View File

@ -29,7 +29,7 @@
{{template "base/head_style" .}}
{{template "custom/header" .}}
</head>
<body hx-headers='{"x-csrf-token": "{{.CsrfToken}}"}' hx-swap="outerHTML" hx-ext="morph" hx-push-url="false" hx-ext="ws" ws-connect="/-/ws">
<body hx-headers='{"x-csrf-token": "{{.CsrfToken}}"}' hx-swap="outerHTML" hx-ext="morph,ws" hx-push-url="false" ws-connect="/-/ws">
{{ctx.DataRaceCheck $.Context}}
{{template "custom/body_outer_pre" .}}

View File

@ -20,3 +20,8 @@ document.body.addEventListener('htmx:responseError', (event) => {
// TODO: add translations
showErrorToast(`Error ${event.detail.xhr.status} when calling ${event.detail.requestConfig.path}`);
});
document.body.addEventListener('htmx:wsOpen', (evt) => {
const socket = evt.detail.socketWrapper;
socket.send(JSON.stringify({action: 'subscribe', data: {url: window.location.href}}));
});