-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aceld/zinx/ziface" | ||
"github.com/aceld/zinx/znet" | ||
"time" | ||
) | ||
|
||
// 用户自定义的心跳检测消息处理方法 | ||
func myHeartBeatMsg(conn ziface.IConnection) []byte { | ||
return []byte("heartbeat, I am server, I am alive") | ||
} | ||
|
||
// 用户自定义的远程连接不存活时的处理方法 | ||
func myOnRemoteNotAlive(conn ziface.IConnection) { | ||
fmt.Println("myOnRemoteNotAlive is Called, connID=", conn.GetConnID(), "remoteAddr = ", conn.RemoteAddr()) | ||
//关闭链接 | ||
conn.Stop() | ||
} | ||
|
||
// 用户自定义的心跳检测消息处理方法 | ||
type myHeartBeatRouter struct { | ||
znet.BaseRouter | ||
} | ||
|
||
func (r *myHeartBeatRouter) Handle(request ziface.IRequest) { | ||
// 业务处理 | ||
fmt.Println("in MyHeartBeatRouter Handle, recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData())) | ||
} | ||
|
||
func main() { | ||
//创建Client客户端 | ||
client := znet.NewClient("127.0.0.1", 8999) | ||
s := znet.NewServer() | ||
|
||
//设置心跳检测 | ||
client.StartHeartBeat(3 * time.Second) | ||
myHeartBeatMsgID := 88888 | ||
|
||
//启动客户端 | ||
client.Start() | ||
//启动心跳检测 | ||
s.StartHeartBeatWithOption(1*time.Second, &ziface.HeartBeatOption{ | ||
MakeMsg: myHeartBeatMsg, | ||
OnRemoteNotAlive: myOnRemoteNotAlive, | ||
Router: &myHeartBeatRouter{}, | ||
HeadBeatMsgID: uint32(myHeartBeatMsgID), | ||
}) | ||
|
||
//防止进程退出,等待中断信号 | ||
select {} | ||
s.Serve() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters