Skip to content

Commit 4a90351

Browse files
committed
Moved subscribe to reflect the same code you should have at the end of the tutorial.
1 parent f40346a commit 4a90351

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

main.go

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,41 +39,6 @@ func drawchat(channel string, username string) {
3939
return nil
4040
})
4141

42-
// Subscribe (listen on) a channel.
43-
successChannel := make(chan []byte)
44-
go pubnub.Subscribe(channel, "", successChannel, false, make(chan []byte))
45-
go func() {
46-
for {
47-
select {
48-
case response := <-successChannel:
49-
var msg []interface{}
50-
err := json.Unmarshal(response, &msg)
51-
if err != nil {
52-
log.Println(err)
53-
return
54-
}
55-
ov, err := g.View("output")
56-
if err != nil {
57-
log.Println("Cannot get output view:", err)
58-
return
59-
}
60-
switch m := msg[0].(type) {
61-
case []interface{}:
62-
// Get output view and print.
63-
_, err = fmt.Fprintf(ov, "%s", m[0])
64-
if err != nil {
65-
log.Println("Cannot print to output view:", err)
66-
}
67-
68-
}
69-
// Refresh view
70-
g.Update(func(g *gocui.Gui) error {
71-
return nil
72-
})
73-
}
74-
}
75-
}()
76-
7742
// Terminal width and height.
7843
termwidth, termheight := g.Size()
7944

@@ -158,6 +123,40 @@ func drawchat(channel string, username string) {
158123
log.Println("Cannot set focus to input view:", err)
159124
}
160125

126+
// Subscribe (listen on) a channel.
127+
successChannel := make(chan []byte)
128+
go pubnub.Subscribe(channel, "", successChannel, false, make(chan []byte))
129+
go func() {
130+
for {
131+
select {
132+
case response := <-successChannel:
133+
var msg []interface{}
134+
err := json.Unmarshal(response, &msg)
135+
if err != nil {
136+
log.Println(err)
137+
return
138+
}
139+
ov, err := g.View("output")
140+
if err != nil {
141+
log.Println("Cannot get output view:", err)
142+
return
143+
}
144+
switch m := msg[0].(type) {
145+
case []interface{}:
146+
// Get output view and print.
147+
_, err = fmt.Fprintf(ov, "%s", m[0])
148+
if err != nil {
149+
log.Println("Cannot print to output view:", err)
150+
}
151+
}
152+
// Refresh view
153+
g.Update(func(g *gocui.Gui) error {
154+
return nil
155+
})
156+
}
157+
}
158+
}()
159+
161160
// Start the main loop.
162161
err = g.MainLoop()
163162
log.Println("Main loop has finished:", err)

0 commit comments

Comments
 (0)