Call webhook on player disconnect (#396)
This commit is contained in:
@@ -409,6 +409,9 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if c.connectionNotifier != nil {
|
||||
c.connectionNotifier.NotifyDisconnected(ctx, clientAddr, serverAddress, userInfo, backendHostPort)
|
||||
}
|
||||
c.metrics.ActiveConnections.Set(float64(
|
||||
atomic.AddInt32(&c.activeConnections, -1)))
|
||||
if c.recordLogins && userInfo != nil {
|
||||
|
||||
@@ -16,4 +16,8 @@ type ConnectionNotifier interface {
|
||||
// NotifyConnected is called when the backend connection succeeded.
|
||||
NotifyConnected(ctx context.Context,
|
||||
clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string) error
|
||||
|
||||
// NotifyDisconnected is called when the backend connection terminates.
|
||||
NotifyDisconnected(ctx context.Context,
|
||||
clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string) error
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ type WebhookNotifier struct {
|
||||
|
||||
const (
|
||||
WebhookEventConnecting = "connect"
|
||||
WebhookEventDisconnecting = "disconnect"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -109,6 +110,24 @@ func (w *WebhookNotifier) NotifyConnected(ctx context.Context, clientAddr net.Ad
|
||||
return w.send(ctx, payload)
|
||||
}
|
||||
|
||||
func (w *WebhookNotifier) NotifyDisconnected(ctx context.Context, clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string) error {
|
||||
if w.requireUser && playerInfo == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
payload := &WebhookNotifierPayload{
|
||||
Event: WebhookEventDisconnecting,
|
||||
Timestamp: time.Now(),
|
||||
Status: WebhookStatusSuccess,
|
||||
Client: ClientInfoFromAddr(clientAddr),
|
||||
Server: serverAddress,
|
||||
PlayerInfo: playerInfo,
|
||||
BackendHostPort: backendHostPort,
|
||||
}
|
||||
|
||||
return w.send(ctx, payload)
|
||||
}
|
||||
|
||||
func (w *WebhookNotifier) send(ctx context.Context, payload *WebhookNotifierPayload) error {
|
||||
jsonPayload, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user