You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.2 KiB
Go

package room
import (
"movie-sync-server/entities"
"movie-sync-server/utils"
"time"
"github.com/sirupsen/logrus"
"github.com/zishang520/socket.io/v2/socket"
)
func InitEndpoint(client *socket.Socket, cliMsg *entities.ClientMessage) []byte {
roomname := cliMsg.Room
// 加入房间,以注册监听
client.Join(socket.Room(roomname))
// 发送当前房间状态
if r, ok := entities.GetCinema().GetRoom(roomname); ok {
rawMsg, err := utils.StructToMapViaJSON(entities.RoomStateChangedMessage{
ServerTime: time.Now().Unix(),
URL: r.GetUrl(),
UserStatus: r.GetAllUserStatus(),
Playing: r.GetPlaying(),
PlayTime: r.GetPlayTime(),
})
if err != nil {
logrus.WithError(err).Error("marshal error")
return nil
}
client.Emit(entities.MessageTypeRoomInfo, rawMsg)
} else {
rawMsg, err := utils.StructToMapViaJSON(entities.RoomStateChangedMessage{
ServerTime: time.Now().Unix(),
URL: "",
UserStatus: []entities.UserStatus{},
Playing: false,
PlayTime: 0,
})
if err != nil {
logrus.WithError(err).Error("marshal error")
return nil
}
client.Emit(entities.MessageTypeRoomInfo, rawMsg)
}
return nil
}