index.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <template>
  2. <div class="srs-webrtc-pull-wrap">
  3. <template v-if="roomNoLive">当前房间没在直播~</template>
  4. <template v-else>
  5. <div class="left">
  6. <div
  7. ref="topRef"
  8. class="head"
  9. >
  10. <div class="info">
  11. <div class="avatar"></div>
  12. <div class="detail">
  13. <div class="top">房间名:{{ roomName }}</div>
  14. <div class="bottom">
  15. <span>你的socketId:{{ getSocketId() }}</span>
  16. </div>
  17. </div>
  18. </div>
  19. </div>
  20. <div class="video-wrap">
  21. <video
  22. id="localVideo"
  23. ref="localVideoRef"
  24. autoplay
  25. webkit-playsinline="true"
  26. playsinline
  27. x-webkit-airplay="allow"
  28. x5-video-player-type="h5"
  29. x5-video-player-fullscreen="true"
  30. x5-video-orientation="portraint"
  31. :muted="appStore.muted"
  32. ></video>
  33. <div class="controls">
  34. <VideoControls></VideoControls>
  35. </div>
  36. </div>
  37. <div
  38. ref="bottomRef"
  39. class="gift"
  40. >
  41. <div
  42. v-for="(item, index) in giftList"
  43. :key="index"
  44. class="item"
  45. >
  46. <div class="ico"></div>
  47. <div class="name">{{ item.name }}</div>
  48. <div class="price">{{ item.price }}</div>
  49. </div>
  50. </div>
  51. </div>
  52. <div class="right">
  53. <div class="tab">
  54. <span>在线用户</span>
  55. <span> | </span>
  56. <span>排行榜</span>
  57. </div>
  58. <div class="user-list">
  59. <div
  60. v-for="(item, index) in liveUserList.filter((item) =>
  61. userStore.userInfo ? item.socketId !== getSocketId() : true
  62. )"
  63. :key="index"
  64. class="item"
  65. >
  66. <div class="info">
  67. <div class="avatar"></div>
  68. <div class="username">{{ item.socketId }}</div>
  69. </div>
  70. </div>
  71. <div
  72. v-if="userStore.userInfo"
  73. class="item"
  74. >
  75. <div class="info">
  76. <img
  77. :src="userStore.userInfo.avatar"
  78. class="avatar"
  79. alt=""
  80. />
  81. <div class="username">{{ userStore.userInfo.username }}</div>
  82. </div>
  83. </div>
  84. </div>
  85. <div class="danmu-list">
  86. <div
  87. v-for="(item, index) in damuList"
  88. :key="index"
  89. class="item"
  90. >
  91. <template v-if="item.msgType === DanmuMsgTypeEnum.danmu">
  92. <span class="name">
  93. {{ item.userInfo?.username || item.socketId }}:
  94. </span>
  95. <span class="msg">{{ item.msg }}</span>
  96. </template>
  97. <template v-else-if="item.msgType === DanmuMsgTypeEnum.otherJoin">
  98. <span class="name system">系统通知:</span>
  99. <span class="msg">
  100. {{ item.userInfo?.username || item.socketId }}进入直播!
  101. </span>
  102. </template>
  103. <template v-else-if="item.msgType === DanmuMsgTypeEnum.userLeaved">
  104. <span class="name system">系统通知:</span>
  105. <span class="msg">
  106. {{ item.userInfo?.username || item.socketId }}离开直播!
  107. </span>
  108. </template>
  109. </div>
  110. </div>
  111. <div class="send-msg">
  112. <textarea
  113. v-model="danmuStr"
  114. class="ipt"
  115. @keydown="keydownDanmu"
  116. ></textarea>
  117. <div
  118. class="btn"
  119. @click="sendDanmu"
  120. >
  121. 发送
  122. </div>
  123. </div>
  124. </div>
  125. </template>
  126. </div>
  127. </template>
  128. <script lang="ts" setup>
  129. import { onMounted, onUnmounted, ref } from 'vue';
  130. import { usePull } from '@/hooks/use-pull';
  131. import { DanmuMsgTypeEnum } from '@/interface';
  132. import { useAppStore } from '@/store/app';
  133. import { useUserStore } from '@/store/user';
  134. const userStore = useUserStore();
  135. const appStore = useAppStore();
  136. const topRef = ref<HTMLDivElement>();
  137. const bottomRef = ref<HTMLDivElement>();
  138. const localVideoRef = ref<HTMLVideoElement>();
  139. const {
  140. initPull,
  141. closeWs,
  142. closeRtc,
  143. getSocketId,
  144. keydownDanmu,
  145. sendDanmu,
  146. roomName,
  147. roomNoLive,
  148. damuList,
  149. giftList,
  150. liveUserList,
  151. danmuStr,
  152. } = usePull({ localVideoRef, isFlv: false, isSRS: true });
  153. onUnmounted(() => {
  154. closeWs();
  155. closeRtc();
  156. });
  157. onMounted(() => {
  158. if (topRef.value && bottomRef.value && localVideoRef.value) {
  159. const res =
  160. bottomRef.value.getBoundingClientRect().top -
  161. (topRef.value.getBoundingClientRect().top +
  162. topRef.value.getBoundingClientRect().height);
  163. localVideoRef.value.style.height = `${res}px`;
  164. }
  165. initPull();
  166. });
  167. </script>
  168. <style lang="scss" scoped>
  169. .srs-webrtc-pull-wrap {
  170. margin: 20px auto 0;
  171. min-width: $large-width;
  172. height: 700px;
  173. text-align: center;
  174. .left {
  175. position: relative;
  176. display: inline-block;
  177. box-sizing: border-box;
  178. width: $large-left-width;
  179. height: 100%;
  180. border-radius: 6px;
  181. background-color: white;
  182. color: #9499a0;
  183. vertical-align: top;
  184. overflow: hidden;
  185. .head {
  186. display: flex;
  187. justify-content: space-between;
  188. padding: 20px;
  189. background-color: papayawhip;
  190. .tag {
  191. display: inline-block;
  192. margin-right: 5px;
  193. padding: 1px 4px;
  194. border: 1px solid;
  195. border-radius: 2px;
  196. color: #9499a0;
  197. font-size: 12px;
  198. }
  199. .info {
  200. display: flex;
  201. align-items: center;
  202. text-align: initial;
  203. .avatar {
  204. margin-right: 20px;
  205. width: 64px;
  206. height: 64px;
  207. border-radius: 50%;
  208. background-color: skyblue;
  209. }
  210. .detail {
  211. .top {
  212. margin-bottom: 10px;
  213. color: #18191c;
  214. }
  215. .bottom {
  216. font-size: 14px;
  217. }
  218. }
  219. }
  220. .other {
  221. display: flex;
  222. flex-direction: column;
  223. justify-content: center;
  224. font-size: 12px;
  225. .top {
  226. display: flex;
  227. align-items: center;
  228. .item {
  229. display: flex;
  230. align-items: center;
  231. margin-right: 20px;
  232. .ico {
  233. display: inline-block;
  234. margin-right: 4px;
  235. width: 10px;
  236. height: 10px;
  237. border-radius: 50%;
  238. background-color: skyblue;
  239. }
  240. }
  241. }
  242. .bottom {
  243. margin-top: 10px;
  244. }
  245. }
  246. }
  247. .video-wrap {
  248. position: relative;
  249. background-color: #18191c;
  250. #localVideo {
  251. max-width: 100%;
  252. max-height: 100%;
  253. }
  254. .controls {
  255. display: none;
  256. }
  257. &:hover {
  258. .controls {
  259. display: block;
  260. }
  261. }
  262. }
  263. .gift {
  264. position: absolute;
  265. right: 0;
  266. bottom: 0;
  267. left: 0;
  268. display: flex;
  269. align-items: center;
  270. justify-content: space-around;
  271. height: 100px;
  272. background-color: papayawhip;
  273. .item {
  274. margin-right: 10px;
  275. text-align: center;
  276. .ico {
  277. width: 50px;
  278. height: 50px;
  279. background-color: skyblue;
  280. }
  281. .name {
  282. color: #18191c;
  283. font-size: 12px;
  284. }
  285. .price {
  286. color: #9499a0;
  287. font-size: 12px;
  288. }
  289. }
  290. }
  291. }
  292. .right {
  293. position: relative;
  294. display: inline-block;
  295. box-sizing: border-box;
  296. margin-left: 10px;
  297. min-width: 300px;
  298. height: 100%;
  299. border-radius: 6px;
  300. background-color: papayawhip;
  301. color: #9499a0;
  302. .tab {
  303. display: flex;
  304. align-items: center;
  305. justify-content: space-evenly;
  306. padding: 5px 0;
  307. font-size: 12px;
  308. }
  309. .user-list {
  310. overflow-y: scroll;
  311. padding: 0 15px;
  312. height: 100px;
  313. background-color: papayawhip;
  314. .item {
  315. display: flex;
  316. align-items: center;
  317. justify-content: space-between;
  318. margin-bottom: 10px;
  319. font-size: 12px;
  320. .info {
  321. display: flex;
  322. align-items: center;
  323. .avatar {
  324. margin-right: 5px;
  325. width: 25px;
  326. height: 25px;
  327. border-radius: 50%;
  328. background-color: skyblue;
  329. }
  330. .username {
  331. color: black;
  332. }
  333. }
  334. }
  335. }
  336. .danmu-list {
  337. overflow-y: scroll;
  338. padding: 0 15px;
  339. height: 450px;
  340. text-align: initial;
  341. .item {
  342. margin-bottom: 10px;
  343. font-size: 12px;
  344. .name {
  345. color: #9499a0;
  346. &.system {
  347. color: red;
  348. }
  349. }
  350. .msg {
  351. color: #61666d;
  352. }
  353. }
  354. }
  355. .send-msg {
  356. position: absolute;
  357. bottom: 15px;
  358. box-sizing: border-box;
  359. padding: 0 10px;
  360. width: 100%;
  361. .ipt {
  362. display: block;
  363. box-sizing: border-box;
  364. margin: 0 auto;
  365. padding: 10px;
  366. width: 100%;
  367. height: 60px;
  368. outline: none;
  369. border: 1px solid hsla(0, 0%, 60%, 0.2);
  370. border-radius: 4px;
  371. background-color: #f1f2f3;
  372. font-size: 14px;
  373. }
  374. .btn {
  375. box-sizing: border-box;
  376. margin-top: 10px;
  377. margin-left: auto;
  378. padding: 5px;
  379. width: 80px;
  380. border-radius: 4px;
  381. background-color: skyblue;
  382. color: white;
  383. text-align: center;
  384. font-size: 12px;
  385. cursor: pointer;
  386. }
  387. }
  388. }
  389. }
  390. // 屏幕宽度小于$large-width的时候
  391. @media screen and (max-width: $large-width) {
  392. .srs-webrtc-pull-wrap {
  393. .left {
  394. width: $medium-left-width;
  395. }
  396. .right {
  397. .list {
  398. .item {
  399. width: 150px;
  400. height: 80px;
  401. }
  402. }
  403. }
  404. }
  405. }
  406. </style>