package controllers

import (
	"errors"
	"spaceofcheng/services/utils"
	"strings"

	"github.com/zjxpcyc/websocket"
)

func (c *BaseController) Ws() {
	grps := c.GetString(":grps")
	id := c.GetString(":id")

	groups := strings.Split(grps, "-")

	clientID := websocket.ClientID{
		ID:     id,
		Groups: groups,
	}

	client, err := websocket.NewClient(
		clientID,
		c.Ctx.ResponseWriter,
		c.Ctx.Request,
		websocket.GHub,
	)

	if err != nil {
		utils.LogError(err)
		c.ResponseError(errors.New("Websocket 连接失败"))
	}

	client.Run()
}
