chat

package module
v0.0.0-...-6ac34ec Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2018 License: MIT Imports: 15 Imported by: 0

README

go-chat

基于go-micro的聊天组件

安装

go get github.com/laoqiu/go-chat

使用

main.go

func main() {
	// chat opts
	redisOpts := redis.Options{}
	regOpts := []registry.Option{}

	// create new web service
	service := web.NewService(
		web.Name("com.laoqiu.web.chat"),
		web.Version("latest"),
		web.Flags(
			cli.StringFlag{
				Name:   "redis_host",
				EnvVar: "REDIS_HOST",
				Usage:  "The redis_host e.g 127.0.0.1:6379",
			},
			cli.StringFlag{
				Name:   "redis_password",
				EnvVar: "REDIS_PASSWORD",
			},
		),
		web.Action(func(c *cli.Context) {
			if len(c.String("redis_host")) > 0 {
				redisOpts.Addr = c.String("redis_host")
			}
			if len(c.String("redis_password")) > 0 {
				redisOpts.Password = c.String("redis_password")
			}
			if len(c.String("registry_address")) > 0 {
				regOpts = append(regOpts, registry.Addrs(c.String("registry_address")))
			}
		}),
	)

	// initialise service
	if err := service.Init(); err != nil {
		log.Fatal(err)
	}

	// chat service
	cs := chat.NewService()
	cs.Init(
		chat.Name("chat"),
		chat.Version("v1"),
		chat.Topic("com.laoqiu.web.chat"),
		chat.RedisOptions(redisOpts),
		chat.RegistryOptions(regOpts...),
	)
	if err := cs.Run(); err != nil {
		log.Fatal(err)
	}

	// register html handler
	// service.Handle("/", http.FileServer(http.Dir("html")))

	// register chat handler
	service.HandleFunc("/stream", cs.NewHandler())

	// run service
	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}

消息格式及登录模式

功能列表

  • 一对一私聊
  • 多项目支持,登录接口自定义
  • 自由消息格式
  • 分布式服务
  • 消息加密
  • 群聊功能
  • 聊天文件(图片、视频)上传接口

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultName    = "go-chat"
	DefaultVersion = "latest"
	DefaultTopic   = "go.micro.web.chat"
)

Functions

func NewService

func NewService() *service

Types

type Broker

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

func NewBroker

func NewBroker(topic string, queue *Queue, opts ...broker.Option) *Broker

func (*Broker) Init

func (b *Broker) Init() error

func (*Broker) Publish

func (b *Broker) Publish(project string, m *Message) error

func (*Broker) Subscribe

func (b *Broker) Subscribe(hub *Hub)

type Client

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

客户端

type Hub

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

Hub maintains the set of active clients and broadcasts messages to the clients.

func NewHub

func NewHub() *Hub

NewHub get hub object

func (*Hub) Clean

func (h *Hub) Clean()

Clean 清理不用的连接

func (*Hub) Run

func (h *Hub) Run()

Run hub.Run

type Message

type Message struct {
	ID string `json:"id"` // 客户端唯一编码
	//ChatType string `json:"chatType"` // 单聊、群聊(singleChat、groupChat)
	//GroupID  string `json:"groupID"`  // 群组(群、公聊、聊天室),ChatType为groupChat时有效
	Type    string `json:"type"`    // 消息类型
	From    string `json:"from"`    // 来自
	To      string `json:"to"`      // 去向
	Content string `json:"content"` // 内容
	Project string `json:"project"` // 项目ID,用于实现多项目通用
}

Message 消息结构体 TODO: 支持群聊

type Option

type Option func(o *Options)

func Name

func Name(n string) Option

Server name

func RedisOptions

func RedisOptions(opts redis.Options) Option

func RegistryOptions

func RegistryOptions(opts ...registry.Option) Option

func Topic

func Topic(v string) Option

func Version

func Version(v string) Option

Version of the service

type Options

type Options struct {
	Name            string
	Version         string
	Topic           string
	RedisOptions    *redis.Options
	RegistryOptions []registry.Option
}

type Queue

type Queue struct {
	ChatKeyPrefix string
	AppKeyPrefix  string
	// contains filtered or unexported fields
}

func (*Queue) GetAppInfo

func (q *Queue) GetAppInfo(key string) (map[string]string, error)

GetAppInfo 查询app信息

func (*Queue) GetKey

func (q *Queue) GetKey(project, dest string) string

func (*Queue) GetUnreadList

func (q *Queue) GetUnreadList(project, dest string) ([]string, error)

GetUnreadList 获得用户所有未读记录

func (*Queue) Remove

func (q *Queue) Remove(project, dest, message string) (int64, error)

Remove 删除一条信息

func (*Queue) Save

func (q *Queue) Save(project, dest, message string) error

Save 保存信息到消息队列

type ValidMessage

type ValidMessage struct {
	ID       string `json:"id"`       // 客户端唯一编码
	APPKey   string `json:"appKey"`   // app key
	Username string `json:"username"` // username
	Token    string `json:"token"`    // token
}

登录验证客户端

Jump to

Keyboard shortcuts

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