当前位置: 首页>>代码示例>>Golang>>正文


Golang client.New函数代码示例

本文整理汇总了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
}
开发者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


注:本文中的github.com/yosssi/gmq/mqtt/client.New函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。