Ejemplo n.º 1
0
Archivo: main.go Proyecto: KrisLee/link
// This is an echo client demo work with the echo_server.
// usage:
//     go run github.com/funny/examples/echo_client/main.go
func main() {
	protocol := link.NewFixProtocol(4, binary.BigEndian)

	client, err := link.Dial("tcp", "127.0.0.1:10010", protocol)
	if err != nil {
		panic(err)
	}

	client.OnMessage(func(session *link.Session, message []byte) {
		println("message:", string(message))
	})

	client.OnClose(func(session *link.Session) {
		println("closed")
	})

	client.Start()

	for {
		var input string
		if _, err := fmt.Scanf("%s\n", &input); err != nil {
			break
		}
		client.Send(EchoMessage{input})
	}

	client.Close()

	println("bye")
}
Ejemplo n.º 2
0
func (self *Manager) connectMsgServer(ms string) (*link.Session, error) {
	p := link.PacketN(2, link.BigEndianBO, link.LittleEndianBF)
	client, err := link.Dial("tcp", ms, p)
	if err != nil {
		glog.Error(err.Error())
		panic(err)
	}

	return client, err
}
Ejemplo n.º 3
0
func main() {
	flag.Parse()
	cfg, err := LoadConfig(*InputConfFile)
	if err != nil {
		glog.Error(err.Error())
		return
	}

	p := link.PacketN(2, link.BigEndianBO, link.LittleEndianBF)

	gatewayClient, err := link.Dial("tcp", cfg.GatewayServer, p)
	if err != nil {
		panic(err)
	}

	fmt.Println("input id :")
	var input string
	if _, err := fmt.Scanf("%s\n", &input); err != nil {
		glog.Error(err.Error())
	}

	inMsg, err := gatewayClient.Read()
	if err != nil {
		glog.Error(err.Error())
	}
	glog.Info(string(inMsg.Get()))

	gatewayClient.Close(nil)

	msgServerClient, err := link.Dial("tcp", string(inMsg.Get()), p)
	if err != nil {
		panic(err)
	}

	glog.Info("test.. send id...")
	cmd := protocol.NewCmdSimple()

	cmd.CmdName = protocol.SEND_CLIENT_ID_CMD
	cmd.Args = append(cmd.Args, input)

	err = msgServerClient.Send(link.JSON{
		cmd,
	})
	if err != nil {
		glog.Error(err.Error())
	}

	go heartBeat(cfg, msgServerClient)

	glog.Info("test.. send create topic...")

	cmd = protocol.NewCmdSimple()

	cmd.CmdName = protocol.CREATE_TOPIC_CMD

	fmt.Println("input topic name :")
	if _, err = fmt.Scanf("%s\n", &input); err != nil {
		glog.Error(err.Error())
	}

	cmd.Args = append(cmd.Args, input)

	err = msgServerClient.Send(link.JSON{
		cmd,
	})
	if err != nil {
		glog.Error(err.Error())
	}

	glog.Info("test.. send join topic...")

	cmd = protocol.NewCmdSimple()

	cmd.CmdName = protocol.JOIN_TOPIC_CMD

	fmt.Println("input topic name :")
	if _, err = fmt.Scanf("%s\n", &input); err != nil {
		glog.Error(err.Error())
	}

	cmd.Args = append(cmd.Args, input)

	err = msgServerClient.Send(link.JSON{
		cmd,
	})
	if err != nil {
		glog.Error(err.Error())
	}

	glog.Info("test.. send send topic msg...")

	cmd = protocol.NewCmdSimple()

	cmd.CmdName = protocol.SEND_MESSAGE_TOPIC_CMD

	fmt.Println("input topic name :")
	if _, err = fmt.Scanf("%s\n", &input); err != nil {
		glog.Error(err.Error())
	}

	cmd.Args = append(cmd.Args, input)

	fmt.Println("input topic msg :")
	if _, err = fmt.Scanf("%s\n", &input); err != nil {
		glog.Error(err.Error())
	}

	cmd.Args = append(cmd.Args, input)

	err = msgServerClient.Send(link.JSON{
		cmd,
	})
	if err != nil {
		glog.Error(err.Error())
	}

	defer msgServerClient.Close(nil)

	msgServerClient.ReadLoop(func(msg link.InBuffer) {
		glog.Info(string(msg.Get()))
	})

	glog.Flush()
}