Skip to content

Commit e899062

Browse files
committed
Fix ping
1 parent 182f110 commit e899062

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

firmware/lib/counter.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule Firmware.Counter do
3333
end
3434

3535
defp schedule_work do
36-
IO.puts("Counter scheduling work")
36+
# IO.puts("Counter scheduling work")
3737
Process.send_after(self(), :work, 5_000)
3838
end
3939
end

firmware/lib/server_link.ex

+22-21
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ defmodule Firmware.ServerLink do
4545
|> assign(:mac_addr, Keyword.get(config, :mac_addr))
4646
|> assign(:main_topic, "group:main")
4747

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)}
4956
end
5057

5158
@impl Slipstream
@@ -68,33 +75,27 @@ defmodule Firmware.ServerLink do
6875
def handle_join("group:main", topic, join_response, socket) do
6976
Logger.info("Join_response => #{inspect(join_response)}")
7077
# 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})
7479
{:ok, socket}
7580
end
7681

7782
@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)}
8989
end
9090

9191
@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
9697

97-
{:noreply, assign(socket, :last_ping, ref)}
98+
{:ok, socket}
9899
end
99100

100101
@impl Slipstream
@@ -108,7 +109,7 @@ defmodule Firmware.ServerLink do
108109
def handle_info(:disconnect, socket) do
109110
IO.inspect("Disconect")
110111
## result = :timer.cancel(socket.assigns.ping_timer)
111-
## IO.inspect(result, label: :RESULT)
112+
# IO.inspect(result, label: :RESULT)
112113

113114
{:ok, socket} =
114115
socket

fullstack/lib/fullstack_web/channels/group_channel.ex

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule FullstackWeb.GroupChannel do
22
use FullstackWeb, :channel
33
alias FullstackWeb.Presence
4+
require Logger
45
# intercept(["user_joined"])
56

67
@impl true
@@ -28,10 +29,19 @@ defmodule FullstackWeb.GroupChannel do
2829
# Channels can be used in a request/response fashion
2930
# by sending replies to requests from the client
3031
@impl true
31-
def handle_in("ping", _payload, socket) do
32+
def handle_in("ping", payload, socket) do
33+
Logger.info("Ping from #{inspect(payload)}")
3234
{:reply, {:ok, %{:ping => :pong}}, socket}
3335
end
3436

37+
@impl true
38+
def handle_in("get_metrics", payload, socket) do
39+
Logger.info("Ping from #{inspect(payload)}")
40+
41+
{:reply, {:ok, %{:metrics => <<104, 101, 108, 108, 111, 45, 119, 111, 114, 108, 100>>}},
42+
socket}
43+
end
44+
3545
# It is also common to receive messages from the client and
3646
# broadcast to everyone in the current topic (group:lobby).
3747
@impl true

0 commit comments

Comments
 (0)