Code Monkey home page Code Monkey logo

danmu.go's Introduction

Danmu.go

GoDoc

Go Report Card

Douyu弹幕简单示例

package main

import (
	"fmt"

	"github.com/importcjj/danmu.go/douyu"
)

// 默认弹幕服务器
const (
	DefaultDouyuDanmuHost = "openbarrage.douyutv.com"
	DefaultDouyuDanmuPort = 8601
)

// DanmuHandle 为自定义的弹幕处理
func DanmuHandle(message *douyu.Message) {
	contentType, ok := message.Field("type")
	if !ok {
		return
	}
	switch contentType {
	// 有多种消息类型.
	case douyu.TypeChatMsg:
		// 默认全部为string
		nick, _ := message.Field("nn")
		level, _ := message.Field("level")
		text, _ := message.Field("txt")
		fmt.Printf("<level %s> - %s >>> %s\n", level, nick, text)
	case douyu.TypeUserEnter:
		nick, _ := message.Field("nn")
		level, _ := message.Field("level")
		fmt.Printf("!!!!!欢迎<lv %s> %s 进入房间\n", level, nick)
	}
}

func main() {
	douyuClient := douyu.New()
	douyuClient.Connect(DefaultDouyuDanmuHost, DefaultDouyuDanmuPort)
	douyuClient.JoinRoom(288016)
	douyuClient.HandleFunc(DanmuHandle)
	douyuClient.Watch()
}

注意上述代码中,JoinRoom使用的参数为房间ID,特殊的房间名是不支持的。需要你用浏览器打开该直播间,并使用开发者工具查看主HTML中$ROOM这个对象中定义的room_id字段的值(可使用文本搜索)

示例效果

danmu.go's People

Contributors

importcjj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.