Skip to content

network ​

go
import "github.com/tangoui/tango/sys/network"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package network reports whether the machine is online, over what kind of link, whether the connection is metered, and when that changes.

st, err := network.Get(a)
if st.Online && !st.Metered { startBigDownload() }
network.OnChange(a, func(st network.Status) { banner.SetVisible(!st.Online) })

Platforms: macOS uses Network.framework's path monitor (nw_path_monitor); Windows the Network List Manager (INetworkListManager connectivity and connection cost) with IP interface change notifications; Linux NetworkManager over the system D-Bus; iOS the same path monitor as macOS; Android ConnectivityManager's default-network callback (Online needs a validated internet connection, Constrained is Data Saver on a metered network; the host manifest declares ACCESS_NETWORK_STATE). "Online" is the system's own judgement (a usable default route, or NetworkManager's connectivity check), not a probe of any particular server.

Functions are safe from any goroutine once the app runs; handlers run on the main thread and only for real changes. OnChange may be called before Run. Get returns ErrUnsupported where the backend cannot tell (minimal backends, Linux without NetworkManager) and tango.ErrNotRunning after the app quit.

常量 ​

go
const (
	Unknown  = platform.NetworkUnknown
	None     = platform.NetworkNone
	Wired    = platform.NetworkWired
	WiFi     = platform.NetworkWiFi
	Cellular = platform.NetworkCellular
	Other    = platform.NetworkOther
)

变量 ​

go
var ErrUnsupported = platform.ErrUnsupported

ErrUnsupported is platform.ErrUnsupported.

函数 ​

func KindString ​

go
func KindString(k Kind) string

KindString names k ("wired", "wifi", "cellular", "none", "other", "unknown"), for logs and the JS API.

func OnChange ​

go
func OnChange(a *tango.App, fn func(Status))

OnChange calls fn whenever connectivity, the link kind or the metered flags change.

类型 ​

Kind ​

go
type Kind = platform.NetworkKind

Kind is the type of link the default route uses.

Status ​

go
type Status = platform.NetworkStatus

Status is a snapshot of connectivity.

func Get ​

go
func Get(a *tango.App) (Status, error)

Get returns the current connectivity.

以 MIT 许可发布