doubi4435 2016-10-31 03:04
浏览 394
已采纳

Go-Web服务器在同一端口上侦听多个协议(HTTP和RTMP)

I'm trying to implement the RTMP protocol to along side my web application in Go, however I can't seem to figure out solution to handle both HTTP and and RTMP on the same port.

The idea would be something such as this.

package main

import (
    "fmt"
    "io"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        io.WriteString(w, "Hello!")
    })

    http.HandleFunc("/rtmp", func(w http.ResponseWriter, r *http.Request) {
        // RTMP handling here
    })

    fmt.Println("Starting web server")
    http.ListenAndServe(":8000", nil)
}

zhangpeihao/gortmp has a great RMTP module with an example that shows handling RTMP by listening on a TCP socket. However how can handle it on a specific endpoint rather then a second port?

  • 写回答

1条回答 默认 最新

  • douqin3245 2016-11-06 20:43
    关注

    While wanting to avoid having to convert RTMPT to RTMP, and without having to fork other modules, this was my solution in the end by reading the first byte. Full implementation can be found here.

    func createLocalConnection(port string) *net.TCPConn {
        addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:"+port)
        if err != nil {
            panic(err)
        }
        conn, err := net.DialTCP("tcp", nil, addr)
        if err != nil {
            panic(err)
        }
        return conn
    }
    
    func proxyConnection(conn *net.TCPConn) {
      defer conn.Close()
      data := make([]byte, 1)
      n, err := conn.Read(data)
      if err != nil {
        fmt.Println(err)
        return
      }
    
      var proxyConn *net.TCPConn
      if data[0] == 0x03 { // RTMP first byte.
        proxyConn = createLocalConnection(RTMPPort)
      } else {
        proxyConn = createLocalConnection(HTTPPort)
      }
      proxyConn.Write(data[:n])
      defer proxyConn.Close()
    
      // Request loop
      go func() {
        for {
          data := make([]byte, 1024*1024)
          n, err := conn.Read(data)
          if err != nil {
            break
          }
          proxyConn.Write(data[:n])
        }
      }()
    
      // Response loop
      for {
        data := make([]byte, 1024*1024)
        n, err := proxyConn.Read(data)
        if err != nil {
          break
        }
        conn.Write(data[:n])
      }
    }
    
    func main() {
      listener, err := net.ListenTCP("tcp", addr)
        if err != nil {
            panic(err)
        }
    
      for {
        conn, err := listener.AcceptTCP()
        if err != nil {
          fmt.Println(err)
          continue
        }
    
        go server.ProxyConnection(conn)
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题