-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
105 lines (96 loc) · 2.87 KB
/
main.go
File metadata and controls
105 lines (96 loc) · 2.87 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"flag"
"fmt"
"html/template"
"io"
"math"
"net/http"
"strings"
"time"
"golang.org/x/net/websocket"
)
var (
// Portnum Port number for WS to bind to
Portnum string
// Hostsite hostname to listen on
Hostsite string
)
// PageSettings settings struct
type PageSettings struct {
Host string
Port string
}
const (
// Canvaswidth exactly what it sounds like
Canvaswidth = 512
// Canvasheight exactly what it sounds like
Canvasheight = 512
// HourColor hour color constant
HourColor = "#ff7373" // pinkish
// MinuteColor minute color constant
MinuteColor = "#00b7e4" //light blue
// SecondColor second color constant
SecondColor = "#b58900" //gold
)
func main() {
flag.StringVar(&Portnum, "Port", "1234", "Port to host server.")
flag.StringVar(&Hostsite, "Site", "localhost", "Site hosting server")
flag.Parse()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
wsurl := PageSettings{Host: Hostsite, Port: Portnum}
template, _ := template.ParseFiles("clock.html")
template.Execute(w, wsurl)
})
http.Handle("/ws", websocket.Handler(wshandle))
fmt.Printf("Running server on %v:%v\n", Hostsite, Portnum)
err := http.ListenAndServe(Hostsite+":"+Portnum, nil)
if err != nil {
fmt.Println(err)
}
}
// Given a websocket connection,
// serves updating time function
func wshandle(ws *websocket.Conn) {
fmt.Printf("Client connected from address: %s\n", ws.RemoteAddr().String())
var ioerr error
for {
hour, min, sec := time.Now().Clock()
hourx, houry := HourCords(hour, Canvasheight/2)
minx, miny := MinSecCords(min, Canvasheight/2)
secx, secy := MinSecCords(sec, Canvasheight/2)
msg := "CLEAR\n"
msg += fmt.Sprintf("HOUR %d %d %s\n", hourx, houry, HourColor)
msg += fmt.Sprintf("MIN %d %d %s\n", minx, miny, MinuteColor)
msg += fmt.Sprintf("SEC %d %d %s", secx, secy, SecondColor)
_, ioerr = io.WriteString(ws, msg)
if ioerr != nil {
if strings.Contains(ioerr.Error(), "broken pipe") {
fmt.Printf("Client disconnected\n")
break
}
fmt.Printf("Client error: %v\n", ioerr.Error())
break
}
time.Sleep(time.Second / 60.0)
}
ws.Close()
}
//MinSecCords Given current minute or second time(i.e 30 min, 60 minutes)
// and the radius, returns pair of cords to draw line to
func MinSecCords(ctime int, radius int) (int, int) {
//converts min/sec to angle and then to radians
theta := ((float64(ctime)*6 - 90) * (math.Pi / 180))
x := float64(radius) * math.Cos(theta)
y := float64(radius) * math.Sin(theta)
return int(x) + 256, int(y) + 256
}
// HourCords Given current hour time(i.e. 12, 8) and the radius,
// returns pair of cords to draw line to
func HourCords(ctime int, radius int) (int, int) {
//converts hours to angle and then to radians
theta := ((float64(ctime)*30 - 90) * (math.Pi / 180))
x := float64(radius) * math.Cos(theta)
y := float64(radius) * math.Sin(theta)
return int(x) + 256, int(y) + 256
}