examples/chat: Various fixes
This commit is contained in:
parent
5217f65cb8
commit
932e076113
@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "io"
|
||||
import "fmt"
|
||||
import "time"
|
||||
import "bufio"
|
||||
import "errors"
|
||||
import "context"
|
||||
import "crypto/tls"
|
||||
import "git.tebibyte.media/sashakoshka/hopp"
|
||||
@ -22,6 +24,7 @@ func main() {
|
||||
}
|
||||
trans, err := join(address, room, nickname)
|
||||
handleErr(1, err)
|
||||
fmt.Fprintf(os.Stdout, "(i) connected to %s/%s\n", address, room)
|
||||
go func() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
@ -32,7 +35,12 @@ func main() {
|
||||
}()
|
||||
for {
|
||||
message, _, err := chat.Receive(trans)
|
||||
handleErr(1, err)
|
||||
if err != nil {
|
||||
if !errors.Is(err, io.EOF) {
|
||||
handleErr(1, err)
|
||||
}
|
||||
break
|
||||
}
|
||||
switch message := message.(type) {
|
||||
case *chat.MessageChat:
|
||||
fmt.Fprintf(os.Stdout, "%s: %s\n", message.Nickname, message.Content)
|
||||
@ -42,6 +50,7 @@ func main() {
|
||||
fmt.Fprintf(os.Stdout, "(i) %s left the room\n", message.Nickname)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(os.Stdout, "(i) disconnected\n")
|
||||
}
|
||||
|
||||
func join(address string, room string, nickname string) (hopp.Trans, error) {
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "io"
|
||||
import "fmt"
|
||||
import "log"
|
||||
import "errors"
|
||||
@ -48,7 +49,7 @@ func host(address string, certPath, keyPath string) error {
|
||||
|
||||
type client struct {
|
||||
conn hopp.Conn
|
||||
nickname hopp.Option[string]
|
||||
nickname string
|
||||
rooms usync.RWMonitor[map[string] hopp.Trans]
|
||||
}
|
||||
|
||||
@ -58,13 +59,13 @@ func (this *client) run() {
|
||||
defer this.conn.Close()
|
||||
|
||||
for {
|
||||
log.Println("accepting transaction")
|
||||
trans, err := this.conn.AcceptTrans()
|
||||
if err != nil {
|
||||
log.Printf("XXX %v failed: %v", this.conn.RemoteAddr(), err)
|
||||
if !errors.Is(err, io.EOF) {
|
||||
log.Printf("XXX %v failed: %v", this.conn.RemoteAddr(), err)
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Println("accepted transaction")
|
||||
go this.runTrans(trans)
|
||||
}
|
||||
}
|
||||
@ -111,7 +112,7 @@ func (this *client) transTalk(trans hopp.Trans, initial *chat.MessageJoin) error
|
||||
}
|
||||
|
||||
func (this *client) handleMessageChat(trans hopp.Trans, room string, message *chat.MessageChat) error {
|
||||
log.Println("(). %s #%s: %s", this.nickname, room, message.Content)
|
||||
log.Printf("(). %s #%s: %s", this.nickname, room, message.Content)
|
||||
clients, done := clients.RBorrow()
|
||||
defer done()
|
||||
for client := range clients {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user