2024-01-27 14:54:40 +01:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/web"
|
2024-02-03 14:11:05 +01:00
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2024-02-03 11:34:26 +01:00
|
|
|
"code.gitea.io/gitea/services/websocket"
|
2024-02-03 22:13:32 +01:00
|
|
|
|
|
|
|
"github.com/olahol/melody"
|
2024-01-27 14:54:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var m *melody.Melody
|
|
|
|
|
|
|
|
func Init(r *web.Route) {
|
|
|
|
m = melody.New()
|
2024-02-03 10:42:23 +01:00
|
|
|
r.Any("/-/ws", webSocket)
|
2024-02-03 11:34:26 +01:00
|
|
|
m.HandleConnect(websocket.HandleConnect)
|
|
|
|
m.HandleMessage(websocket.HandleMessage)
|
|
|
|
m.HandleDisconnect(websocket.HandleDisconnect)
|
2024-02-03 14:11:05 +01:00
|
|
|
notify_service.RegisterNotifier(websocket.NewNotifier(m))
|
2024-01-27 14:54:40 +01:00
|
|
|
}
|
|
|
|
|
2024-02-03 10:42:23 +01:00
|
|
|
func webSocket(ctx *context.Context) {
|
2024-01-27 14:54:40 +01:00
|
|
|
err := m.HandleRequest(ctx.Resp, ctx.Req)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("HandleRequest", err)
|
|
|
|
}
|
|
|
|
}
|