routes_live.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { Hono } from "hono";
  2. import LiveClient, * as $LiveClient from "@alicloud/live20161101";
  3. import LiveInteractionClient, * as $LiveInteraction from "@alicloud/live-interaction20201214";
  4. import * as $OpenApi from "@alicloud/openapi-client";
  5. import type { Variables, WithAuth } from "./app.tsx";
  6. // 阿里云SDK配置
  7. const liveConfig = new $OpenApi.Config({
  8. accessKeyId: Deno.env.get("ALIYUN_LIVE_ACCESS_KEY_ID") || "",
  9. accessKeySecret: Deno.env.get("ALIYUN_LIVE_ACCESS_KEY_SECRET") || "",
  10. endpoint: "live.aliyuncs.com"
  11. });
  12. const liveInteractionClient = new LiveInteractionClient.default(liveConfig);
  13. const liveClient = new LiveClient.default(liveConfig);
  14. // 创建直播路由
  15. export function createLiveRoutes(withAuth: WithAuth) {
  16. const liveRoutes = new Hono<{ Variables: Variables }>();
  17. // 创建房间
  18. liveRoutes.post("/create-room", withAuth, async (c) => {
  19. const { roomId } = await c.req.json();
  20. try {
  21. // 创建直播间
  22. const createRequest = new $LiveInteraction.CreateRoomRequest({
  23. AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
  24. RoomId: roomId,
  25. Title: `Live Room ${roomId}`,
  26. TemplateId: "standard"
  27. });
  28. const createResponse = await liveInteractionClient.createRoom(createRequest);
  29. // 获取推流/播放地址
  30. const urlRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
  31. AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
  32. RoomId: roomId
  33. });
  34. const urlResponse = await liveClient.describeLiveStreamsOnlineList(urlRequest);
  35. return c.json({
  36. success: true,
  37. roomId,
  38. pushUrl: urlResponse.PushUrl,
  39. playUrl: urlResponse.PlayUrl,
  40. chatToken: createResponse.Token
  41. }, 201);
  42. } catch (error) {
  43. return c.json({
  44. success: false,
  45. error: error instanceof Error ? error.message : "Unknown error"
  46. }, 500);
  47. }
  48. });
  49. // 加入房间
  50. liveRoutes.post("/join-room", withAuth, async (c) => {
  51. const { roomId } = await c.req.json();
  52. try {
  53. // 获取直播间信息
  54. const infoRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
  55. AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
  56. RoomId: roomId
  57. });
  58. const infoResponse = await liveClient.describeLiveStreamsOnlineList(infoRequest);
  59. // 获取用户Token - 使用正确的API方法
  60. const tokenRequest = new $LiveInteraction.CreateTokenRequest({
  61. AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
  62. RoomId: roomId,
  63. UserId: c.get("user")?.id || "anonymous"
  64. });
  65. const tokenResponse = await liveInteractionClient.createToken(tokenRequest);
  66. liveInteractionClient.getLoginToken()
  67. return c.json({
  68. success: true,
  69. playUrl: infoResponse.PlayUrl,
  70. chatToken: tokenResponse.Token
  71. });
  72. } catch (error) {
  73. return c.json({
  74. success: false,
  75. error: error instanceof Error ? error.message : "Unknown error"
  76. }, 500);
  77. }
  78. });
  79. // 获取房间信息
  80. liveRoutes.get("/room-info/:id", withAuth, async (c) => {
  81. const roomId = c.req.param("id");
  82. try {
  83. // 获取直播间信息
  84. const infoRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
  85. AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
  86. RoomId: roomId
  87. });
  88. const infoResponse = await liveClient.describeLiveStreamsOnlineList(infoRequest);
  89. // 获取在线人数
  90. const statsRequest = new $LiveClient.DescribeLiveDomainOnlineUserNumRequest({
  91. AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
  92. RoomId: roomId
  93. });
  94. const statsResponse = await liveClient.describeLiveDomainOnlineUserNum(statsRequest);
  95. return c.json({
  96. success: true,
  97. roomId,
  98. playUrl: infoResponse.PlayUrl,
  99. onlineCount: statsResponse.OnlineCount
  100. });
  101. } catch (error) {
  102. return c.json({
  103. success: false,
  104. error: error instanceof Error ? error.message : "Unknown error"
  105. }, 500);
  106. }
  107. });
  108. // 获取推流地址
  109. liveRoutes.get("/push-url/:roomId", withAuth, async (c) => {
  110. const roomId = c.req.param("roomId");
  111. try {
  112. const urlRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
  113. AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
  114. RoomId: roomId
  115. });
  116. const urlResponse = await liveClient.describeLiveStreamsOnlineList(urlRequest);
  117. return c.json({
  118. success: true,
  119. pushUrl: urlResponse.PushUrl
  120. });
  121. } catch (error) {
  122. return c.json({
  123. success: false,
  124. error: error instanceof Error ? error.message : "Unknown error"
  125. }, 500);
  126. }
  127. });
  128. // 获取拉流地址
  129. liveRoutes.get("/pull-url/:roomId", withAuth, async (c) => {
  130. const roomId = c.req.param("roomId");
  131. try {
  132. const urlRequest = new $LiveClient.DescribeLiveStreamsOnlineListRequest({
  133. AppId: Deno.env.get("ALIYUN_CHAT_APP_ID") || "",
  134. RoomId: roomId
  135. });
  136. const urlResponse = await liveClient.describeLiveStreamsOnlineList(urlRequest);
  137. return c.json({
  138. success: true,
  139. pullUrl: urlResponse.PlayUrl
  140. });
  141. } catch (error) {
  142. return c.json({
  143. success: false,
  144. error: error instanceof Error ? error.message : "Unknown error"
  145. }, 500);
  146. }
  147. });
  148. return liveRoutes;
  149. }