本文整理汇总了Golang中github.com/yosssi/gmq/mqtt/client.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: ConnectToMQTTServer
/*
Forward message to MQTT server
*/
func ConnectToMQTTServer(MQTTServerAddress string) (*client.Client, error) {
defer func() {
if err := recover(); err != nil {
utils.Log.Println(err)
debug.PrintStack()
}
}()
cli := client.New(&client.Options{
ErrorHandler: func(err error) {
utils.Log.Println("MQTT Client error:", err)
},
})
var err error
RandomID := utils.MakeRandomID()
err = cli.Connect(&client.ConnectOptions{
Network: "tcp",
Address: MQTTServerAddress,
ClientID: []byte(RandomID),
CleanSession: true,
PINGRESPTimeout: 5 * time.Second,
KeepAlive: 5,
})
if err != nil {
return nil, err
}
return cli, nil
}
开发者ID:amghost,项目名称:message_service,代码行数:36,代码来源:mqtt.go
示例2: newMqttEngine
func newMqttEngine() (*mqttEngine, error) {
murl, err := url.Parse(*mqttURL)
if err != nil {
return nil, err
}
mq := &mqttEngine{}
// Create an MQTT Client.
cli := client.New(&client.Options{
ErrorHandler: mq.handleClientError,
})
mq.murl = murl
mq.cli = cli
mq.attemptConnect()
//mq.publisher = publisher
mq.pollTicker = time.NewTicker(time.Second * 1)
mq.pubTicker = time.NewTicker(time.Second * 15)
go poll(mq)
go publish(mq, "Ready to publish")
return mq, nil
}
开发者ID:paulcull,项目名称:mqtt-webbrick,代码行数:29,代码来源:mqttEngine.go
示例3: Test_newCommandConn_ReadFileErr
func Test_newCommandConn_ReadFileErr(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
defer quit(cli)
if _, err := newCommandConn([]string{"-crt", "not_exist_file.crt"}, cli); err == nil {
notNilErrorExpected(t)
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:11,代码来源:command_conn_test.go
示例4: Test_newCommandConn
func Test_newCommandConn(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
defer quit(cli)
if _, err := newCommandConn([]string{"-crt", filepath.Join("test", "test.crt")}, cli); err != nil {
nilErrorExpected(t, err)
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:11,代码来源:command_conn_test.go
示例5: Test_newCommandPub_errCmdArgsParse
func Test_newCommandPub_errCmdArgsParse(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
defer quit(cli)
if _, err := newCommandPub([]string{"-not-exist-flag"}, cli); err != errCmdArgsParse {
invalidError(t, err, errCmdArgsParse)
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:11,代码来源:command_pub_test.go
示例6: Test_newCommandPub
func Test_newCommandPub(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
defer quit(cli)
if _, err := newCommandPub([]string{"-t", "topicName"}, cli); err != nil {
nilErrorExpected(t, err)
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:11,代码来源:command_pub_test.go
示例7: Test_newCommand_errInvalidCmdName
func Test_newCommand_errInvalidCmdName(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
defer quit(cli)
if _, err := newCommand("invalidCmdName", nil, cli); err != errInvalidCmdName {
invalidError(t, err, errInvalidCmdName)
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:11,代码来源:command_test.go
示例8: Test_newCommandDisconn
func Test_newCommandDisconn(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
defer quit(cli)
if cmd := newCommandDisconn(cli); cmd == nil {
t.Error("cmd => nil, want => not nil")
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:11,代码来源:command_disconn_test.go
示例9: Test_newCommandConn_errParseCrtFailure
func Test_newCommandConn_errParseCrtFailure(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
defer quit(cli)
if _, err := newCommandConn([]string{"-crt", filepath.Join("test", "error.crt")}, cli); err != errParseCrtFailure {
invalidError(t, err, errParseCrtFailure)
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:11,代码来源:command_conn_test.go
示例10: main
func main() {
// Set up channel on which to send signal notifications.
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, os.Kill)
// Print the version of GMQ Client and exit if "v" flag is true.
if *flagV {
printVersion()
exit(0)
return
}
// Create a Client.
cli := client.New(&client.Options{
ErrorHandler: errorHandler,
})
// Quit if signal notifications are sent.
go func() {
<-sigc
quit(cli)
}()
// Create a scanner which reads lines from standard input.
scanner := bufio.NewScanner(stdin)
for printHeader(); scanner.Scan(); printHeader() {
// Get the command name and the command arguments from
// the scanner.
cmdName, cmdArgs := cmdNameArgs(scanner.Text())
// Skip the remaining processes if the command name is zero value.
if cmdName == "" {
continue
}
// Create a command.
cmd, err := newCommand(cmdName, cmdArgs, cli)
if err != nil {
printError(err)
continue
}
// Run the command.
if err := cmd.run(); err != nil {
printError(err)
continue
}
// Print the successfule message.
printSuccess(cmdName)
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:53,代码来源:main.go
示例11: Test_commandDisconn_run
func Test_commandDisconn_run(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
defer quit(cli)
cmd := &commandDisconn{cli: cli}
if err := cmd.run(); err != client.ErrNotYetConnected {
invalidError(t, err, client.ErrNotYetConnected)
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:13,代码来源:command_disconn_test.go
示例12: Test_commandQuit_run
func Test_commandQuit_run(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
cmd := commandQuit{
cli: cli,
}
if err := cmd.run(); err != nil {
nilErrorExpected(t, err)
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:13,代码来源:command_quit_test.go
示例13: Test_commandPub_run
func Test_commandPub_run(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
defer quit(cli)
cmd, err := newCommandPub([]string{"-t", "topicName"}, cli)
if err != nil {
nilErrorExpected(t, err)
}
if err := cmd.run(); err != client.ErrNotYetConnected {
invalidError(t, err, client.ErrNotYetConnected)
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:16,代码来源:command_pub_test.go
示例14: Test_quit
func Test_quit(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
err := cli.Connect(&client.ConnectOptions{
Network: "tcp",
Address: testAddress,
ClientID: []byte("clientID"),
})
if err != nil {
nilErrorExpected(t, err)
}
quit(cli)
}
开发者ID:mezzato,项目名称:gmq,代码行数:16,代码来源:command_quit_test.go
示例15: Test_commandConn_run
func Test_commandConn_run(t *testing.T) {
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
defer quit(cli)
cmd, err := newCommandConn(nil, cli)
if err != nil {
nilErrorExpected(t, err)
return
}
if err := cmd.run(); err == nil {
notNilErrorExpected(t)
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:17,代码来源:command_conn_test.go
示例16: NewChat
func NewChat() (*Chat, error) {
cli := client.New(&client.Options{
ErrorHandler: func(err error) {
log.Println(err)
},
})
chat := Chat{MqttClient: cli, Rooms: make(map[string]*Room)}
u, _ := uuid.NewV4()
err := cli.Connect(&client.ConnectOptions{
Network: "tcp",
Address: getMqttMaster() + ":1883",
ClientID: []byte(u.String()),
})
if err != nil {
return nil, err
}
err = cli.Subscribe(&client.SubscribeOptions{
SubReqs: []*client.SubReq{
&client.SubReq{
TopicFilter: []byte("rooms/#"),
QoS: mqtt.QoS0,
Handler: func(topicName, message []byte) {
s := strings.Split(string(topicName), "/")
if len(s) < 2 {
log.Printf("Unexpected topic: %s", string(topicName))
return
}
room := chat.GetRoom(s[1])
var msg Message
json.Unmarshal(message, &msg)
for e := room.Channels.Front(); e != nil; e = e.Next() {
ch := e.Value.(chan Message)
ch <- msg
}
},
},
},
})
if err != nil {
panic(err)
}
return &chat, nil
}
开发者ID:yuichi1004,项目名称:mqtt-chat,代码行数:45,代码来源:chat.go
示例17: Test_newCommand
func Test_newCommand(t *testing.T) {
cmdNames := []string{
cmdNameConn,
cmdNameDisconn,
cmdNameHelp,
cmdNamePub,
cmdNameQuit,
cmdNameSub,
cmdNameUnsub,
}
cli := client.New(&client.Options{
ErrorHandler: func(_ error) {},
})
defer quit(cli)
for _, cmdName := range cmdNames {
if _, err := newCommand(cmdName, nil, cli); err != nil {
nilErrorExpected(t, err)
}
}
}
开发者ID:mezzato,项目名称:gmq,代码行数:23,代码来源:command_test.go
示例18: main
func main() {
r := gin.New()
r.Use(gin.Recovery())
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
"lastseen": time.Now().String(),
})
})
r.GET("/fgdata", func(c *gin.Context) {
datamux.Lock()
if fgdata != "" && strings.Contains(fgdata, "#") {
s := strings.Split(fgdata, "#")
curdata.Message = "OK"
curdata.Lastseen = lastseen
curdata.Lux = s[1]
curdata.Temp = s[2]
curdata.Lstate = s[0]
}
if light1 != "" {
curdata.Light1 = light1
curdata.Light1ls = light1ls
} else {
curdata.Light1 = "N/A"
curdata.Light1ls = light1ls
}
datamux.Unlock()
if fgdata != "" && strings.Contains(fgdata, "#") {
//s := strings.Split(fgdata, "#")
c.JSON(200, curdata)
// c.JSON(200, gin.H{
// "message": "OK",
// "lastseen": lastseen,
// "lux": s[1],
// "temp": s[2],
// "lstate": s[0],
//})
} else {
c.JSON(200, gin.H{
"message": "No data retreived. Check Network Connection",
})
}
})
r.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/static")
})
r.StaticFS("/static", http.Dir("./web"))
//MQTT
// Create an MQTT Client.
cli := client.New(&client.Options{
// Define the processing of the error handler.
ErrorHandler: func(err error) {
fmt.Println(err)
},
})
// Terminate the Client.
defer cli.Terminate()
defer cli.Disconnect()
// Connect to the MQTT Server.
err := cli.Connect(&client.ConnectOptions{
Network: "tcp",
Address: "192.168.2.37:1883",
ClientID: []byte("example-client"),
})
if err != nil {
fmt.Println("Error on mqtt connect:", err)
os.Exit(1)
}
go mqttWorker(cli)
r.Run() // listen and server on 0.0.0.0:8080
}
开发者ID:gmaisto,项目名称:home-rest-gw,代码行数:86,代码来源:homegw.go
示例19: main
func main() {
flag.Parse()
if flag.NFlag() < 2 {
fmt.Println("Too few arguments")
Usage()
os.Exit(0)
}
if (*message == "") || (*topic == "") {
fmt.Println("Need a topic and message to publish")
Usage()
os.Exit(0)
}
fmt.Println("topic: ", *topic, "\tmessage: ", *message)
// Set up channel on which to send signal notifications.
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, os.Kill)
// Create an MQTT Client.
cli := client.New(&client.Options{
// Define the processing of the error handler.
ErrorHandler: func(err error) {
fmt.Println(err)
},
})
// Terminate the Client.
defer cli.Terminate()
s := []string{*host, *port}
address := strings.Join(s, ":")
fmt.Println("host info: ", address, "QoS option: ", *qos)
var mQoS = mqtt.QoS0
switch *qos {
case "0":
mQoS = mqtt.QoS0
case "1":
mQoS = mqtt.QoS1
case "2":
mQoS = mqtt.QoS2
default:
mQoS = mqtt.QoS0
}
// Connect to the MQTT Server.
err := cli.Connect(&client.ConnectOptions{
Network: "tcp",
Address: address,
ClientID: []byte("example-client"),
// CONNACKTimeout: 30,
// KeepAlive: 50,
})
if err != nil {
panic(err)
}
err = cli.Publish(&client.PublishOptions{
QoS: mQoS,
TopicName: []byte(*topic),
Message: []byte(*message),
})
if err != nil {
panic(err)
}
// Wait for receiving a signal.
// I cannot figure out why publish does not work without this command
// <-sigc
time.Sleep(time.Duration(*delay) * time.Millisecond)
// Disconnect the Network Connection.
if err := cli.Disconnect(); err != nil {
panic(err)
}
}
开发者ID:fmotta,项目名称:golang-mqttpub,代码行数:79,代码来源:main.go
注:本文中的github.com/yosssi/gmq/mqtt/client.New函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论