Skip to content

Commit

Permalink
Merge pull request #3 from JasterV/refactor/set-connect-url-on-server
Browse files Browse the repository at this point in the history
refactor: set connect url on server
  • Loading branch information
JasterV authored Mar 29, 2024
2 parents a2b4376 + 7c86384 commit 5b40c34
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 30 deletions.
5 changes: 1 addition & 4 deletions assets/js/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { LiveSocket } from "phoenix_live_view";
import HubHook from "./hubHook";

const Hooks = {
HubHook: HubHook(
"ws://127.0.0.1:12345/buttplug",
"ws://192.168.1.45:12345/buttplug",
),
HubHook: HubHook(),
};

const liveSocket = new LiveSocket("/live", Socket, {
Expand Down
12 changes: 2 additions & 10 deletions assets/js/hubHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ButtplugBrowserWebsocketClientConnector,
} from "buttplug";

const Hub = (localAddress, remoteAddress) => {
const Hub = () => {
const client = new ButtplugClient("IntisyncClient");

const connect = async (view, address) => {
Expand Down Expand Up @@ -32,15 +32,7 @@ const Hub = (localAddress, remoteAddress) => {
await device.vibrate(vibration / 100.0);
});

this.handleEvent(
"local_connect",
async () => await connect(this, localAddress),
);

this.handleEvent(
"remote_connect",
async () => await connect(this, remoteAddress),
);
this.handleEvent("connect", async ({ url }) => await connect(this, url));

client.addListener("deviceadded", async (device) => {
const event = {
Expand Down
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ config :intisync, IntisyncWeb.Endpoint,
pubsub_server: Intisync.PubSub,
live_view: [signing_salt: "aSHjRP5j"]

config :intisync, IntisyncWeb.HubLive, connect_url: "ws://127.0.0.1:12345/buttplug"

# Configure esbuild (the version is required)
config :esbuild,
version: "0.17.11",
Expand Down
11 changes: 4 additions & 7 deletions lib/intisync_web/live/hub_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule IntisyncWeb.HubLive do
alias Intisync.SessionsSupervisor
alias Intisync.SessionServer

@config Application.compile_env!(:intisync, __MODULE__)

def mount(_params, _session, socket) do
socket =
socket
Expand Down Expand Up @@ -94,13 +96,8 @@ defmodule IntisyncWeb.HubLive do
# Intiface Client events #
##########################

def handle_event("local_connect", %{}, socket) do
socket = push_event(socket, "local_connect", %{})
{:noreply, socket}
end

def handle_event("remote_connect", %{}, socket) do
socket = push_event(socket, "remote_connect", %{})
def handle_event("connect", %{}, socket) do
socket = push_event(socket, "connect", %{url: @config[:connect_url]})
{:noreply, socket}
end

Expand Down
8 changes: 1 addition & 7 deletions lib/intisync_web/live/hub_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,9 @@
id="connect-buttons"
class="flex flex-col gap-6 items-center"
>
<.button id="intiface-local-connect-button" type="button" phx-click="local_connect">
<.button id="intiface-connect-button" type="button" phx-click="connect">
Connect to Intiface Central
</.button>

<p class="font-semibold">or</p>

<.button id="intiface-remote-connect-button" type="button" phx-click="remote_connect">
Connect to a remote Intiface Central
</.button>
</section>

<section :if={@intiface_client_status == :connected} id="connected-devices-section">
Expand Down
4 changes: 2 additions & 2 deletions test/intisync_web/live/hub_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ defmodule IntisyncWeb.HubLiveTest do

assert view |> element("#connect-buttons") |> has_element?()

assert view |> element("#intiface-local-connect-button") |> render_click()
assert view |> element("#intiface-connect-button") |> render_click()

assert_push_event(view, "local_connect", %{})
assert_push_event(view, "connect", %{})
end

test "A device connected event is ignored if intiface central is not connected", %{
Expand Down

0 comments on commit 5b40c34

Please sign in to comment.