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