Commit cf7afc10 authored by Jens-Christian Fischer's avatar Jens-Christian Fischer
Browse files

Communincate with GrainsEngine

parent 79678f85
......@@ -44,6 +44,11 @@ defmodule Grains.OrientationChannel do
value: yaw
}
Grains.Endpoint.broadcast("osc", "orientation", %{user: socket.assigns.user_uuid,
pitch: pitch,
roll: roll,
yaw: yaw})
{:reply, :ok, socket}
end
......
defmodule Grains.PingChannel do
defmodule Grains.OscChannel do
use Phoenix.Channel
require Logger
def join(_topic, _payload, socket) do
Process.send_after(self(), :leave_or_crash, :rand.uniform(2000) + 2000)
{:ok, socket}
end
def handle_info(:leave_or_crash, socket) do
if :rand.uniform(5) == 1 do
Logger.warn("deliberately disconnecting the client")
Process.exit(socket.transport_pid, :kill)
{:noreply, socket}
else
Logger.warn("deliberately leaving the topic")
{:stop, :normal, socket}
end
end
def handle_info(_message, socket) do
{:noreply, socket}
end
......@@ -28,4 +17,7 @@ defmodule Grains.PingChannel do
Logger.warn("unhandled event #{event} #{inspect payload}")
{:noreply, socket}
end
end
......@@ -24,6 +24,10 @@ defmodule Grains.SliderChannel do
element: slider,
value: value
}
Grains.Endpoint.broadcast("osc", "slider", %{user: socket.assigns.user_uuid,
element: slider,
value: value})
{:reply, :ok, socket}
......
......@@ -9,7 +9,7 @@ defmodule Grains.UserSocket do
channel "presence:*", Grains.PresenceChannel
channel "ping", Grains.PingChannel
channel "osc", Grains.OscChannel
## Transports
transport :websocket, Phoenix.Transports.WebSocket
......@@ -29,18 +29,18 @@ defmodule Grains.UserSocket do
@max_age 2 * 7 * 24 * 60 * 60
def connect(%{"token" => "foobar"}, socket) do
{:ok, assign(socket, :user_uuid, "foobar")}
end
def connect(%{"token" => token}, socket) do
case Phoenix.Token.verify(socket, "user socket", token, max_age: @max_age) do
{:ok, uuid} ->
IO.puts "verified : #{uuid}"
{:ok, assign(socket, :user_uuid, uuid) }
{:error, _reason} ->
if token == "foobar" do
{:ok, assign(socket, :user_uuid, token)}
else
IO.puts "error: #{_reason}"
:error
end
IO.puts "error: #{_reason}"
:error
end
end
......
......@@ -19,7 +19,7 @@ import SliderPanel from "./slider";
import OrientationPanel from "./orientation";
SliderPanel.init(socket, "#sliderPanel", ["s1", "s2", "s3"]);
SliderPanel.init(socket, "sliderPanel", ["s1", "s2", "s3"]);
OrientationPanel.init(socket, "orientationPanel");
// Import local files
......
......@@ -8,6 +8,7 @@ let SliderPanel = {
init(socket, domId, sliders) {
if (!document.getElementById(domId)) {
console.log("No element found: " + domId);
return;
}
......
<h1>Grain <%= @grain %></h1>
<p>Slider</p>
<div id="sliderPanel" class="interfacePanel"></div>
<div id="pitch">pitch</div>
<div id="roll">roll</div>
<div id="yaw">yaw</div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment