-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (74 loc) · 1.89 KB
/
main.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
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
package main
import (
"flag"
"fmt"
"html/template"
"net/http"
"time"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func serveIndex(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.New("index").Parse(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket Time</title>
</head>
<body>
<div id="time"></div>
<script>
const isSecure = window.location.protocol === "https:";
const protocol = isSecure ? "wss" : "ws";
const socket = new WebSocket(protocol + "://" + window.location.host + "/ws");
socket.onmessage = function(event) {
document.getElementById("time").innerHTML = "Current Time: " + event.data;
socket.send("asda");
};
</script>
</body>
</html>
`)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
tmpl.Execute(w, nil)
}
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
return
}
defer conn.Close()
for {
currentTime := time.Now().Format("2006-01-02 15:04:05")
err := conn.WriteMessage(websocket.TextMessage, []byte(currentTime))
if err != nil {
fmt.Println(err)
return
}
time.Sleep(1 * time.Second)
typ, _, err := conn.ReadMessage()
fmt.Println("Hello", typ, err)
}
}
func main() {
var port int
flag.IntVar(&port, "p", 8080, "Port to run the server on")
flag.Parse()
http.HandleFunc("/", serveIndex)
http.HandleFunc("/ws", handleWebSocket)
fmt.Printf("Server is running on http://localhost:%d\n", port)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
fmt.Println(err)
}
}