forked from langhuihui/jessibuca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mse.go
68 lines (61 loc) · 1.61 KB
/
mse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"flag"
"fmt"
"net/http"
"golang.org/x/net/websocket"
)
func main() {
var frontend, backend int
flag.IntVar(&frontend, "l", 8088, "frontend, serve websocket")
flag.IntVar(&backend, "b", 8080, "backend, which fetch flv stream from")
flag.Parse()
fmt.Println(fmt.Sprintf("Transmux http://:%v/* to ws://:%v/*", backend, frontend))
http.Handle("/", websocket.Handler(func(c *websocket.Conn) {
r := c.Request()
url := fmt.Sprintf("http://127.0.0.1:%v%v", backend, r.RequestURI)
fmt.Println("Proxy client", r.RemoteAddr, "to", url)
defer c.Close()
flv, err := http.Get(url)
go func() {
for {
var content string
err := websocket.Message.Receive(c, &content)
if err != nil {
fmt.Println("receive err", err)
flv.Body.Close()
c.Close()
return
}
}
}()
if err != nil {
fmt.Println("Connect backend", url, "failed, err is", err)
return
}
defer flv.Body.Close()
b := make([]byte, 1024)
for {
var n int
if n, err = flv.Body.Read(b); err != nil {
fmt.Println("Recv from backend failed, err is", err)
return
}
if err = websocket.Message.Send(c, b[0:n]); err != nil {
fmt.Println("Send to ws failed, err is", err)
return
}
//fmt.Println("Transmux", n, "bytes")
}
}))
go func() {
err_https :=http.ListenAndServeTLS(fmt.Sprintf(":%v", frontend+1),"cert.crt", "key.pem",nil);
if err_https!=nil{
fmt.Println("https Serve failed, err is", err_https)
}
}()
if err_http := http.ListenAndServe(fmt.Sprintf(":%v", frontend), nil); err_http != nil {
fmt.Println("http Serve failed, err is", err_http)
return
}
}