本文整理汇总了Golang中github.com/yosssi/gmq/mqtt/client.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
示例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
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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")
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
示例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)
}
}