130 lines
2.9 KiB
Go
130 lines
2.9 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
"os/signal"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"git.saintnet.tech/stryan/snengame/internal/coordinator"
|
||
|
"github.com/google/uuid"
|
||
|
"github.com/gorilla/websocket"
|
||
|
)
|
||
|
|
||
|
var done chan interface{}
|
||
|
var interrupt chan os.Signal
|
||
|
|
||
|
func receiveHandler(connection *websocket.Conn) {
|
||
|
defer close(done)
|
||
|
for {
|
||
|
var resp coordinator.SessionCommandResult
|
||
|
err := connection.ReadJSON(&resp)
|
||
|
if err != nil {
|
||
|
log.Println("Error in receive:", err)
|
||
|
return
|
||
|
}
|
||
|
log.Printf("Received: %s\n", resp)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
done = make(chan interface{}) // Channel to indicate that the receiverHandler is done
|
||
|
interrupt = make(chan os.Signal) // Channel to listen for interrupt signal to terminate gracefully
|
||
|
cmd := make(chan coordinator.SessionCommand)
|
||
|
|
||
|
signal.Notify(interrupt, os.Interrupt) // Notify the interrupt channel for SIGINT
|
||
|
|
||
|
socketUrl := "ws://localhost:7636" + "/ws"
|
||
|
conn, _, err := websocket.DefaultDialer.Dial(socketUrl, nil)
|
||
|
id := uuid.New()
|
||
|
|
||
|
if err != nil {
|
||
|
log.Fatal("Error connecting to Websocket Server:", err)
|
||
|
}
|
||
|
defer conn.Close()
|
||
|
go receiveHandler(conn)
|
||
|
go GetCommand(id, cmd)
|
||
|
|
||
|
// Our main loop for the client
|
||
|
// We send our relevant packets here
|
||
|
for {
|
||
|
var c coordinator.SessionCommand
|
||
|
select {
|
||
|
case c = <-cmd:
|
||
|
// Send an echo packet every second
|
||
|
err := conn.WriteJSON(c)
|
||
|
if err != nil {
|
||
|
log.Println("Error during writing to websocket:", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
case <-interrupt:
|
||
|
// We received a SIGINT (Ctrl + C). Terminate gracefully...
|
||
|
log.Println("Received SIGINT interrupt signal. Closing all pending connections")
|
||
|
|
||
|
// Close our websocket connection
|
||
|
err := conn.WriteJSON(coordinator.SessionCommand{
|
||
|
ID: id,
|
||
|
Command: coordinator.SessionCmdLeave,
|
||
|
})
|
||
|
if err != nil {
|
||
|
log.Println("Error during closing websocket:", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
select {
|
||
|
case <-done:
|
||
|
log.Println("Receiver Channel Closed! Exiting....")
|
||
|
case <-time.After(time.Duration(1) * time.Second):
|
||
|
log.Println("Timeout in closing receiving channel. Exiting....")
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func GetCommand(uid uuid.UUID, resp chan coordinator.SessionCommand) {
|
||
|
for {
|
||
|
var cmd string
|
||
|
var t int
|
||
|
fmt.Print("> ")
|
||
|
_, err := fmt.Scanf("%d", &t)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
}
|
||
|
if t == -1 {
|
||
|
panic("quitting")
|
||
|
}
|
||
|
_, err = fmt.Scanf("%s", &cmd)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
}
|
||
|
cmd = strings.TrimSpace(cmd)
|
||
|
switch t {
|
||
|
case 0:
|
||
|
//session
|
||
|
switch coordinator.SessionCmd(cmd) {
|
||
|
case coordinator.SessionCmdQuery:
|
||
|
resp <- coordinator.SessionCommand{
|
||
|
ID: uid,
|
||
|
Command: coordinator.SessionCmdQuery,
|
||
|
}
|
||
|
case coordinator.SessionCmdJoin:
|
||
|
resp <- coordinator.SessionCommand{
|
||
|
ID: uid,
|
||
|
Command: coordinator.SessionCmdJoin,
|
||
|
}
|
||
|
case coordinator.SessionCmdLeave:
|
||
|
resp <- coordinator.SessionCommand{
|
||
|
ID: uid,
|
||
|
Command: coordinator.SessionCmdLeave,
|
||
|
}
|
||
|
default:
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|