This commit is contained in:
2024-08-28 13:14:10 +07:00
parent d976bb4a59
commit caeeb437ee
8 changed files with 41 additions and 27 deletions

View File

@ -1,10 +1,13 @@
package server
import (
"encoding/json"
"fmt"
"log"
"net"
"git.rouggy.com/rouggy/RaceBot/internal/domain"
"git.rouggy.com/rouggy/RaceBot/models"
)
type Server struct {
@ -24,7 +27,7 @@ func (s *Server) Start(cfg *domain.Config) {
s.listener, _ = net.ListenTCP("tcp", tcpAddr)
fmt.Println("[Server] Server is running on port", cfg.Port)
log.Printf("[Server] Server is running on %v:%v", cfg.Host, cfg.Port)
for {
s.Conn, _ = s.listener.Accept()
@ -33,6 +36,13 @@ func (s *Server) Start(cfg *domain.Config) {
}
func (s *Server) handleRequest() {
r := models.NewRace()
d := json.NewDecoder(s.Conn)
err := d.Decode(&r)
log.Println(r, err)
//we make a buffer to hold the incoming data.
buf := make([]byte, 1024)
@ -43,10 +53,7 @@ func (s *Server) handleRequest() {
}
// Print the message to the console.
fmt.Println("Received data:", string(buf[:reqLen]))
// Write a response back to the client.
s.Conn.Write([]byte("Message received."))
log.Println(string(buf[:reqLen]))
// Close the connection when you're done with it.
s.Conn.Close()