diff options
-rw-r--r-- | go/main.go | 67 |
1 files changed, 61 insertions, 6 deletions
@@ -5,6 +5,7 @@ import ( "log" "net" "os" + "reflect" "gioui.org/app" "gioui.org/font/gofont" @@ -17,6 +18,19 @@ import ( "gioui.org/widget/material" ) +type XmppEvent uint8 + +const ( + XmppDisconnect XmppEvent = iota + XmppConnect +) + +type GuiEvent uint8 + +const ( + Disconnect GuiEvent = iota +) + type LimoxState uint8 const ( @@ -29,7 +43,8 @@ type Limox struct { JidEditor widget.Editor PwdEditor widget.Editor MainButton widget.Clickable - XmppError chan error + XmppEvents chan any + GuiEvents chan GuiEvent State LimoxState Window *app.Window Operations op.Ops @@ -58,7 +73,8 @@ func NewLimox() Limox { ), Operations: op.Ops{}, Theme: material.NewTheme(gofont.Collection()), - XmppError: make(chan error), + XmppEvents: make(chan any), + GuiEvents: make(chan GuiEvent), State: Disconnected, } } @@ -76,8 +92,23 @@ func (l *Limox) run() error { } l.draw(e) } - case <-l.XmppError: - l.State = Disconnected + case ev := <-l.XmppEvents: + switch ev.(type) { + case error: + log.Print(ev) + l.State = Disconnected + case XmppEvent: + switch ev { + case XmppDisconnect: + l.State = Disconnected + case XmppConnect: + l.State = Connected + default: + log.Fatalf("Unknown XmppEvent '%d'\n", ev) + } + default: + log.Fatalf("Unknown event type '%s'.\n", reflect.TypeOf(ev)) + } } } } @@ -93,6 +124,7 @@ func (l *Limox) buttonCallback() { l.State = Disconnected case Connected: log.Println("Disconnected") + l.GuiEvents <- Disconnect l.State = Disconnected } } @@ -105,11 +137,34 @@ func (l *Limox) xmpp(jid string, pwd string) { tcpServer, err := net.ResolveTCPAddr("tcp", domain+":"+"5222") if err != nil { - log.Print(err) - l.XmppError <- err + l.XmppEvents <- err return } log.Printf("Server: %s\n", tcpServer) + + conn, err := net.DialTCP("tcp", nil, tcpServer) + if err != nil { + l.XmppEvents <- err + return + } + l.XmppEvents <- XmppConnect + + var closing bool = false + for { + ev := <-l.GuiEvents + switch ev { + case Disconnect: + closing = true + default: + log.Fatalf("Unknown GuiEvent '%d'!\n", ev) + } + if closing { + break + } + } + + conn.Close() + l.XmppEvents <- XmppDisconnect } // domainpart extracts the domain name from a JID / XMPP address. See |