| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import { Hono } from "hono";
- import LiveClient, * as $LiveClient from "@alicloud/live20161101";
- import LiveInteractionClient, * as $LiveInteraction from "@alicloud/live-interaction20201214";
- import * as $OpenApi from "@alicloud/openapi-client";
- import type { Variables, WithAuth } from "./app.tsx";
- // 阿里云SDK配置
- const liveConfig = 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"
- });
- const liveInteractionClient = new LiveInteractionClient.default(liveConfig);
- const liveClient = new LiveClient.default(liveConfig);
- // 创建直播路由
- export function createLiveRoutes(withAuth: WithAuth) {
- const liveRoutes = new Hono<{ Variables: Variables }>();
- // 创建房间
- liveRoutes.post("/create-room", withAuth, async (c) => {
- const { roomId } = await c.req.json();
-
- try {
- // 创建直播间
- const createRequest = new $LiveInteraction.CreateRoomRequest({
- AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
- RoomId: roomId,
- Title: `Live Room ${roomId}`,
- TemplateId: "standard"
- });
- const createResponse = await liveInteractionClient.createRoom(createRequest);
- // 获取推流/播放地址
- const urlRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
- AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
- RoomId: roomId
- });
- const urlResponse = await liveClient.describeLiveStreamsOnlineList(urlRequest);
- return c.json({
- success: true,
- roomId,
- pushUrl: urlResponse.PushUrl,
- playUrl: urlResponse.PlayUrl,
- chatToken: createResponse.Token
- }, 201);
- } catch (error) {
- return c.json({
- success: false,
- error: error instanceof Error ? error.message : "Unknown error"
- }, 500);
- }
- });
- // 加入房间
- liveRoutes.post("/join-room", withAuth, async (c) => {
- const { roomId } = await c.req.json();
-
- try {
- // 获取直播间信息
- const infoRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
- AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
- RoomId: roomId
- });
- const infoResponse = await liveClient.describeLiveStreamsOnlineList(infoRequest);
- // 获取用户Token - 使用正确的API方法
- const tokenRequest = new $LiveInteraction.CreateTokenRequest({
- AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
- RoomId: roomId,
- UserId: c.get("user")?.id || "anonymous"
- });
- const tokenResponse = await liveInteractionClient.createToken(tokenRequest);
- liveInteractionClient.getLoginToken()
- return c.json({
- success: true,
- playUrl: infoResponse.PlayUrl,
- chatToken: tokenResponse.Token
- });
- } catch (error) {
- return c.json({
- success: false,
- error: error instanceof Error ? error.message : "Unknown error"
- }, 500);
- }
- });
- // 获取房间信息
- liveRoutes.get("/room-info/:id", withAuth, async (c) => {
- const roomId = c.req.param("id");
-
- try {
- // 获取直播间信息
- const infoRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
- AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
- RoomId: roomId
- });
- const infoResponse = await liveClient.describeLiveStreamsOnlineList(infoRequest);
- // 获取在线人数
- const statsRequest = new $LiveClient.DescribeLiveDomainOnlineUserNumRequest({
- AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
- RoomId: roomId
- });
- const statsResponse = await liveClient.describeLiveDomainOnlineUserNum(statsRequest);
- return c.json({
- success: true,
- roomId,
- playUrl: infoResponse.PlayUrl,
- onlineCount: statsResponse.OnlineCount
- });
- } catch (error) {
- return c.json({
- success: false,
- error: error instanceof Error ? error.message : "Unknown error"
- }, 500);
- }
- });
- // 获取推流地址
- liveRoutes.get("/push-url/:roomId", withAuth, async (c) => {
- const roomId = c.req.param("roomId");
-
- try {
- const urlRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
- AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
- RoomId: roomId
- });
- const urlResponse = await liveClient.describeLiveStreamsOnlineList(urlRequest);
- return c.json({
- success: true,
- pushUrl: urlResponse.PushUrl
- });
- } catch (error) {
- return c.json({
- success: false,
- error: error instanceof Error ? error.message : "Unknown error"
- }, 500);
- }
- });
- // 获取拉流地址
- liveRoutes.get("/pull-url/:roomId", withAuth, async (c) => {
- const roomId = c.req.param("roomId");
-
- try {
- const urlRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
- AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
- RoomId: roomId
- });
- const urlResponse = await liveClient.describeLiveStreamsOnlineList(urlRequest);
- return c.json({
- success: true,
- pullUrl: urlResponse.PlayUrl
- });
- } catch (error) {
- return c.json({
- success: false,
- error: error instanceof Error ? error.message : "Unknown error"
- }, 500);
- }
- });
- return liveRoutes;
- }
|