GithubHelp home page GithubHelp logo

tnet-chat's Introduction

tnet-chat (项目设计参考go-zero,tcp基于tnet)

测试

启动sever

root@tdev:/home/code/tnet-chat# make dev
go run chat.go -f etc/chat.yaml
TCP服务启动成功...

启动客户单

root@tdev:/home/code/tnet-chat/Client# go run main.go 
{"code":500,"msg":"invalid character 'e' in literal true (expecting 'r')"}
{"code":500,"msg":"密码错误"}
{"code":200,"msg":"登录成功"}

服务端的打印

root@tdev:/home/code/tnet-chat# make dev
go run chat.go -f etc/chat.yaml
TCP服务启动成功...
连接建立成功

2023/02/28 16:51:52 /home/code/tnet-chat/chatmodel/dao/query/user.gen.go:234
[0.948ms] [rows:1] SELECT * FROM `user` WHERE `user`.`name` = 'timzzx' ORDER BY `user`.`id` LIMIT 1
123456

2023/02/28 16:51:52 /home/code/tnet-chat/chatmodel/dao/query/user.gen.go:234
[0.718ms] [rows:1] SELECT * FROM `user` WHERE `user`.`name` = 'timzzx' ORDER BY `user`.`id` LIMIT 1
消息解析失败: EOF

基于cobra完成singleTest tcp命令行客户端

目的

在写tcp长连接的时候,测试需要一个命令行测试工具,所以基于cobra来实现

cobra初始化项目

cd singleTest
// 初始化项目
cobra-cli init
// 生成子命令
cobra-cli init agent

项目目录

├── LICENSE
├── cmd
│   ├── agent.go // 基于这个命令文件来实现需求
│   └── root.go
├── main.go

命令执行

go run main.go agent

实现tcp client

先写接口types/types.go

package types
// 指令接口
type Directives interface {
	Do(agent Agent)
}
// tcp client 发送接口
type Agent interface {
	Send(rid int, data []byte) error
}

增加指令directives/login.go

package directives

import (
	"chat/jsontype"
	"chat/singleTest/types"
	"encoding/json"
	"fmt"
)

type Login struct {
}
// 请求逻辑
func (h *Login) Do(agent types.Agent) {
	// 登录
	req, _ := json.Marshal(jsontype.LoginReq{
		Name:     "timzzx",
		Password: "123456",
	})
	if err := agent.Send(1, req); err != nil {
		fmt.Println("消息发送失败", err)
	}
}

后续只需要增加类似login.go指令文件即可。

增加agent/agent.go

package agent

import (
	"chat/singleTest/directives"
	"chat/singleTest/types"
	"fmt"
	"net"

	"github.com/timzzx/tnet"
)

type agent struct {
	conn       net.Conn
	Directives map[string]types.Directives
}

func NewAgent(conn net.Conn) *agent {
	return &agent{
		conn:       conn,
		Directives: make(map[string]types.Directives),
	}
}

// 发送消息
func (a *agent) Send(rid int, data []byte) error {
	msg, err := tnet.Pack(1, data)
	_, err = a.conn.Write(msg)
	return err
}

// 接收消息(统一消息接收处理)
func (a *agent) Reader() {
	for {
		// 接收消息
		rid, data, err := tnet.Unpack(a.conn)
		if err != nil {
			fmt.Println("消息收回", err)
			a.conn.Close()
			return
		}

		if rid != 0 {
			fmt.Println("Resp:" + string(data))
			fmt.Print("> ")
		}
	}
}

// 停止
func (a *agent) Stop() {
	a.conn.Close()
}

// 初始化指令
func (a *agent) InitDirectives() {
	a.Directives["login"] = &directives.Login{}
}

修改cobra agent文件就是修改Run方法 cmd/agent.go

/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"bufio"
	"chat/singleTest/agent"
	"fmt"
	"net"
	"os"
	"strings"

	"github.com/spf13/cobra"
)

// agentCmd represents the agent command
var agentCmd = &cobra.Command{
	Use:   "agent",
	Short: "A brief description of your command",
	Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
    // cmd接收指令处理 
	Run: func(cmd *cobra.Command, args []string) {
		// 连接服务器
		conn, err := net.Dial("tcp", "192.168.1.13:9999")
		if err != nil {
			fmt.Println("连接失败", err)
			return
		}
		defer conn.Close()

		// 创建agent
		client := agent.NewAgent(conn)
		client.InitDirectives()

		// 监听消息接收
		go client.Reader()

		buf := bufio.NewReader(os.Stdin)
		// 监听输入
		for {
			fmt.Print("> ")
			directives, err := buf.ReadBytes('\n')
			if err != nil {
				fmt.Println("命令操作", err)
				return
			}
			// 去除换行符
			str := strings.Replace(string(directives), "\n", "", -1)
			// 处理指令
			handeler, ok := client.Directives[str]
			if !ok {
				fmt.Println("指令不存在")
			} else {
				handeler.Do(client)
			}

		}
	},
}

func init() {
	rootCmd.AddCommand(agentCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// agentCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// agentCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

测试

先启动tcp server 这个看之前的文档golang tcp服务

// 启动server
make dev 

启动singleTest

cd singleTest
// 启动
go run main.go agent

输入指令

root@tdev:/home/code/tnet-chat/singleTest# go run main.go agent
> login
> Resp:{"code":200,"msg":"登录成功"}
> 

源码

tnet-chat's People

Contributors

timzzx avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.