-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.go
59 lines (52 loc) · 1.21 KB
/
send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package nsq
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/nsqio/go-nsq"
)
var producer *nsq.Producer
/*// 主函数
func NsqpSend() {
//读取控制台输入
reader := bufio.NewReader(os.Stdin)
data, _, _ := reader.ReadLine()
command := string(data)
}
*/
// 初始化生产者
/*func init() {
var err error
producer, err = nsq.NewProducer(Addr4150, nsq.NewConfig())
if err != nil {
panic(err)
}
foo := &Foo{}
bar := &Bar{}
reflectinvoker = reflectinvoke.NewReflectinvoker()
reflectinvoker.RegisterMethod(foo)
reflectinvoker.RegisterMethod(bar)
NewConsumer("topic_json","jchan", handleJsonMessage)
NewConsumer("topic_string","schan", handleStringMessage)
}*/
// 发布消息
func publish(topic string, message string) error {
var err error
if producer != nil {
if len(message) == 0 { //不能发布空串,否则会导致error
return nil
}
err = producer.Publish(topic, []byte(message)) // 发布消息
return err
}
return fmt.Errorf("producer is nil", err)
}
func Start(c *gin.Context) {
stringType := c.Query("st")
if stringType == "0" {
message := c.Query("message")
publish("topic_string", message)
} else {
message := c.Query("message")
publish("topic_json", message)
}
}