Change ws URL segment "ws" to "streams"

This commit is contained in:
cnotch
2024-11-24 15:20:26 +08:00
parent 26bbc0826b
commit 5d61e70d77
5 changed files with 23 additions and 13 deletions

View File

@@ -8,12 +8,12 @@
<body>
<div id="sourcesNode"></div>
<div>
<input id="stream_url" value= "ws://localhost:1554/ws/test/live1" size="36">
<input id="stream_url" value= "ws://localhost:1554/streams/test/live1" size="36">
<button id="load_url">load</button>
<button id="unload_url">unload</button>
</div>
<div>
<p style="color:#808080">Enter your ws link to the stream, for example: "ws://localhost:1554/ws/test/live1"</p>
<p style="color:#808080">Enter your ws link to the stream, for example: "ws://localhost:1554/streams/test/live1"</p>
</div>
<video id="test_video" controls autoplay>
<source src="rtsp://placehold" type="application/x-rtsp">

View File

@@ -235,7 +235,7 @@ To activate key, please, use the activation application that is placed:
}
var playerOptions = {
socket: "ws://localhost:8088/ws/",
socket: "ws://localhost:8088/streams/",
redirectNativeMediaErrors : true,
bufferDuration: 30,
errorHandler: errHandler,
@@ -354,7 +354,7 @@ To activate key, please, use the activation application that is placed:
html5Player.src = newSource;
// 修改原例子 begn =======>
// 我们直接使用ws来决定播放的路径
// 比如ws://192.168.1.100:1554/ws/test/live1
// 比如ws://192.168.1.100:1554/streams/test/live1
// 表示要播放服务器上路径为/test/live1
// 如果播放失败,可能是以下情况(1和2发生在升级websocket阶段3发生在rtsp通讯阶段)
// 1. 如果服务器rtsp的验证模式不为NONE则需要登录后获取到token才能访问像这样ws://.../test/live1?token=...
@@ -363,12 +363,13 @@ To activate key, please, use the activation application that is placed:
//
// 如果不是使用例子,我们可以这样
// html5Player.src = "rtsp://placehold"
// playerOptions.socket ="ws://localhost:1554/ws/test/live1"
// playerOptions.socket ="ws://localhost:1554/streams/test/live1"
// 知道ws主机后实际上只需要提供流媒体path即可这也更好
//
let rtspUrl = new URL(newSource)
rtspUrl.protocol = "ws"
rtspUrl.pathname = "/ws"+rtspUrl.pathname
rtspUrl.pathname = "/streams"+rtspUrl.pathname
playerOptions.socket = rtspUrl.href
// <========= end

View File

@@ -215,7 +215,7 @@ http://.../api/v1/streams/rtsp/room/door?token=your_access_token
+ http-flv
http://.../streams/room/door.flv?token=your_access_token
+ websocket-flv
ws://.../ws/room/door.flv?token=your_access_token
ws://.../streams/room/door.flv?token=your_access_token
+ http-flv
http://.../steams/room/door.m3u8?token=your_access_token

View File

@@ -60,7 +60,7 @@ rtsp://localhost:1554/hr/door/video1 请求在服务器内自动变成去拉取r
### 3.3 使用websocket-rtsp
打开demo地址http://localhost:1554/demos/rtsp
输入ws://localhost:1554/ws/group/door 即可访问。
输入ws://localhost:1554/streams/group/door 即可访问。
### 3.4 使用http-flv访问
打开demo地址http://localhost:1554/demos/flv
@@ -72,19 +72,19 @@ rtsp://localhost:1554/hr/door/video1 请求在服务器内自动变成去拉取r
### 3.5 使用 websocket-flv访问
打开demo地址http://localhost:1554/demos/flv
输入ws://locaolhost:1554/ws/group/door.flv 即可访问。
输入ws://locaolhost:1554/streams/group/door.flv 即可访问。
### 3.6 使用 http-hls访问
由于 iOS的Safari不支持上述任何http访问模式请使用 http-hls
在浏览器输入: http://localhost:1554/streams/group/door.m3m8 即可访问。
在浏览器输入: http://localhost:1554/streams/group/door.m3u8 即可访问。
**注意:** 由于http-hls的段文件默认被放在内存中占用大量的内存如系统内存不足请配置存储路径。
### 3.7 访问 h265 flv
打开demo地址http://localhost:1554/demos/flv265
输入http://locaolhost:1554/streams/group/door.flv 即可访问。
输入http://locaolhost:1554/streams/group/door.flv 或 ws://locaolhost:1554/streams/group/door.flv即可访问。
## 4. 需要授权的情况
除rtsp外其他使用token进行访问。

View File

@@ -7,6 +7,7 @@ package service
import (
"net/http"
"path"
"strings"
"github.com/cnotch/ipchub/config"
"github.com/cnotch/ipchub/network/websocket"
@@ -20,7 +21,7 @@ import (
// 初始化流式访问
func (s *Service) initHTTPStreams(mux *http.ServeMux) {
mux.Handle("/ws/", apirouter.WrapHandler(http.HandlerFunc(s.onWebSocketRequest), apirouter.PreInterceptor(s.streamInterceptor)))
// mux.Handle("/ws/", apirouter.WrapHandler(http.HandlerFunc(s.onWebSocketRequest), apirouter.PreInterceptor(s.streamInterceptor)))
mux.Handle("/streams/", apirouter.WrapHandler(http.HandlerFunc(s.onStreamsRequest), apirouter.PreInterceptor(s.streamInterceptor)))
}
@@ -54,8 +55,16 @@ func (s *Service) onWebSocketRequest(w http.ResponseWriter, r *http.Request) {
}
}
// streams 请求处理(flv,mu38,ts)
// streams 请求处理(websocket connect,flv,mu38,ts)
func (s *Service) onStreamsRequest(w http.ResponseWriter, r *http.Request) {
// 检测 websocket 请求
if r.Method == "GET" &&
strings.ToLower(r.Header.Get("Connection")) == "upgrade" &&
strings.ToLower(r.Header.Get("Upgrade")) == "websocket" {
s.onWebSocketRequest(w, r)
return
}
// 获取文件后缀和流路径
streamPath, ext := extractStreamPathAndExt(r.URL.Path)