mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2025-02-16 17:23:15 +03:00
add readme and simple code
This commit is contained in:
parent
278efd1524
commit
aae7ee1f5b
@ -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)收到用户订阅之后,的处理过程和与用户之间的响应互动。
|
||||
|
||||
并模拟以下情形:
|
||||
|
||||
+ 主题向用户发送消息
|
||||
+ 用户订阅主题
|
||||
+ 用户发送某个主题消息
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user