mirror of
https://github.com/langhuihui/monibuca.git
synced 2025-12-24 13:48:04 +08:00
fix: add rtmp ping timer
This commit is contained in:
@@ -180,6 +180,7 @@ func (c *Client) Run() (err error) {
|
||||
err = c.SendMessage(RTMP_MSG_AMF0_COMMAND, &CommandMessage{"createStream", 2})
|
||||
if err == nil {
|
||||
c.Info("connected")
|
||||
c.OnConnected()
|
||||
}
|
||||
}
|
||||
case *ResponseCreateStreamMessage:
|
||||
|
||||
@@ -50,6 +50,19 @@ type Writers = map[uint32]*struct {
|
||||
*m7s.Publisher
|
||||
}
|
||||
|
||||
type PingTask struct {
|
||||
task.TickTask
|
||||
NetConnection *NetConnection
|
||||
}
|
||||
|
||||
func (t *PingTask) GetTickInterval() time.Duration {
|
||||
return time.Second * 10
|
||||
}
|
||||
|
||||
func (t *PingTask) Tick(any) {
|
||||
t.NetConnection.SendPingRequest()
|
||||
}
|
||||
|
||||
type NetConnection struct {
|
||||
task.Job
|
||||
*util.BufReader
|
||||
@@ -77,7 +90,7 @@ func NewNetConnection(conn net.Conn) (ret *NetConnection) {
|
||||
|
||||
func (nc *NetConnection) Init(conn net.Conn) {
|
||||
nc.Conn = conn
|
||||
nc.BufReader = util.NewBufReaderWithTimeout(conn, 10*time.Second)
|
||||
nc.BufReader = util.NewBufReaderWithTimeout(conn, 30*time.Second)
|
||||
nc.bandwidth = RTMP_MAX_CHUNK_SIZE << 3
|
||||
nc.ReadChunkSize = RTMP_DEFAULT_CHUNK_SIZE
|
||||
nc.WriteChunkSize = RTMP_DEFAULT_CHUNK_SIZE
|
||||
@@ -89,6 +102,12 @@ func (nc *NetConnection) Init(conn net.Conn) {
|
||||
nc.Writers = make(Writers)
|
||||
}
|
||||
|
||||
func (nc *NetConnection) OnConnected() {
|
||||
nc.AddTask(&PingTask{
|
||||
NetConnection: nc,
|
||||
})
|
||||
}
|
||||
|
||||
func (nc *NetConnection) Dispose() {
|
||||
nc.Conn.Close()
|
||||
nc.BufReader.Recycle()
|
||||
@@ -429,7 +448,6 @@ func (nc *NetConnection) SendMessage(t byte, msg RtmpMessage) (err error) {
|
||||
nc.totalWrite += nc.writeSeqNum
|
||||
nc.writeSeqNum = 0
|
||||
err = nc.SendMessage(RTMP_MSG_ACK, Uint32Message(nc.totalWrite))
|
||||
err = nc.SendPingRequest()
|
||||
}
|
||||
for !nc.writing.CompareAndSwap(false, true) {
|
||||
runtime.Gosched()
|
||||
|
||||
Reference in New Issue
Block a user