mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-07-22 13:34:29 +02: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
|
// Skip
|
||||||
return
|
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() {
|
func (ch *Channel) Close() {
|
||||||
|
ch.users.Each(func(u Item) {
|
||||||
|
u.(*User).Close()
|
||||||
|
})
|
||||||
|
ch.users.Clear()
|
||||||
close(ch.broadcast)
|
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
|
// Size of the set right now
|
||||||
func (s *Set) Len() int {
|
func (s *Set) Len() int {
|
||||||
return len(s.lookup)
|
return len(s.lookup)
|
||||||
|
@ -23,6 +23,9 @@ const (
|
|||||||
|
|
||||||
// Invert inverts the following text
|
// Invert inverts the following text
|
||||||
Invert = "\033[7m"
|
Invert = "\033[7m"
|
||||||
|
|
||||||
|
// Newline
|
||||||
|
Newline = "\r\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Interface for Colors
|
// Interface for Colors
|
||||||
|
@ -19,6 +19,7 @@ type User struct {
|
|||||||
joined time.Time
|
joined time.Time
|
||||||
msg chan Message
|
msg chan Message
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
|
closed bool
|
||||||
Config UserConfig
|
Config UserConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,6 +75,7 @@ func (u *User) Wait() {
|
|||||||
|
|
||||||
// Disconnect user, stop accepting messages
|
// Disconnect user, stop accepting messages
|
||||||
func (u *User) Close() {
|
func (u *User) Close() {
|
||||||
|
u.closed = true
|
||||||
close(u.done)
|
close(u.done)
|
||||||
close(u.msg)
|
close(u.msg)
|
||||||
}
|
}
|
||||||
@ -94,7 +96,7 @@ func (u *User) ConsumeOne(out io.Writer) {
|
|||||||
|
|
||||||
func (u *User) consumeMsg(m Message, out io.Writer) {
|
func (u *User) consumeMsg(m Message, out io.Writer) {
|
||||||
s := m.Render(u.Config.Theme)
|
s := m.Render(u.Config.Theme)
|
||||||
_, err := out.Write([]byte(s))
|
_, err := out.Write([]byte(s + Newline))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Printf("Write failed to %s, closing: %s", u.Name(), err)
|
logger.Printf("Write failed to %s, closing: %s", u.Name(), err)
|
||||||
u.Close()
|
u.Close()
|
||||||
@ -103,6 +105,10 @@ func (u *User) consumeMsg(m Message, out io.Writer) {
|
|||||||
|
|
||||||
// Add message to consume by user
|
// Add message to consume by user
|
||||||
func (u *User) Send(m Message) error {
|
func (u *User) Send(m Message) error {
|
||||||
|
if u.closed {
|
||||||
|
return ErrUserClosed
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case u.msg <- m:
|
case u.msg <- m:
|
||||||
default:
|
default:
|
||||||
|
11
cmd.go
11
cmd.go
@ -104,11 +104,18 @@ func main() {
|
|||||||
go func() {
|
go func() {
|
||||||
defer term.Close()
|
defer term.Close()
|
||||||
name := term.Conn.User()
|
name := term.Conn.User()
|
||||||
term.SetPrompt(fmt.Sprintf("[%s]", name))
|
term.SetPrompt(fmt.Sprintf("[%s] ", name))
|
||||||
// TODO: term.AutoCompleteCallback = ...
|
// TODO: term.AutoCompleteCallback = ...
|
||||||
user := chat.NewUserScreen(name, term)
|
user := chat.NewUserScreen(name, term)
|
||||||
|
defer user.Close()
|
||||||
channel.Join(user)
|
channel.Join(user)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
// FIXME: This isn't working.
|
||||||
|
user.Wait()
|
||||||
|
channel.Leave(user)
|
||||||
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// TODO: Handle commands etc?
|
// TODO: Handle commands etc?
|
||||||
line, err := term.ReadLine()
|
line, err := term.ReadLine()
|
||||||
@ -120,8 +127,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Handle disconnect sooner (currently closes channel before removing)
|
// TODO: Handle disconnect sooner (currently closes channel before removing)
|
||||||
channel.Leave(user)
|
|
||||||
user.Close()
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user