Assemble useful client-server communication
This commit is contained in:
@@ -66,11 +66,8 @@ func main() {
|
||||
url: "ws://localhost:8080/ws",
|
||||
}
|
||||
wsConn.open()
|
||||
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for timestamp := range ticker.C {
|
||||
wsConn.writeMessage(timestamp.String())
|
||||
for {
|
||||
log.Printf("zzz...")
|
||||
time.Sleep(30 * time.Second)
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
@@ -9,8 +11,7 @@ import (
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{}
|
||||
|
||||
// upgrader.CheckOrigin = func(r *http.Request) bool { return true }
|
||||
var wsBroadcast = make(chan []byte, 100)
|
||||
|
||||
func wsHandler(responseWriter http.ResponseWriter, request *http.Request) {
|
||||
conn, err := upgrader.Upgrade(responseWriter, request, nil)
|
||||
@@ -18,29 +19,66 @@ func wsHandler(responseWriter http.ResponseWriter, request *http.Request) {
|
||||
fmt.Println("Error during connection upgrade:", err)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
go wsHandleRead(conn)
|
||||
wsHandleWrite(conn)
|
||||
}
|
||||
|
||||
func wsHandleRead(conn *websocket.Conn) {
|
||||
log.Printf("Starting read handler")
|
||||
for {
|
||||
messageType, packet, err := conn.ReadMessage()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error during message reading:", err)
|
||||
break
|
||||
return
|
||||
}
|
||||
log.Printf("Received: %v %s", messageType, packet)
|
||||
|
||||
// err = conn.WriteMessage(messageType, packet)
|
||||
// if err != nil {
|
||||
// fmt.Println("Error during message writing:", err)
|
||||
// break
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
func wsHandleWrite(conn *websocket.Conn) {
|
||||
log.Println("Starting write handler")
|
||||
for {
|
||||
packet := <-wsBroadcast
|
||||
log.Printf("Broadcasting: %s", packet)
|
||||
err := conn.WriteMessage(websocket.TextMessage, packet)
|
||||
if err != nil {
|
||||
fmt.Println("Error during message writing:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type DownloadReq struct {
|
||||
Link string `json:"link"`
|
||||
}
|
||||
|
||||
func handleDownload(responseWriter http.ResponseWriter, request *http.Request) {
|
||||
body, err := io.ReadAll(request.Body)
|
||||
if err != nil {
|
||||
log.Printf("Error reading request body: %v", err)
|
||||
http.Error(responseWriter, "Error reading request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer request.Body.Close()
|
||||
|
||||
req := DownloadReq{}
|
||||
err = json.Unmarshal(body, &req)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing JSON: %v", err)
|
||||
http.Error(responseWriter, "Error parsing JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Received download request: %s", req.Link)
|
||||
wsBroadcast <- []byte(req.Link)
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.SetFlags(log.Lmicroseconds)
|
||||
|
||||
http.HandleFunc("/ws", wsHandler)
|
||||
http.HandleFunc("/download", handleDownload)
|
||||
log.Println("Server starting on :8080")
|
||||
err := http.ListenAndServe(":8080", nil)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user