https://github.com/codeskyblue/heartbeat
heart beat for process status check. (心跳检测,UDP协议)
https://github.com/codeskyblue/heartbeat
Last synced: about 1 month ago
JSON representation
heart beat for process status check. (心跳检测,UDP协议)
- Host: GitHub
- URL: https://github.com/codeskyblue/heartbeat
- Owner: codeskyblue
- License: gpl-2.0
- Created: 2013-07-16T03:10:12.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2018-12-17T03:27:06.000Z (over 6 years ago)
- Last Synced: 2025-03-25T14:44:48.672Z (about 2 months ago)
- Language: Go
- Size: 25.4 KB
- Stars: 23
- Watchers: 3
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# heartbeat
Implement a simple hearbeat detect with HTTP protocol with secret.For old version which use UDP protocol. see [tag 1.0](#TODO)
## Install
```bash
go get -v github.com/codeskyblue/heartbeat
```## Usage
Server and Client should have the same secret.Server Example:
```go
package mainimport (
"fmt"
"net/http"
"time""github.com/codeskyblue/heartbeat"
)func main() {
hbs := heartbeat.NewServer("my-secret", 15*time.Second) // secret: my-secret, timeout: 15s
hbs.OnConnect = func(identifier string, r *http.Request) {
fmt.Println(identifier, "is online")
}
hbs.OnDisconnect = func(identifier string) {
fmt.Println(identifier, "is offline")
}
http.Handle("/heartbeat", hbs)
http.ListenAndServe(":7000", nil)
}
```Client Example:
```go
package mainimport (
"time""github.com/codeskyblue/heartbeat"
)func main() {
client := &heartbeat.Client{
ServerAddr: "http://localhost:7000/heartbeat", // replace to your server addr
Secret: "my-secret", // must be save with server secret
Identifier: "client-unique-name",
}
cancel := client.Beat(5 * time.Second)
defer cancel() // cancel heartbeat
// Do something else
time.Sleep(10 * time.Second)
}
```## Protocol
1. client get timestamp from server
2. client send identifier, timestamp and hmac hash to server every interval
3. server send back the new timestamp to client on each request# LICENSE
[GNU 2.0](LICENSE)