| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- import LiveClient, * as $LiveClient from "@alicloud/live20161101";
- import LiveInteractionClient, * as $LiveInteraction from "@alicloud/live-interaction20201214";
- import * as $OpenApi from "@alicloud/openapi-client";
- interface RoomOptions {
- title?: string;
- templateId?: string;
- maxUsers?: number;
- }
- interface UserInfo {
- id: string;
- name?: string;
- avatar?: string;
- isAdmin?: boolean;
- }
- interface Message {
- id: string;
- sender: UserInfo;
- content: string;
- timestamp: Date;
- }
- export class RoomManagementSystem {
- private liveClient: LiveClient.default;
- private liveInteractionClient: LiveInteractionClient.default;
- private appId: string;
- constructor() {
- const config = new $OpenApi.Config({
- accessKeyId: Deno.env.get("ALIYUN_LIVE_ACCESS_KEY_ID") || "",
- accessKeySecret: Deno.env.get("ALIYUN_LIVE_ACCESS_KEY_SECRET") || "",
- endpoint: "live.aliyuncs.com"
- });
-
- this.liveClient = new LiveClient.default(config);
- this.liveInteractionClient = new LiveInteractionClient.default(config);
- this.appId = Deno.env.get("ALIYUN_CHAT_APP_ID") || "";
- }
- /**
- * 创建房间
- * @param roomId 房间ID
- * @param options 房间选项
- * @returns 房间信息
- */
- async createRoom(roomId: string, options: RoomOptions = {}): Promise<{
- roomId: string;
- pushUrl: string;
- playUrl: string;
- chatToken: string;
- }> {
- try {
- // 创建互动直播房间
- const createRequest = new $LiveInteraction.CreateRoomRequest({
- AppId: this.appId,
- RoomId: roomId,
- Title: options.title || `Room ${roomId}`,
- TemplateId: options.templateId || "standard"
- });
- const createResponse = await this.liveInteractionClient.createRoom(createRequest);
- // 获取推流/播放地址
- const urlRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
- AppId: this.appId,
- RoomId: roomId
- });
- const urlResponse = await this.liveClient.describeLiveStreamsOnlineList(urlRequest);
- return {
- roomId,
- pushUrl: urlResponse.PushUrl,
- playUrl: urlResponse.PlayUrl,
- chatToken: createResponse.Token
- };
- } catch (error) {
- throw new Error(`Failed to create room: ${error instanceof Error ? error.message : "Unknown error"}`);
- }
- }
- /**
- * 销毁房间
- * @param roomId 房间ID
- */
- async destroyRoom(roomId: string): Promise<void> {
- try {
- const request = new $LiveInteraction.DestroyRoomRequest({
- AppId: this.appId,
- RoomId: roomId
- });
- await this.liveInteractionClient.destroyRoom(request);
- } catch (error) {
- throw new Error(`Failed to destroy room: ${error instanceof Error ? error.message : "Unknown error"}`);
- }
- }
- /**
- * 用户加入房间
- * @param roomId 房间ID
- * @param user 用户信息
- * @returns 用户Token和播放地址
- */
- async joinRoom(roomId: string, user: UserInfo) {
- try {
- const addMemberRequest = new $LiveInteraction.AddGroupMembersRequest({
- appId:this.appId,
- requestParams: {
- groupId: roomId,
- members: [user.id]
- }
- })
- await this.liveInteractionClient.addGroupMembers(addMemberRequest) ;
- } catch (error) {
- throw new Error(`Failed to join room: ${error instanceof Error ? error.message : "Unknown error"}`);
- }
- }
- /**
- * 用户离开房间
- * @param roomId 房间ID
- * @param userId 用户ID
- */
- async leaveRoom(roomId: string, userId: string): Promise<void> {
- try {
- const request = new $LiveInteraction.RemoveGroupMembersRequest({
- AppId: this.appId,
- RoomId: roomId,
- UserId: userId
- });
- await this.liveInteractionClient.leaveRoom(request);
- } catch (error) {
- throw new Error(`Failed to leave room: ${error instanceof Error ? error.message : "Unknown error"}`);
- }
- }
- /**
- * 发送消息
- * @param roomId 房间ID
- * @param sender 发送者信息
- * @param content 消息内容
- * @returns 消息ID
- */
- async sendMessage(roomId: string, sender: UserInfo, content: string): Promise<string> {
- try {
- const request = new $LiveInteraction.SendMessageRequest({
- AppId: this.appId,
- RoomId: roomId,
- SenderId: sender.id,
- Content: content
- });
- const response = await this.liveInteractionClient.sendMessage(request);
- return response.MessageId;
- } catch (error) {
- throw new Error(`Failed to send message: ${error instanceof Error ? error.message : "Unknown error"}`);
- }
- }
- /**
- * 获取历史消息
- * @param roomId 房间ID
- * @param limit 消息数量限制
- * @returns 消息列表
- */
- async getHistoryMessages(roomId: string, limit: number = 50): Promise<Message[]> {
- try {
- const request = new $LiveInteraction.GetHistoryMessagesRequest({
- AppId: this.appId,
- RoomId: roomId,
- Limit: limit
- });
- const response = await this.liveInteractionClient.getHistoryMessages(request);
-
- return response.Messages.map((msg: any) => ({
- id: msg.MessageId,
- sender: {
- id: msg.SenderId,
- name: msg.SenderName,
- avatar: msg.SenderAvatar
- },
- content: msg.Content,
- timestamp: new Date(msg.Timestamp)
- }));
- } catch (error) {
- throw new Error(`Failed to get history messages: ${error instanceof Error ? error.message : "Unknown error"}`);
- }
- }
- /**
- * 禁言用户
- * @param roomId 房间ID
- * @param userId 用户ID
- * @param duration 禁言时长(秒)
- */
- async muteUser(roomId: string, userId: string, duration: number = 3600): Promise<void> {
- try {
- const request = new $LiveInteraction.MuteUserRequest({
- AppId: this.appId,
- RoomId: roomId,
- UserId: userId,
- Duration: duration
- });
- await this.liveInteractionClient.muteUser(request);
- } catch (error) {
- throw new Error(`Failed to mute user: ${error instanceof Error ? error.message : "Unknown error"}`);
- }
- }
- /**
- * 踢出用户
- * @param roomId 房间ID
- * @param userId 用户ID
- */
- async kickUser(roomId: string, userId: string): Promise<void> {
- try {
- const request = new $LiveInteraction.KickUserRequest({
- AppId: this.appId,
- RoomId: roomId,
- UserId: userId
- });
- await this.liveInteractionClient.kickUser(request);
- } catch (error) {
- throw new Error(`Failed to kick user: ${error instanceof Error ? error.message : "Unknown error"}`);
- }
- }
- /**
- * 获取房间信息
- * @param roomId 房间ID
- * @returns 房间信息
- */
- async getRoomInfo(roomId: string): Promise<{
- roomId: string;
- playUrl: string;
- onlineCount: number;
- }> {
- try {
- // 获取直播间信息
- const infoRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
- AppId: this.appId,
- RoomId: roomId
- });
- const infoResponse = await this.liveClient.describeLiveStreamsOnlineList(infoRequest);
- // 获取在线人数
- const statsRequest = new $LiveClient.DescribeLiveDomainOnlineUserNumRequest({
- AppId: this.appId,
- RoomId: roomId
- });
- const statsResponse = await this.liveClient.describeLiveDomainOnlineUserNum(statsRequest);
- return {
- roomId,
- playUrl: infoResponse.PlayUrl,
- onlineCount: statsResponse.OnlineCount
- };
- } catch (error) {
- throw new Error(`Failed to get room info: ${error instanceof Error ? error.message : "Unknown error"}`);
- }
- }
- }
|