voice chat을 gocommon으로 옮김

This commit is contained in:
2024-01-08 20:22:30 +09:00
parent fdb9830fed
commit 4098812556
2 changed files with 246 additions and 0 deletions

105
voicechat/voice_chat.go Normal file
View File

@ -0,0 +1,105 @@
package core
import (
"context"
"encoding/gob"
"net/http"
"sync/atomic"
"unsafe"
"repositories.action2quare.com/ayo/gocommon"
"repositories.action2quare.com/ayo/gocommon/logger"
)
type joinVoiceChatRequst struct {
Gid string
Mid string
Service string
Alias string
}
type leaveVoiceChatRequst struct {
Gid string
Mid string
Service string
}
func init() {
gob.Register(joinVoiceChatRequst{})
gob.Register(leaveVoiceChatRequst{})
}
type voiceChatConfig struct {
EosClientId string `json:"eos_client_id"`
EosClientSecret string `json:"eos_client_secret"`
EosDeploymentId string `json:"eos_deployment_id"`
}
type voiceServiceImpl interface {
joinVoiceChat(joinVoiceChatRequst) map[string]any
leaveVoiceChat(leaveVoiceChatRequst)
}
var config voiceChatConfig
type VoiceChatService struct {
eosptr unsafe.Pointer
}
func (gv *VoiceChatService) Initialize(ctx context.Context) error {
if err := gocommon.LoadConfig(&config); err != nil {
return logger.ErrorWithCallStack(err)
}
if len(config.EosClientId) == 0 {
logger.Println("eos voice chat is disabled. 'eos_client_id' is empty")
}
if len(config.EosClientSecret) == 0 {
logger.Println("eos voice chat is disabled. 'eos_client_secret' is empty")
}
if len(config.EosDeploymentId) == 0 {
logger.Println("eos voice chat is disabled. 'eos_deployment_id' is empty")
}
gv.eosptr = unsafe.Pointer(&eosauth{})
if len(config.EosClientId) > 0 && len(config.EosClientSecret) > 0 && len(config.EosDeploymentId) > 0 {
go eosTokenRefresh(&gv.eosptr, ctx)
}
return nil
}
func (gv *VoiceChatService) eos() voiceServiceImpl {
ptr := atomic.LoadPointer(&gv.eosptr)
return (*eosauth)(ptr)
}
func (gv *VoiceChatService) JoinVoiceChat(w http.ResponseWriter, r *http.Request) {
var data joinVoiceChatRequst
if err := gocommon.MakeDecoder(r).Decode(&data); err != nil {
logger.Println("JoinVoiceChat failed. DecodeGob returns err :", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
switch data.Service {
case "eos":
result := gv.eos().joinVoiceChat(data)
gocommon.MakeEncoder(w, r).Encode(result)
}
}
func (gv *VoiceChatService) LeaveVoiceChat(w http.ResponseWriter, r *http.Request) {
var data leaveVoiceChatRequst
if err := gocommon.MakeDecoder(r).Decode(&data); err != nil {
logger.Println("JoinVoiceChat failed. DecodeGob returns err :", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
switch data.Service {
case "eos":
gv.eos().leaveVoiceChat(data)
}
}