Skip to content

04 Zinx Connection

刘丹冰 edited this page May 11, 2023 · 2 revisions

Case Source Code : https://github.com/aceld/zinx-usage/tree/main/zinx_connection

1. IConnection Interface

The interface provided by Connection is as follows:

// Define the connection interface
type IConnection interface {
	Start()                   // Start the connection and make the current connection work
	Stop()                    // Stop the connection and end the current connection state
	Context() context.Context // Return ctx for user-defined goroutines to obtain connection exit status

	GetConnection() net.Conn // Get the original socket TCPConn from the current connection
	GetConnID() uint32       // Get the current connection ID
	RemoteAddr() net.Addr    // Get the remote address information of the link
	LocalAddr() net.Addr     // Get the local address information of the link

	SendMsg(msgID uint32, data []byte) error     // Send the Message data directly to the remote TCP client (no buffer)
	SendBuffMsg(msgID uint32, data []byte) error // Send the Message data directly to the remote TCP client (with buffer)

	SetProperty(key string, value interface{})   // Set the property of the link
	GetProperty(key string) (interface{}, error) // Get the property of the link
	RemoveProperty(key string)                   // Remove the property of the link
	IsAlive() bool                               // Determine whether the current connection is alive
}

2. Set the attribute parameters carried by the link

During the link process, if you need to set some attribute and parameter information carried by the current conn to facilitate business differentiation, etc., you can use the following method to set:

// Set the property parameter carried by the current IConnection
conn.SetProperty("Name", "aceld")
conn.SetProperty("Home", "https://www.yuque.com/aceld")

Get it through the following method:

// Get the attribute parameters carried by the link
name, _ := conn.GetProperty("Name")
home, _ := conn.GetProperty("Home")

Here is an example of setting properties using Client:

package main

import (
	"fmt"
	"github.com/aceld/zinx/ziface"
	"github.com/aceld/zinx/znet"
	"time"
)

// Custom business logic for the client
func pingLoop(conn ziface.IConnection) {
	for {
		err := conn.SendMsg(1, []byte("Ping...Ping...Ping...[FromClient]"))
		if err != nil {
			fmt.Println(err)
			break
		}

		// Get the attribute parameters carried by the link
		name, _ := conn.GetProperty("Name")
		home, _ := conn.GetProperty("Home")
		fmt.Println("Name = ", name, "Home = ", home)

		time.Sleep(1 * time.Second)
	}
}

// Executed when the connection is created
func onClientStart(conn ziface.IConnection) {
	fmt.Println("onClientStart is Called ... ")

	// Set the property parameter carried by the current IConnection
	conn.SetProperty("Name", "aceld")
	conn.SetProperty("Home", "https://www.yuque.com/aceld")

	go pingLoop(conn)
}

func main() {
	// Create a client
	client := znet.NewClient("127.0.0.1", 8999)

	// Set the hook function after the link is established
	client.SetOnConnStart(onClientStart)

	// Start the client
	client.Start()

	// Prevent the process from exiting and wait for the interrupt signal
	select {}
}

Run the client and get the following result:

$ go run client.go 
2023/03/23 14:28:48 [INFO]client.go:73: [START] Zinx Client LocalAddr: 127