第4章 带你快速入门WebRTC技术

发布时间 2023-12-08 11:27:46作者: 游客0721

4-3 屏幕共享项目-用golang搭建静态资源服务

这段代码是一个使用Go语言编写的简单的HTTP和HTTPS服务器。下面是对代码中涉及的知识点和语法格式的详细解释

package main

import (
	"fmt"
	"net/http"
)
  • package main:这行代码指定了当前文件所属的包是main,表示这是一个可执行程序的入口文件。

  • import:这个关键字用于导入需要使用的包,本例中导入了fmt和net/http包。

func startHttp(port string) {
	fmt.Printf("Start http port: %s\n", port)
	err := http.ListenAndServe(port, nil)
	if err != nil {
		fmt.Println(err)
	}
}
  • func startHttp(port string) { ... }:这是一个函数定义,定义了一个名为startHttp的函数,它接受一个字符串类型的参数port。

  • fmt.Printf("Start http port: %s\n", port):使用Printf函数打印一条启动HTTP端口的消息。

  • http.ListenAndServe(port, nil):使用http.ListenAndServe函数启动一个HTTP服务器,监听指定的端口。第一个参数是要监听的端口,第二个参数是处理HTTP请求的处理器(在本例中使用nil表示使用默认的处理器)。

  • if err != nil { ... }:如果启动HTTP服务器出错,则打印错误信息。

func startHttps(port, cert, key string) {
	fmt.Printf("Start https port: %s\n", port)
	err := http.ListenAndServeTLS(port, cert, key, nil)
	if err != nil {
		fmt.Println(err)
	}
}
  • func startHttps(port, cert, key string) { ... }:这是一个函数定义,定义了一个名为startHttps的函数,它接受三个字符串类型的参数port、cert和key。

  • fmt.Printf("Start https port: %s\n", port):使用Printf函数打印一条启动HTTPS端口的消息。

  • http.ListenAndServeTLS(port, cert, key, nil):使用http.ListenAndServeTLS函数启动一个HTTPS服务器,监听指定的端口,并使用指定的证书文件和私钥文件进行TLS加密通信。第一个参数是要监听的端口,第二个参数是证书文件的路径,第三个参数是私钥文件的路径,第四个参数是处理HTTPS请求的处理器(在本例中使用nil表示使用默认的处理器)。

  • if err != nil { ... }:如果启动HTTPS服务器出错,则打印错误信息。

func main() {
	staticUrl := "/static/"
	fs := http.FileServer(http.Dir("./static"))
	http.Handle(staticUrl, http.StripPrefix(staticUrl, fs))
	go startHttp(":8080")
	startHttps(":8081", "./conf/fullchain.pem", "./conf/privkey.pem")
}
  • func main() { ... }:这是程序的入口函数。

  • staticUrl := "/static/":定义了一个名为staticUrl的字符串变量,它保存了URL的前缀。

  • fs := http.FileServer(http.Dir("./static")):创建了一个文件服务器,使用http.Dir函数指定根目录为"./static"。

  • http.Handle(staticUrl, http.StripPrefix(staticUrl, fs)):将URL前缀与文件服务器绑定,使用http.Handle函数来注册URL和处理器的对应关系。http.StripPrefix函数用于剥去URL前缀,以便正确地定位文件。

  • go startHttp(":8080"):使用go关键字启动一个新的Go协程,异步地启动HTTP服务器并监听端口8080。

  • startHttps(":8081", "./conf/fullchain.pem", "./conf/privkey.pem"):启动HTTPS服务器并监听端口8081,使用"./conf/fullchain.pem"作为证书文件,"./conf/privkey.pem"作为私钥文件。

代码
package main

import (
	"fmt"
	"net/http"
)

func startHttp(port string) {
	fmt.Printf("Start http port: %s\n", port)
	err := http.ListenAndServe(port, nil)
	if err != nil {
		fmt.Println(err)
	}
}

func startHttps(port, cert, key string) {
	fmt.Printf("Start https port: %s\n", port)
	err := http.ListenAndServeTLS(port, cert, key, nil)
	if err != nil {
		fmt.Println(err)
	}
}

func main() {
	staticUrl := "/static/"
	fs := http.FileServer(http.Dir("./static"))
	http.Handle(staticUrl, http.StripPrefix(staticUrl, fs))
	go startHttp(":8080")
	startHttps(":8081", "./conf/fullchain.pem", "./conf/privkey.pem")
}

4-5 屏幕共享项目-设计UI界面

代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>1v1屏幕共享示例</title>

    <style type="text/css">
        video {
            width: 320px;
            height: 240px;
            border: 1px solid black;
        }
        
        button {
            background-color: #d84a38;
            border: none;
            color: white;
            margin: 0 1em 0 0;
            padding: 0.5em 0.7em 0.6em 0.7em;
        }
    </style>
</head>

<body>
    <h3>1v1屏幕共享示例</h3>
    
    <div>
        <video id="localVideo" autoplay controls><
        /video>
        <video id="remoteVideo" autoplay controls></video>
    </div>
    
    <button id="btnStartPush">开始推流</button>
    <button id="btnStopPush">停止推流</button>
    <button id="btnStartPull">开始拉流</button>
    <button id="btnStopPull">停止拉流</button>
    
    <script src="/static/js/adapter.js"></script>
    <script src="/static/js/screen_share.js"></script>
</body>
</html