Compare commits
2 Commits
42c41a194b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f786edfb90 | ||
|
|
165fe1b9e5 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*exe
|
||||
4
go.mod
4
go.mod
@@ -1,3 +1,7 @@
|
||||
module test
|
||||
|
||||
go 1.24.5
|
||||
|
||||
require github.com/gorilla/mux v1.8.1
|
||||
|
||||
require github.com/gorilla/websocket v1.5.3
|
||||
|
||||
4
go.sum
Normal file
4
go.sum
Normal file
@@ -0,0 +1,4 @@
|
||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
90
main.go
90
main.go
@@ -1,7 +1,93 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("hello from go")
|
||||
port := flag.Uint("port", 9401, "listening port")
|
||||
flag.Parse()
|
||||
|
||||
router := mux.NewRouter()
|
||||
|
||||
router.HandleFunc("/start", start)
|
||||
router.HandleFunc("/stop", stop)
|
||||
router.HandleFunc("/status", state)
|
||||
router.HandleFunc("/ws", websocketHandler)
|
||||
|
||||
http.Handle("/", router)
|
||||
http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
|
||||
}
|
||||
|
||||
var Prg Program
|
||||
|
||||
func start(w http.ResponseWriter, r *http.Request) {
|
||||
if Prg.Run {
|
||||
w.Write([]byte("Programm already running"))
|
||||
setState("Programm already running")
|
||||
|
||||
return
|
||||
}
|
||||
Prg.start()
|
||||
w.Write([]byte("Program started"))
|
||||
setState("Programm started")
|
||||
|
||||
}
|
||||
|
||||
func stop(w http.ResponseWriter, r *http.Request) {
|
||||
if !Prg.Run {
|
||||
w.Write([]byte("Programm already stopped"))
|
||||
setState("Programm already stopped")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Prg.stop()
|
||||
w.Write([]byte("Program stopped"))
|
||||
setState("Programm stopped")
|
||||
|
||||
}
|
||||
|
||||
func state(w http.ResponseWriter, r *http.Request) {
|
||||
if !Prg.Run {
|
||||
w.Write([]byte("Programm stopped"))
|
||||
setState("Programm stopped")
|
||||
return
|
||||
}
|
||||
|
||||
w.Write([]byte(Prg.State))
|
||||
}
|
||||
|
||||
type Program struct {
|
||||
Run bool
|
||||
State string
|
||||
}
|
||||
|
||||
func (p *Program) start() {
|
||||
if p.Run {
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
p.Run = true
|
||||
counter := 0
|
||||
for p.Run {
|
||||
counter++
|
||||
if counter > 100 {
|
||||
counter = 0
|
||||
}
|
||||
p.State = fmt.Sprintf("Hello, Go! :%d", counter)
|
||||
setState(fmt.Sprintf("Hello, Go! :%d", counter))
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (p *Program) stop() {
|
||||
p.Run = false
|
||||
}
|
||||
|
||||
102
websocket.go
Normal file
102
websocket.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// WebSocket upgrader
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
// Allow all origins (CORS)
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
clients = make(map[*websocket.Conn]bool)
|
||||
clientsMu sync.Mutex
|
||||
|
||||
appState = "idle"
|
||||
appStateMu sync.Mutex
|
||||
)
|
||||
|
||||
func websocketHandler(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
log.Println("WebSocket upgrade error:", err)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
// Register client
|
||||
clientsMu.Lock()
|
||||
clients[conn] = true
|
||||
clientsMu.Unlock()
|
||||
|
||||
log.Println("Client connected")
|
||||
|
||||
// Optionally send initial state
|
||||
sendStateToClient(conn)
|
||||
|
||||
for {
|
||||
_, _, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
// (Ignore incoming messages — or handle if needed)
|
||||
}
|
||||
|
||||
// Remove client on disconnect
|
||||
clientsMu.Lock()
|
||||
delete(clients, conn)
|
||||
clientsMu.Unlock()
|
||||
|
||||
log.Println("Client disconnected")
|
||||
}
|
||||
|
||||
func setState(newState string) {
|
||||
appStateMu.Lock()
|
||||
defer appStateMu.Unlock()
|
||||
|
||||
if appState != newState {
|
||||
appState = newState
|
||||
//log.Printf("State changed to: %s", newState)
|
||||
broadcastState()
|
||||
}
|
||||
}
|
||||
|
||||
func broadcastState() {
|
||||
clientsMu.Lock()
|
||||
defer clientsMu.Unlock()
|
||||
|
||||
for conn := range clients {
|
||||
err := conn.WriteJSON(map[string]string{
|
||||
"type": "state_update",
|
||||
"state": appState,
|
||||
"time": time.Now().Format(time.RFC3339),
|
||||
})
|
||||
if err != nil {
|
||||
log.Println("Error sending to client, removing:", err)
|
||||
conn.Close()
|
||||
delete(clients, conn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func sendStateToClient(conn *websocket.Conn) {
|
||||
appStateMu.Lock()
|
||||
defer appStateMu.Unlock()
|
||||
|
||||
conn.WriteJSON(map[string]string{
|
||||
"type": "state_update",
|
||||
"state": appState,
|
||||
"time": time.Now().Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user