live

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package live

bilibili 开放平台 cmd 协议 see https://open-live.bilibili.com/document/f9ce25be-312e-1f4a-85fd-fef21f1637f8

Index

Constants

View Source
const (
	// CloseAuthFailed 鉴权失败
	CloseAuthFailed = basic.CloseAuthFailed
	// CloseActively 调用者主动关闭
	CloseActively = basic.CloseActively
	// CloseReadingConnError 读取链接错误
	CloseReadingConnError = basic.CloseReadingConnError
	// CloseReceivedShutdownMessage 收到关闭消息
	CloseReceivedShutdownMessage = basic.CloseReceivedShutdownMessage
	// CloseTypeUnknown 未知原因
	CloseTypeUnknown = basic.CloseTypeUnknown
)
View Source
const (
	CmdLiveOpenPlatformDanmu        = proto.CmdLiveOpenPlatformDanmu        // 弹幕
	CmdLiveOpenPlatformSendGift     = proto.CmdLiveOpenPlatformSendGift     // 礼物
	CmdLiveOpenPlatformSuperChat    = proto.CmdLiveOpenPlatformSuperChat    // SC
	CmdLiveOpenPlatformSuperChatDel = proto.CmdLiveOpenPlatformSuperChatDel // SC删除
	CmdLiveOpenPlatformGuard        = proto.CmdLiveOpenPlatformGuard        // 付费大航海
	CmdLiveOpenPlatformLike         = proto.CmdLiveOpenPlatformLike         // 点赞
)
View Source
const (
	H5QueryTimestamp = "Timestamp"
	H5QueryCode      = "Code"
	H5QueryMid       = "Mid"
	H5QueryCaller    = "Caller"
	H5QueryCodeSign  = "CodeSign"

	H5QueryRoomID  = "RoomId"
	H5QueryPlugEnv = "plug_env"
)
View Source
const (
	AcceptHeader              = "Accept"
	ContentTypeHeader         = "Content-Type"
	AuthorizationHeader       = "Authorization"
	JsonType                  = "application/json"
	BiliVersion               = "1.0"
	HmacSha256                = "HMAC-SHA256"
	BiliTimestampHeader       = "x-bili-timestamp"
	BiliSignatureMethodHeader = "x-bili-signature-method"
	BiliSignatureNonceHeader  = "x-bili-signature-nonce"
	BiliAccessKeyIdHeader     = "x-bili-accesskeyid"
	BiliSignVersionHeader     = "x-bili-signature-version"
	BiliContentMD5Header      = "x-bili-content-md5"
)
View Source
const (
	HostProdLiveOpen = "https://live-open.biliapi.com" // 开放平台 (线上环境)
)

Variables

View Source
var (
	// BilibiliRequestFailed 发生在http请求失败
	BilibiliRequestFailed = errors.BilibiliRequestFailed

	// BilibiliResponseNotSuccess 发生在http请求成功,但响应不是success
	BilibiliResponseNotSuccess = errors.BilibiliResponseNotSuccess

	// BilibiliWebsocketAuthFailed 发生在websocket连接建立后,发送auth请求后,收到的响应不是success
	BilibiliWebsocketAuthFailed = errors.BilibiliWebsocketAuthFailed
)

Functions

func AutomaticParsingMessageCommand

func AutomaticParsingMessageCommand(payload []byte) (string, interface{}, error)

AutomaticParsingMessageCommand 自动解析消息命令 如果是已知的命令,data 会被解析成对应的结构体,否则 data 会被解析成 map[string]interface{} Deprecated: use proto.AutomaticParsingMessageCommand instead

func HmacSHA256

func HmacSHA256(key string, data string) string

func Md5

func Md5(str string) (md5str string)

func ToSortedString

func ToSortedString(payloadMap map[string]string) string

Types

type AnchorInfo

type AnchorInfo struct {
	RoomID int    `json:"room_id"`
	Uname  string `json:"uname"`
	UFace  string `json:"uface"`
	Uid    int    `json:"uid"`
}

type AppBatchHeartbeatRequest

type AppBatchHeartbeatRequest struct {
	GameIDs []string `json:"game_ids"`
}

type AppBatchHeartbeatResponse

type AppBatchHeartbeatResponse struct {
	FailedGameIds []string `json:"failed_game_ids"` // 失败的场次id
}

type AppEndRequest

type AppEndRequest struct {
	// 场次id
	GameID string `json:"game_id"`
	// 项目id
	AppID int64 `json:"app_id"`
}

type AppHeartbeatRequest

type AppHeartbeatRequest struct {
	GameID string `json:"game_id"`
}

type AppStartRequest

type AppStartRequest struct {
	// 主播身份码
	Code string `json:"code"`
	// 项目id
	AppID int64 `json:"app_id"`
}

type AppStartResponse

type AppStartResponse struct {
	// 主播信息
	AnchorInfo AnchorInfo `json:"anchor_info"`
	// 场次信息
	GameInfo GameInfo `json:"game_info"`
	// 长连信息
	WebsocketInfo WebSocketInfo `json:"websocket_info"`
}

func (*AppStartResponse) GetAuthBody added in v0.2.1

func (as *AppStartResponse) GetAuthBody() []byte
func (as *AppStartResponse) GetLinks() []string

type BaseResp

type BaseResp struct {
	Code      int64           `json:"code"`
	Message   string          `json:"message"`
	RequestID string          `json:"request_id"`
	Data      json.RawMessage `json:"data"`
}

func (BaseResp) Success

func (resp BaseResp) Success() bool

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(rCfg *Config) *Client

func (*Client) AppBatchHeartbeat

func (c *Client) AppBatchHeartbeat(gameIDs []string) (*AppBatchHeartbeatResponse, error)

AppBatchHeartbeat 批量心跳

func (*Client) AppEnd

func (c *Client) AppEnd(gameID string) error

AppEnd 关闭app

func (*Client) AppHeartbeat

func (c *Client) AppHeartbeat(gameID string) error

AppHeartbeat 心跳

func (*Client) AppStart

func (c *Client) AppStart(code string) (*AppStartResponse, error)

AppStart 启动app

func (*Client) DoRequest

func (c *Client) DoRequest(reqJSON, reqPath, nonce string) (*BaseResp, error)

DoRequest 发起请求 用于用户自定义请求

func (*Client) StartWebsocket

func (c *Client) StartWebsocket(startResp *AppStartResponse, dispatcherHandleMap map[uint32]DispatcherHandle, onCloseFunc WsClientCloseCallback) (*WsClient, error)

StartWebsocket 启动websocket Deprecated: use basic.StartWebsocket instead 此方法会一键完成鉴权,心跳,消息分发 由于2024年B站决定在开发者平台启用直播间长链功能,所以重新设计了WsClient,并且将其移动到basic包中 这里仅作为兼容性处理,后续版本会废弃

func (*Client) VerifyH5RequestSignature

func (c *Client) VerifyH5RequestSignature(req *http.Request) bool

VerifyH5RequestSignature 验证h5请求签名

func (*Client) VerifyH5RequestSignatureWithParams

func (c *Client) VerifyH5RequestSignatureWithParams(h5sp *H5SignatureParams) bool

VerifyH5RequestSignatureWithParams 验证h5请求签名

type CmdLiveOpenPlatformDanmuData

type CmdLiveOpenPlatformDanmuData proto.CmdDanmuData

type CmdLiveOpenPlatformGuardData

type CmdLiveOpenPlatformGuardData proto.CmdGuardData

type CmdLiveOpenPlatformLikeData

type CmdLiveOpenPlatformLikeData proto.CmdLikeData

type CmdLiveOpenPlatformSendGiftData

type CmdLiveOpenPlatformSendGiftData proto.CmdSendGiftData

type CmdLiveOpenPlatformSuperChatData

type CmdLiveOpenPlatformSuperChatData proto.CmdSuperChatData

type CmdLiveOpenPlatformSuperChatDelData

type CmdLiveOpenPlatformSuperChatDelData proto.CmdSuperChatDelData

type CommonHeader

type CommonHeader struct {
	ContentType       string
	ContentAcceptType string
	Timestamp         string
	SignatureMethod   string
	SignatureVersion  string
	Authorization     string
	Nonce             string
	AccessKeyID       string
	ContentMD5        string
}

func (*CommonHeader) CreateSignature

func (h *CommonHeader) CreateSignature(accessKeySecret string) string

CreateSignature 生成签名

func (*CommonHeader) ToMap

func (h *CommonHeader) ToMap() map[string]string

ToMap 所有字段转map<string, string>

func (*CommonHeader) ToSortMap

func (h *CommonHeader) ToSortMap() map[string]string

ToSortMap 参与签名的字段转map<string, string>

type Config

type Config struct {
	AccessKey            string // access_key
	AccessKeySecret      string // access_key_secret
	OpenPlatformHttpHost string // 开放平台 (线上环境)
	AppID                int64  // 应用id
}

func NewConfig

func NewConfig(accessKey, accessKeySecret string, appID int64) *Config

type DispatcherHandle

type DispatcherHandle func(msg *proto.Message) error

type GameInfo

type GameInfo struct {
	GameID string `json:"game_id"`
}

type H5SignatureParams

type H5SignatureParams struct {
	Timestamp string // 时间戳
	Code      string // 身份码
	Mid       string // 用户id
	Caller    string // 调用方
	CodeSign  string // 签名

	RoomID string // 直播间ID 调用页会携带
	// 场景参数 调用页会携带
	// plug_env=1显示设置项区域的场景,如插件详情页、直播姬内插件配置弹窗;plug_env=0插件实际使用的场景,如插件详情页复制链接、直播姬内使用载入的链接。
	// see https://open-live.bilibili.com/document/ad4901b8-c13e-7a20-e07e-410ad182564a
	PlugEnv string
}

func ParseH5SignatureParamsWithRequest

func ParseH5SignatureParamsWithRequest(req *http.Request) *H5SignatureParams

ParseH5SignatureParamsWithRequest 从http.Request中解析出签名参数

func (H5SignatureParams) CreateSignature

func (h5sp H5SignatureParams) CreateSignature(accessKeySecret string) string

CreateSignature 生成签名

func (H5SignatureParams) ToSortMap

func (h5sp H5SignatureParams) ToSortMap() map[string]string

ToSortMap 参与签名的字段转map<string, string>

func (H5SignatureParams) ValidateSignature

func (h5sp H5SignatureParams) ValidateSignature(accessKeySecret string) bool

ValidateSignature 验证签名

type WebSocketInfo

type WebSocketInfo struct {
	//  长连使用的请求json体 第三方无需关注内容,建立长连时使用即可
	AuthBody string `json:"auth_body"`
	//  wss 长连地址
	WssLink []string `json:"wss_link"`
}

type WsClient

type WsClient struct {
	basic.WsClient
}

func NewWsClient

func NewWsClient(startResp *AppStartResponse, dispatcherHandleMap map[uint32]DispatcherHandle, logger *slog.Logger) *WsClient

NewWsClient 创建一个新的WsClient Deprecated: use basic.NewWsClient instead 由于2024年B站决定在开发者平台启用直播间长链功能,所以重新设计了WsClient,并且将其移动到basic包中 请使用 basic.NewWsClient 替代 这里仅作为兼容性处理,后续版本会废弃

func (*WsClient) WithOnClose

func (wsClient *WsClient) WithOnClose(onClose WsClientCloseCallback) *WsClient

type WsClientCloseCallback added in v0.1.3

type WsClientCloseCallback func(wsClient *WsClient, startResp *AppStartResponse, closeType int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL