add readme and simple code

This commit is contained in:
Edward 2020-04-29 22:44:12 +08:00
parent 278efd1524
commit aae7ee1f5b
3 changed files with 12 additions and 14 deletions

View File

@ -9,3 +9,11 @@
![图示关系](../../images/pub-sub-pattern-1.png)
图片来源:[pubsub-pattern.md](https://github.com/imsardine/dev-notes/blob/source/docs/pubsub-pattern.md)
示例演示消息队的某个主题(Topic)收到用户订阅之后,的处理过程和与用户之间的响应互动。
并模拟以下情形:
+ 主题向用户发送消息
+ 用户订阅主题
+ 用户发送某个主题消息

View File

@ -8,8 +8,7 @@ import (
//Message for msg in Message bus
type Message struct {
//Type 类型[code :1,2,3,4]
Type int
Seq int
Text string
From Session //消息来源
}
@ -106,11 +105,6 @@ func (q *Queue) AddTopic(topicName string, topicUserQueueSize int) Topic {
return q.Topics[topicName]
}
//String remove Subscription
func (t *Topic) String() string {
return t.Name
}
func (t *Topic) findUserSubscription(uid uint64, topicName string) (Subscription, bool) {
// Get session or create one if it's the first

View File

@ -8,7 +8,7 @@ import (
////////////////////////////////
//通常意义上是,连接消息队列之后就可以发送消息
////当订阅著之后才会收到相关Topic消息的推送
//当订阅著之后才会收到相关Topic消息的推送
//这里为了简化,直接订阅成功后发送消息,省去连接消息队列发送消息的步骤
////////////////////////////////
func TestMessageSubAndPub(t *testing.T) {
@ -41,13 +41,11 @@ func TestMessageSubAndPub(t *testing.T) {
}()
msg := Message{
//Type 类型[code :1,2,3,4]
Type: 1,
Text: "here is a apple",
From: Session{User{123, "lily"}, time.Now()},
}
subSCription123.Publish(msg)
msg.Type++
msg.Seq++
subSCription123.Publish(msg)
}
@ -71,15 +69,13 @@ func TestMessageSubAndPub(t *testing.T) {
}()
msg := Message{
//Type 类型[code :1,2,3,4]
Type: 1,
Text: "here is a peach",
From: Session{User{123, "bob"}, time.Now()},
}
subSCription456.Publish(msg)
msg.Type++
msg.Seq++
subSCription456.Publish(msg)