mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-14 16:17:17 +03:00
Progress trying to make things less buggy, not much.
This commit is contained in:
parent
eda2b7c0d9
commit
601a95c1cd
@ -32,7 +32,11 @@ func NewChannel() *Channel {
|
||||
// Skip
|
||||
return
|
||||
}
|
||||
user.Send(m)
|
||||
err := user.Send(m)
|
||||
if err != nil {
|
||||
ch.Leave(user)
|
||||
user.Close()
|
||||
}
|
||||
})
|
||||
}
|
||||
}()
|
||||
@ -41,6 +45,10 @@ func NewChannel() *Channel {
|
||||
}
|
||||
|
||||
func (ch *Channel) Close() {
|
||||
ch.users.Each(func(u Item) {
|
||||
u.(*User).Close()
|
||||
})
|
||||
ch.users.Clear()
|
||||
close(ch.broadcast)
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,15 @@ func NewSet() *Set {
|
||||
}
|
||||
}
|
||||
|
||||
// Remove all items and return the number removed
|
||||
func (s *Set) Clear() int {
|
||||
s.Lock()
|
||||
n := len(s.lookup)
|
||||
s.lookup = map[Id]Item{}
|
||||
s.Unlock()
|
||||
return n
|
||||
}
|
||||
|
||||
// Size of the set right now
|
||||
func (s *Set) Len() int {
|
||||
return len(s.lookup)
|
||||
|
@ -23,6 +23,9 @@ const (
|
||||
|
||||
// Invert inverts the following text
|
||||
Invert = "\033[7m"
|
||||
|
||||
// Newline
|
||||
Newline = "\r\n"
|
||||
)
|
||||
|
||||
// Interface for Colors
|
||||
|
@ -19,6 +19,7 @@ type User struct {
|
||||
joined time.Time
|
||||
msg chan Message
|
||||
done chan struct{}
|
||||
closed bool
|
||||
Config UserConfig
|
||||
}
|
||||
|
||||
@ -74,6 +75,7 @@ func (u *User) Wait() {
|
||||
|
||||
// Disconnect user, stop accepting messages
|
||||
func (u *User) Close() {
|
||||
u.closed = true
|
||||
close(u.done)
|
||||
close(u.msg)
|
||||
}
|
||||
@ -94,7 +96,7 @@ func (u *User) ConsumeOne(out io.Writer) {
|
||||
|
||||
func (u *User) consumeMsg(m Message, out io.Writer) {
|
||||
s := m.Render(u.Config.Theme)
|
||||
_, err := out.Write([]byte(s))
|
||||
_, err := out.Write([]byte(s + Newline))
|
||||
if err != nil {
|
||||
logger.Printf("Write failed to %s, closing: %s", u.Name(), err)
|
||||
u.Close()
|
||||
@ -103,6 +105,10 @@ func (u *User) consumeMsg(m Message, out io.Writer) {
|
||||
|
||||
// Add message to consume by user
|
||||
func (u *User) Send(m Message) error {
|
||||
if u.closed {
|
||||
return ErrUserClosed
|
||||
}
|
||||
|
||||
select {
|
||||
case u.msg <- m:
|
||||
default:
|
||||
|
11
cmd.go
11
cmd.go
@ -104,11 +104,18 @@ func main() {
|
||||
go func() {
|
||||
defer term.Close()
|
||||
name := term.Conn.User()
|
||||
term.SetPrompt(fmt.Sprintf("[%s]", name))
|
||||
term.SetPrompt(fmt.Sprintf("[%s] ", name))
|
||||
// TODO: term.AutoCompleteCallback = ...
|
||||
user := chat.NewUserScreen(name, term)
|
||||
defer user.Close()
|
||||
channel.Join(user)
|
||||
|
||||
go func() {
|
||||
// FIXME: This isn't working.
|
||||
user.Wait()
|
||||
channel.Leave(user)
|
||||
}()
|
||||
|
||||
for {
|
||||
// TODO: Handle commands etc?
|
||||
line, err := term.ReadLine()
|
||||
@ -120,8 +127,6 @@ func main() {
|
||||
}
|
||||
|
||||
// TODO: Handle disconnect sooner (currently closes channel before removing)
|
||||
channel.Leave(user)
|
||||
user.Close()
|
||||
}()
|
||||
}
|
||||
}()
|
||||
|
Loading…
x
Reference in New Issue
Block a user