-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.go
49 lines (41 loc) · 851 Bytes
/
clock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package katamari
import (
"errors"
"fmt"
"net/http"
"strconv"
"time"
)
// Time returns a string timestamp
func Time() string {
now := time.Now().UTC().UnixNano()
return strconv.FormatInt(now, 10)
}
func (app *Server) sendTime() {
app.Stream.BroadcastClock(Time())
}
func (app *Server) tick() {
ticker := time.NewTicker(app.Tick)
for {
<-ticker.C
if app.Active() {
app.sendTime()
continue
}
return
}
}
func (app *Server) clock(w http.ResponseWriter, r *http.Request) {
if !app.Audit(r) {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "%s", errors.New("katamari: this request is not authorized"))
app.Console.Err("socketConnectionUnauthorized time")
return
}
client, err := app.Stream.New("", w, r)
if err != nil {
return
}
go app.Stream.WriteClock(client, Time())
app.Stream.Read("", client)
}