diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/client.go | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/internal/client.go b/internal/client.go index 164a29d..f1939f9 100644 --- a/internal/client.go +++ b/internal/client.go @@ -4,28 +4,43 @@ import ( "fmt" "log" "net" + "strings" ) -func HandleClientConnection(conn net.Conn) { - defer conn.Close() - log.Printf("New client connected from %s\n", conn.RemoteAddr()) +type Client struct { + conn net.Conn +} + +func NewIRCClient(conn net.Conn) *Client { + c := Client{conn: conn} + + return &c +} + +func (c *Client) HandleConnection() { + defer c.conn.Close() + log.Printf("New client connected from %s\n", c.conn.RemoteAddr()) buffer := make([]byte, 512) for { - n, err := conn.Read(buffer) + n, err := c.conn.Read(buffer) if err != nil { - log.Printf("Error reading from client %s: %v\n", conn.RemoteAddr(), err) + log.Printf("Error reading from client %s: %v\n", c.conn.RemoteAddr(), err) return } - data := string(buffer[:n]) - fmt.Printf("%s sent %s\n", conn.RemoteAddr(), data) + data := strings.TrimSpace(string(buffer[:n])) + fmt.Printf("%s sent %s\n", c.conn.RemoteAddr(), data) - _, err = conn.Write([]byte("you said " + data)) + _, err = c.conn.Write([]byte("you said " + data)) if err != nil { - log.Printf("Error sending to client %s: %v", conn.RemoteAddr(), err) + log.Printf("Error sending to client %s: %v", c.conn.RemoteAddr(), err) return } + + if data == "QUIT" { + break + } } } |
