简体   繁体   English

通过WebSocket发送div部分时HL流无法正常工作

[英]Hls streaming not working when sending div section through websocket

Hey I am trying to get Hls streaming to work on a site/application I am building using Node.js, javascript, HTML... 嘿,我试图让Hls流在使用Node.js,javascript,HTML构建的网站/应用程序上工作。

I am using this exact code snippet here - 我在这里使用这个确切的代码片段-

<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<video id="video"></video>
<script>
    if(Hls.isSupported()) {
        var video = document.getElementById('video');
        var hls = new Hls();
        hls.loadSource('https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8');
        hls.attachMedia(video);
        hls.on(Hls.Events.MANIFEST_PARSED,function() {
          video.play();
      });
   }
</script>

which works great when I just load it up pasted in a HTML page normally.. 当我正常地将其加载到粘贴在HTML页面中时,该方法非常有用。

however, on my page, I have an HTML page that loads with a blank body section, except for these few lines - 但是,在我的页面上,我有一个HTML页面,其中加载了空白的正文部分,但以下几行除外-

<script type='text/javascript' src="/inc/js/veud/createServerWsConnection.js"></script>
<script src="inc/js/veud/registerWsHandlers.js"></script>
<script>
    var ws = createWebSocket();
    registerHandlers(ws);
</script>
<script src ="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>

which create a websocket connection, and receives a message from the server containing the div section, which is then appended to the end of the body. 会创建一个websocket连接,并从服务器接收包含div部分的消息,然后将其附加到正文的末尾。

When this happens, the hls application does not load, and no streaming video is available, however, if I load up a test HTML page with the exact same HTML, with the div section just already written in the HTML code from the beginning, instead of sending the div section through a websocket and then appending it after the browser page has loaded, then it works perfectly.. 发生这种情况时,hls应用程序将不会加载,并且没有可用的流式视频,但是,如果我加载的HTML网页具有完全相同的测试HTML页面,而div部分从一开始就已经写在HTML代码中,通过网络套接字发送div部分,然后在加载浏览器页面后附加它,那么它可以完美地工作。

even though the HTML code of the page looks the exact same from inspection, the Hls application/ streaming video is not working when the div section containing the first code above is appended to the Html page after receiving from a websocket message.. 即使页面的HTML代码从检查中看起来完全相同,但是当从Websocket消息接收到包含上面第一个代码的div部分后,将Hls应用程序/流视频不起作用时。

could Hls not be activating because it is being added to the Html document after the browser has accessed the site initially? 浏览器最初访问网站后,由于正在将其添加到HTML文档中而导致Hls无法激活吗? do I need to do some sort of redraw/refresh to get it to load? 我需要做某种重绘/刷新才能加载它吗?

I know the Hls is not reloading for some reason, because when Hls works and you inspect the Html, it adds something to the HTML of the site under the video tab where the Hls is loaded it adds a src that wasnt there before so something like 我知道Hls由于某种原因没有重新加载,因为当Hls工作并且您检查Html时,它会在加载Hls的视频标签下向网站的HTML添加一些内容,它会添加一个以前没有的src,例如

<video id="video"></video>

becomes

<video id="video" src="blob:http://172.30.204.207/1d2771a3-f86d-42ed-bec7-794b4a4a4770"></video>

this happens when I put the Hls code in a Test HTML file and load it like that, but when the Hls video object / script section is added to div section that is sent through websocket and appended to page after client requests page, that src="blob: ..." section is not automatically added to the HTML, making me think for some reason the Hls is not loading when appended to page after the client requests the page.. 当我将Hls代码放在Test HTML文件中并像这样加载时会发生这种情况,但是当Hls视频对象/脚本部分添加到通过websocket发送的div部分并在客户端请求页面后附加到页面时,即src = “ blob:...”部分不会自动添加到HTML,由于某些原因,我认为在客户端请求页面后将Hls附加到页面时,HLS不会加载。

any way I can refresh the page to make it work or force it to load the application after the div section is added? 添加div部分后,有什么方法可以刷新页面以使其正常工作或强制其加载应用程序?

kind of stuck here so any help is greatly appreciated. 卡在这里,因此不胜感激。

This doesn't really have anything to do with WebSockets, nor HLS itself. 这实际上与WebSockets无关,也与HLS本身无关。 The issue here is that you're dynamically adding an element which won't be automatically enhanced by the HLS player you're using. 这里的问题是您正在动态添加一个元素,该元素不会被您使用的HLS播放器自动增强。

Untested, but the documentation has an example for enhancing the player explicitly : 未经测试,但是文档中有一个示例可以显式增强播放器

  var video = document.getElementById('video');
  var hls = new Hls();
  // bind them together
  hls.attachMedia(video);
  // MEDIA_ATTACHED event is fired by hls object once MediaSource is ready
  hls.on(Hls.Events.MEDIA_ATTACHED, function () {
        console.log("video and hls.js are now bound together !");
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM