khatru/websocket.go
fiatjaf d4334ac2bb revert "a function to send an event directly to a specific connection."
this reverts commit e9c9d0c3a7b6ace94cd40e04d74e4ffd31b6b898 because it is not a good idea and probably useless.
2024-10-16 15:07:55 -03:00

41 lines
739 B
Go

package khatru
import (
"context"
"net/http"
"sync"
"github.com/fasthttp/websocket"
)
type WebSocket struct {
conn *websocket.Conn
mutex sync.Mutex
// original request
Request *http.Request
// this Context will be canceled whenever the connection is closed from the client side or server-side.
Context context.Context
cancel context.CancelFunc
// nip42
Challenge string
AuthedPublicKey string
Authed chan struct{}
authLock sync.Mutex
}
func (ws *WebSocket) WriteJSON(any any) error {
ws.mutex.Lock()
defer ws.mutex.Unlock()
return ws.conn.WriteJSON(any)
}
func (ws *WebSocket) WriteMessage(t int, b []byte) error {
ws.mutex.Lock()
defer ws.mutex.Unlock()
return ws.conn.WriteMessage(t, b)
}