xingo之hello world

 xingo helloworld主要实现功能,xingo客户端helloworld_client.go连接xingo helloworld服务器发送HelloReq消息,服务器收到消息后发送ack HelloAck给客户端,之后延迟5秒发送DelayNtf消息给客户端报时,所有代码见仓库[地址:点我]。 

(1)定义protobuf通信协议 

syntax="proto3";
package pb;
option csharp_namespace="Pb";

//msg=1
message HelloReq{
    string name         = 1;
}

//msg=2
message HelloAck{
    string content      = 1;
}

//msg=3
message DelayNtf{
    string ts          = 1;
}

 生成msg.pb.go 

\path\to\protoc.exe –plugin=protoc-gen-go=%GOPATH%\bin\protoc-gen-go.exe –go_out F:\workspace\src\xingo_examples\helloword\pb -I F:\workspace\src\xingo_examples\helloword\pb F:\workspace\src\xingo_examples\helloword\pb\msg.proto 

 (2)api
这里需要实现HelloReq协议的处理函数

func (this *TestRouter)sendDelayMsg(fconn iface.Iconnection){
	utils.GlobalObject.GetSafeTimer().CreateTimer(5000, func(args ...interface{}){
		con := args[0].(iface.Iconnection)
		ntf := &pb.DelayNtf{
			Ts: time.Now().String(),
		}
		ntfRaw, err := utils.GlobalObject.Protoc.GetDataPack().Pack(3, ntf)
		if err == nil {
			con.Send(ntfRaw)
		}
	},[]interface{}{fconn})//xingo定时器
}

/*
HelloReq
*/
func (this *TestRouter)Handle(request iface.IRequest){
	msg := &pb.HelloReq{}
	err := proto.Unmarshal(request.GetData(), msg)
	if err == nil {
		request.GetConnection().SetProperty("name", msg.Name)
		//send ack
		ack := &pb.HelloAck{
			Content: fmt.Sprintf("Hello %s.You will receive a Ntf after 5 seconds.\n", msg.Name),
		}
		data, err := utils.GlobalObject.Protoc.GetDataPack().Pack(2, ack)
		if err == nil{
			request.GetConnection().Send(data)
			this.sendDelayMsg(request.GetConnection())
		}
	} else {
		logger.Error(err)
		request.GetConnection().LostConnection()
	}
}

 (3)实现自己的xingo server
首先需要修改配置文件conf/server.json

{
	"WriteList": ["127.0.0.1"],
	"DebugPort": 8881,
	"TcpPort": 8999,
	"PoolSize": 1,
	"LogLevel": 1,
	"MaxConn": 2000,
	"MaxPacketSize": 1024,
	"FrequencyControl": "10/s"
}

 helloworld_server.go

func DoConnectionMade(fconn iface.Iconnection) {
	logger.Debug(fmt.Sprintf("session %d connectioned helloworld server.", fconn.GetSessionId()))
}

func DoConnectionLost(fconn iface.Iconnection) {
	logger.Debug(fmt.Sprintf("session %d disconnectioned helloworld server.", fconn.GetSessionId()))
}

func main() {
	s := xingo.NewXingoTcpServer()

	//add api ---------------start
	TestRouterObj := &api.TestRouter{}
	s.AddRouter("1", TestRouterObj)
	//add api ---------------end

	//regest callback
	utils.GlobalObject.OnConnectioned = DoConnectionMade
	utils.GlobalObject.OnClosed = DoConnectionLost
	s.Serve()
}

 (4) helloworld_client.go
首先需要实现自己的clientprotocol ->HelloWorldCPtotoc
然后再HelloWorldCPtotoc的DoMsg中处理服务器返回的ack和ntf协议消息,详情见代码。

func (this *HelloWorldCPtotoc)DoMsg(fconn iface.Iclient, pdata *fnet.PkgData){
	//处理消息
	fmt.Println(fmt.Sprintf("msg id :%d, data len: %d", pdata.MsgId, pdata.Len))
	switch pdata.MsgId {
	case 2:
		ack := &pb.HelloAck{}
		err := proto.Unmarshal(pdata.Data, ack)
		if err == nil {
			logger.Debug(ack.Content)
		}else{
			logger.Error("Unmarshal ack err: ", err)
		}
	case 3:
		nft := &pb.DelayNtf{}
		err := proto.Unmarshal(pdata.Data, nft)
		if err == nil {
			logger.Debug(nft.Ts)
		}else{
			logger.Error("Unmarshal ntf err: ", err)
		}
	default:
		logger.Error("Unkown message!!!!")
	}
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注