Git Product home page Git Product logo

embedded-mqtt-broker's Introduction

embedded-mqtt-broker

go语言版本的mqtt轻量级服务器实现,开发者可以自由的在此基础上进行定制,实现更为复杂的业务功能。主要特性有:

  • 不依赖任何其它中间件,非常轻量;
  • 可以灵活嵌入到其它任何go语言程序中;
  • 完整支持MQTT 3.1.1协议;

使用方式

下载依赖

go get github.com/davidfantasy/embedded-mqtt-broker

代码示例

import mqtt "github.com/davidfantasy/embedded-mqtt-broker"

func main() {
	config := config.NewDefaultConfig()
	broker := mqtt.NewMqttServer(config)
	broker.Startup()
}

配置项

func NewDefaultConfig() *ServerConfig {
	return &ServerConfig{
		//服务监听地址
		Address: "0.0.0.0",
		//服务监听端口
		Port: 1883,
		//默认的会话超时时间,客户端断联超过该时间后,其订阅信息及其它与会话绑定的消息都将被清除
		SessionExpiryInterval: time.Hour * 2,
	}
}

权限控制

现在mqtt broker可以指定接入客户端的访问控制权限,开发者可以自定义一个security.AuthenticationProvider,并根据接入客户端的验证信息返回不同的权限,包括对topic的publis和subcribe的权限。示例代码如下:

import (
	mqtt "github.com/davidfantasy/embedded-mqtt-broker"
	"github.com/davidfantasy/embedded-mqtt-broker/security"
)

type CustomAuthManager struct {
}

func main() {
	config := mqtt.NewServerOptions()
	broker := mqtt.NewMqttServer(config)
	//添加权限管理器
	broker.SetAuthProvider(&CustomAuthManager{})
	broker.Startup()
}

//自定义权限管理器,实现AuthenticationProvider接口
func (manager *CustomAuthManager) Authenticate(username, password string) *security.Authentication {
	if username == "admin" && password == "psw" {
		return security.NewAuthentication([]security.Acl{{Topic: "admin/#", Access: security.CanSubPub}})
	} else if username == "user" && password == "psw" {
		return security.NewAuthentication([]security.Acl{{Topic: "user/#", Access: security.CanSubPub}})
	}
	return nil
}

日志

默认DEBUG,INFO,WARN,ERROR日志都是使用os.Stdout/os.Stderr进行输出,如果需要将日志记录到其它输出流,可为不同级别的logger提供一个自定义的实现,例如:

type NOOPLogger struct{}

//实现日志接口
func (NOOPLogger) Println(v ...interface{})               {}
func (NOOPLogger) Printf(format string, v ...interface{}) {}

func changeLogger() {
	//将debug的logger替换为空输出,这样就不会输出debug日志
	logger.DEBUG = NOOPLogger{}
}

todos

  1. 支持保留消息(RETAIN)
  2. 支持QOS大于0的消息的收发
  3. 性能测试和优化

后续会不断完善相关功能

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.