package main import ( "bufio" "fmt" "net" "protohackers/pkg/conn" ) func main() { l, err := net.Listen(conn.Type, conn.Port) if err != nil { panic(err) } defer l.Close() fmt.Println(fmt.Sprintf("Listening on port %s", conn.Port)) r := createRoom() for { c, err := l.Accept() if err != nil { break } go handleConn(r, c) } } func handleConn(r *room, c net.Conn) { u := newUser(c) defer c.Close() go u.sendMessage() u.write("Welcome to budgetchat! What shall I call you?") s := bufio.NewScanner(u.conn) for s.Scan() { input := s.Text() if !u.online { r.sendToRest(r.joinMsg(u), u) u.write(r.connectMsg()) r.joinUser(u) } else { r.sendToRest(msgWrap(input, u), u) } if s.Err() != nil { return } } } func msgWrap(msg string, u *user) string { return fmt.Sprintf("[%s] %s", u.nick, msg) }