-
Notifications
You must be signed in to change notification settings - Fork 10
/
simple.go
118 lines (98 loc) · 3.23 KB
/
simple.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"github.com/nshafer/phx"
"log"
"net/url"
)
// This example works with a simple channel generated with:
//
// mix phx.gen.channel Test
//
// then adding it to 'user_socket.ex' like:
//
// channel "test:lobby", MyProjectWeb.TestChannel
//
// See https://hexdocs.pm/phoenix/channels.html for more information
func main() {
// Create a url.URL to connect to. `ws://` is non-encrypted websocket.
endPoint, _ := url.Parse("ws://localhost:4000/socket?foo=bar")
log.Println("Connecting to", endPoint)
// Create a new phx.Socket
socket := phx.NewSocket(endPoint)
// Replace the default phx.NoopLogger with phx.SimpleLogger that prints messages
// that are Info, Warning or Error. Set this to `phx.LogDebug` to see more info, such as the
// raw messages being sent and received.
socket.Logger = phx.NewSimpleLogger(phx.LogInfo)
// Wait for the socket to connect before continuing. If it's not able to, it will keep
// retrying forever.
cont := make(chan bool)
socket.OnOpen(func() {
cont <- true
})
// Tell the socket to connect (or start retrying until it can connect)
err := socket.Connect()
if err != nil {
log.Fatal(err)
}
// Wait for the connection
<-cont
// Create a phx.Channel to connect to the default 'room:lobby' channel with no params
// (alternatively params could be a `map[string]string`)
channel := socket.Channel("test:lobby", nil)
// Join the channel. A phx.Push is returned which can be used to bind to replies and errors
join, err := channel.Join()
if err != nil {
log.Fatal(err)
}
// Listen for a response and only continue once we know we're joined
join.Receive("ok", func(response any) {
log.Println("Joined channel:", channel.Topic(), response)
cont <- true
})
join.Receive("error", func(response any) {
log.Println("Join error", response)
})
// wait to be joined
<-cont
// Send a ping and see if we get a response
ping, err := channel.Push("ping", "pong")
if err != nil {
log.Fatal(err)
}
// If we get the response we expect, then continue on
ping.Receive("ok", func(response any) {
ret, ok := response.(string)
if ok && ret == "pong" {
log.Println("Got pong from our ping")
cont <- true
}
})
// If there is an error or timeout, then bail
ping.Receive("error", func(response any) {
log.Fatal(response)
})
ping.Receive("timeout", func(response any) {
log.Fatal(response)
})
// Wait for ping response
<-cont
// Let's listen for the "shout" broadcasts
channel.On("shout", func(payload any) {
log.Println("received shout:", payload)
})
// Let's send a shout, which we will also receive. The default `handle_in("shout", ...)` just
// rebroadcasts what we send, and `broadcast/3` requires the payload to be a map, so we need
// to shout a map. Your custom `handle_in/3` functions could of course handle whatever payload
// you want to send them.
shout, err := channel.Push("shout", map[string]string{"hello": "from go"})
if err != nil {
log.Fatal(err)
}
// The default `handle_in("shout", ...)` does not send a reply! So it will eventually timeout, which
// can be caught with the "timeout" event.
shout.Receive("timeout", func(response any) {
log.Println("shout timeout", response)
})
// Now we will block forever, hit ctrl+c to exit
select {}
}