BLiveDanmaku

package module
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2023 License: MIT Imports: 19 Imported by: 5

README

BLiveDanmaku

Golang编写的B站直播弹幕客户端。使用WebSocket协议(ProtoVer=3)。

使用方法可参考代码库内example下的几个示例。

Documentation

Index

Constants

View Source
const (
	CMD_ANCHOR_LOT_AWARD              = "ANCHOR_LOT_AWARD"
	CMD_ANCHOR_LOT_CHECKSTATUS        = "ANCHOR_LOT_CHECKSTATUS"
	CMD_ANCHOR_LOT_END                = "ANCHOR_LOT_END"
	CMD_ANCHOR_LOT_START              = "ANCHOR_LOT_START"
	CMD_COMBO_SEND                    = "COMBO_SEND"
	CMD_DANMU_MSG                     = "DANMU_MSG"
	CMD_ENTRY_EFFECT                  = "ENTRY_EFFECT"
	CMD_GUARD_BUY                     = "GUARD_BUY"
	CMD_HOT_RANK_CHANGED              = "HOT_RANK_CHANGED"
	CMD_HOT_RANK_SETTLEMENT           = "HOT_RANK_SETTLEMENT"
	CMD_INTERACT_WORD                 = "INTERACT_WORD"
	CMD_LIVE_INTERACTIVE_GAME         = "LIVE_INTERACTIVE_GAME"
	CMD_ONLINE_RANK_COUNT             = "ONLINE_RANK_COUNT"
	CMD_ROOM_REAL_TIME_MESSAGE_UPDATE = "ROOM_REAL_TIME_MESSAGE_UPDATE"
	CMD_SEND_GIFT                     = "SEND_GIFT"
	CMD_STOP_LIVE_ROOM_LIST           = "STOP_LIVE_ROOM_LIST"
	CMD_SUPER_CHAT_MESSAGE            = "SUPER_CHAT_MESSAGE"
	CMD_SUPER_CHAT_MESSAGE_DELETE     = "SUPER_CHAT_MESSAGE_DELETE"
	CMD_SUPER_CHAT_MESSAGE_JPN        = "SUPER_CHAT_MESSAGE_JPN"
	CMD_USER_TOAST_MSG                = "USER_TOAST_MSG"
	CMD_LIVE                          = "LIVE"
	CMD_PREPARING                     = "PREPARING"
)
View Source
const (
	OP_HANDSHAKE       = 0
	OP_HANDSHAKE_REPLY = 1

	OP_HEARTBEAT       = 2
	OP_HEARTBEAT_REPLY = 3

	OP_SEND_MSG       = 4
	OP_SEND_MSG_REPLY = 5

	OP_AUTH       = 7
	OP_AUTH_REPLY = 8

	VER_NORMAL  = 1
	VER_DEFLATE = 2
	VER_BROTLI  = 3

	HEADER_LENGTH = 16

	ROOM_INFO_API    string = `https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom`
	DANMAKU_INFO_API string = `https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo`
	SEND_MSG_API     string = `https://api.live.bilibili.com/msg/send`
	SEND_DM_API      string = `https://api.vc.bilibili.com/web_im/v1/web_im/send_msg`
	UPLOAD_PIC_API   string = `https://api.bilibili.com/x/dynamic/feed/draw/upload_bfs`

	USER_AGENT string = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36`
)

Variables

View Source
var ErrClosed error = errors.New("underlyinh websocket closed")
View Source
var ErrHeartbeatTimeout error = errors.New("heartbeat timeout")
View Source
var ErrMsgIncomplete error = errors.New("msg not complete")
View Source
var ErrMsgUnknownProtocolVersion error = errors.New("msg has an unknown version")
View Source
var ErrNotConnected error = errors.New("not connected")
View Source
var HEARTBEAT_MSG []byte = []byte(`[object Object]`)

Functions

func GetDMDeviceID added in v1.0.4

func GetDMDeviceID() (string, error)

func SendMsg

func SendMsg(msg string, room *RoomInfo, sess_data, jct string) error

func SetLogWriter

func SetLogWriter(out io.Writer)

Types

type Client

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

func Dial

func Dial(bilibili_live_room_id int64, conf *ClientConf) (*Client, error)

func (*Client) Close

func (c *Client) Close()

func (*Client) Room

func (c *Client) Room() *RoomInfo

func (*Client) SendMsg

func (c *Client) SendMsg(msg *RawMessage) error

type ClientConf

type ClientConf struct {
	OpHandlerMap  map[uint32][]OpHandler
	CmdHandlerMap map[string][]CmdHandler

	OnNetError         ErrHandler
	OnServerDisconnect ServerDisconnectHandler

	HeartbeatInterval time.Duration
	HeartbeatTimeout  time.Duration

	HandshakeTimeout time.Duration
}

func (*ClientConf) AddCmdHandler

func (c *ClientConf) AddCmdHandler(cmd string, cb CmdHandler) *ClientConf

func (*ClientConf) AddOpHandler

func (c *ClientConf) AddOpHandler(op uint32, cb OpHandler) *ClientConf

func (*ClientConf) Clone

func (c *ClientConf) Clone() *ClientConf

func (*ClientConf) PrependCmdHandler

func (c *ClientConf) PrependCmdHandler(cmd string, cb CmdHandler) *ClientConf

type CmdHandler

type CmdHandler func(*Client, string, []byte) bool

type DanmakuHost

type DanmakuHost struct {
	Host    string `json:"host"` // Host only contains domain name
	Port    int    `json:"port"`
	WssPort int    `json:"wss_port"`
	WsPort  int    `json:"ws_port"`
}

type DanmakuInfo

type DanmakuInfo struct {
	Token    string        `json:"token"`
	HostList []DanmakuHost `json:"host_list"`
}

func GetDanmakuInfo

func GetDanmakuInfo(room_id int64) (*DanmakuInfo, error)

type ErrHandler

type ErrHandler func(*Client, error)

type NullWriter

type NullWriter struct{}

func (NullWriter) Write

func (w NullWriter) Write(data []byte) (int, error)

type OpHandler

type OpHandler func(*Client, *RawMessage) bool

type RawMessage

type RawMessage struct {
	Ver  int
	Op   uint32
	Seq  uint32
	Data []byte
}

func (*RawMessage) Decode

func (msg *RawMessage) Decode(buf []byte) ([]byte, error)

func (*RawMessage) Encode

func (msg *RawMessage) Encode() []byte

type RoomInfo

type RoomInfo struct {
	Base struct {
		Uid           int64  `json:"uid"`
		RoomID        int64  `json:"room_id"`
		Title         string `json:"title"`
		Cover         string `json:"cover"`
		LiveStatus    int    `json:"live_status"`
		LiveStartTime int64  `json:"live_start_time"`
	} `json:"room_info"`
	Liver struct {
		Base struct {
			Name   string `json:"uname"`
			Icon   string `json:"face"`
			Gender string `json:"gender"`
		} `json:"base_info"`
		Medal struct {
			Name    string `json:"medal_name"`
			Id      int    `json:"medal_id"`
			FanClub int    `json:"fansclub"`
		} `json:"medal_info"`
	} `json:"anchor_info"`
}

func GetRoomInfo

func GetRoomInfo(room_id int64) (*RoomInfo, error)

type SendDirectMsgRsp added in v1.0.3

type SendDirectMsgRsp struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	TTL     int    `json:"ttl"`
	Data    struct {
		KeyHitInfos map[string]interface{} `json:"key_hit_infos"`
		Content     string                 `json:"msg_content"`
		MsgKey      uint64                 `json:"msg_key"`
	} `json:"data"`
}

func SendDirectMsg added in v1.0.3

func SendDirectMsg(sender, reciever int64, content, dev_id, sess_data, jct string) (*SendDirectMsgRsp, error)

func SendDirectMsgPicture added in v1.0.5

func SendDirectMsgPicture(sender, reciever int64, pic *UploadedPic, image_type, dev_id, sess_data, jct string) (*SendDirectMsgRsp, error)

func SendDirectMsgRaw added in v1.0.5

func SendDirectMsgRaw(sender, reciever int64, content []byte, msg_type, dev_id, sess_data, jct string) (*SendDirectMsgRsp, error)

type ServerDisconnectHandler

type ServerDisconnectHandler func(*Client, error)

type UploadedPic added in v1.0.5

type UploadedPic struct {
	Height   int    `json:"image_height"`
	Width    int    `json:"image_width"`
	ImageURL string `json:"image_url"`
}

func UploadPic added in v1.0.5

func UploadPic(pic_data []byte, pic_file, sess_data, jct string) (*UploadedPic, error)

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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