update
This commit is contained in:
53
server/server.go
Normal file
53
server/server.go
Normal file
@ -0,0 +1,53 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"git.rouggy.com/rouggy/RaceBot/internal/domain"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
listenAddr string
|
||||
listener net.Listener
|
||||
Conn net.Conn
|
||||
}
|
||||
|
||||
func NewServer(listenAddr string) *Server {
|
||||
return &Server{
|
||||
listenAddr: listenAddr,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Start(cfg *domain.Config) {
|
||||
tcpAddr, _ := net.ResolveTCPAddr("tcp", cfg.Host+":"+cfg.Port)
|
||||
|
||||
s.listener, _ = net.ListenTCP("tcp", tcpAddr)
|
||||
|
||||
fmt.Println("[Server] Server is running on port", cfg.Port)
|
||||
|
||||
for {
|
||||
s.Conn, _ = s.listener.Accept()
|
||||
go s.handleRequest()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleRequest() {
|
||||
//we make a buffer to hold the incoming data.
|
||||
buf := make([]byte, 1024)
|
||||
|
||||
// Read the incoming connection into the buffer.
|
||||
reqLen, err := s.Conn.Read(buf)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading:", err.Error())
|
||||
}
|
||||
|
||||
// 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."))
|
||||
|
||||
// Close the connection when you're done with it.
|
||||
s.Conn.Close()
|
||||
}
|
Reference in New Issue
Block a user