@@ -45,7 +45,14 @@ defmodule Firmware.ServerLink do
45
45
|> assign ( :mac_addr , Keyword . get ( config , :mac_addr ) )
46
46
|> assign ( :main_topic , "group:main" )
47
47
48
- { :ok , socket }
48
+ { :ok , socket , { :continue , :start_ping } }
49
+ end
50
+
51
+ @ impl Slipstream
52
+ def handle_continue ( :start_ping , socket ) do
53
+ timer = :timer . send_interval ( 1000 , self ( ) , :request_metrics )
54
+
55
+ { :noreply , assign ( socket , :ping_timer , timer ) }
49
56
end
50
57
51
58
@ impl Slipstream
@@ -68,33 +75,27 @@ defmodule Firmware.ServerLink do
68
75
def handle_join ( "group:main" , topic , join_response , socket ) do
69
76
Logger . info ( "Join_response => #{ inspect ( join_response ) } " )
70
77
# an asynchronous push with no reply:
71
- # push(socket, @topic, "handshake", %{:hello => :world})
72
- ## something = send(self(), :start_ping)
73
- ## IO.inspect("sent interval #{inspect(something)}")
78
+ push ( socket , "group:main" , "handshake" , % { :hello => :world } )
74
79
{ :ok , socket }
75
80
end
76
81
77
82
@ impl Slipstream
78
- def handle_info ( :start_ping , socket ) do
79
- case connected? ( socket ) do
80
- true ->
81
- timer = :timer . send_interval ( 10_000 , self ( ) , :ping_server )
82
- IO . inspect ( timer , label: :TIMER )
83
- { :noreply , assign ( socket , :ping_timer , timer ) }
84
-
85
- false ->
86
- IO . puts ( "Ping not available due socket conn down" )
87
- :timer . send_after ( self ( ) , :start_ping , 1_000 )
88
- end
83
+ def handle_info ( :request_metrics , socket ) do
84
+ # we will asynchronously receive a reply and handle it in the
85
+ # handle_reply/3 implementation below
86
+ { :ok , ref } = push ( socket , "group:main" , "get_metrics" , % { format: "json" } )
87
+
88
+ { :noreply , assign ( socket , :metrics_request , ref ) }
89
89
end
90
90
91
91
@ impl Slipstream
92
- def handle_info ( :ping_server , socket ) do
93
- # we will asynchronously receive a reply and handle it in the
94
- # handle_reply/3 implementation below
95
- { :ok , ref } = push ( socket , "group:main" , "ping" , % { format: "json" } )
92
+ def handle_reply ( ref , metrics , socket ) do
93
+ if ref == socket . assigns . metrics_request do
94
+ Logger . info ( "Do some metrics #{ inspect ( metrics ) } " )
95
+ # :ok = MyApp.MetricsPublisher.publish(metrics)
96
+ end
96
97
97
- { :noreply , assign ( socket , :last_ping , ref ) }
98
+ { :ok , socket }
98
99
end
99
100
100
101
@ impl Slipstream
@@ -108,7 +109,7 @@ defmodule Firmware.ServerLink do
108
109
def handle_info ( :disconnect , socket ) do
109
110
IO . inspect ( "Disconect" )
110
111
## result = :timer.cancel(socket.assigns.ping_timer)
111
- ## IO.inspect(result, label: :RESULT)
112
+ # IO.inspect(result, label: :RESULT)
112
113
113
114
{ :ok , socket } =
114
115
socket
0 commit comments