From 7d0009d83c9b5c747d4adb535817924762429f30 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 26 Sep 2018 15:26:06 +0200 Subject: Make it work, including Web interface --- shardweb/assets/js/app.js | 3 +- shardweb/lib/shard_web/channels/room_channel.ex | 37 +++++++++++++++------- .../lib/shard_web/controllers/room_controller.ex | 15 ++++++++- .../lib/shard_web/templates/room/show.html.eex | 13 +++----- 4 files changed, 45 insertions(+), 23 deletions(-) (limited to 'shardweb') diff --git a/shardweb/assets/js/app.js b/shardweb/assets/js/app.js index 59a0acd..c98ec1b 100644 --- a/shardweb/assets/js/app.js +++ b/shardweb/assets/js/app.js @@ -28,7 +28,7 @@ if (room_name != undefined) channel.on('shout', function (payload) { // listen to the 'shout' event var li = document.createElement("li"); // creaet new list item DOM element var name = payload.name || 'guest'; // get name from payload or set default - li.innerHTML = '' + name + ': ' + payload.message; // set li contents + li.innerHTML = '' + name + ' ' + payload.pk16 + ': ' + payload.message; // set li contents console.log(ul.scrollTop + ' ' + ul.scrollHeight + ' ' + ul.clientHeight); var must_scroll = (ul.scrollTop >= ul.scrollHeight - ul.clientHeight - 10); @@ -49,7 +49,6 @@ if (room_name != undefined) msg.addEventListener('keypress', function (event) { if (event.keyCode == 13 && msg.value.length > 0) { // don't sent empty msg. channel.push('shout', { // send the message to the server on "shout" channel - name: name.value, // get value of "name" of person sending the message message: msg.value // get message text (value) from msg input field. }); msg.value = ''; // reset the message input field for next message. diff --git a/shardweb/lib/shard_web/channels/room_channel.ex b/shardweb/lib/shard_web/channels/room_channel.ex index bb41a74..2935c45 100644 --- a/shardweb/lib/shard_web/channels/room_channel.ex +++ b/shardweb/lib/shard_web/channels/room_channel.ex @@ -6,17 +6,17 @@ defmodule ShardWeb.RoomChannel do def join("room:" <> room_name, payload, socket) do if authorized?(payload) do - list = for {_chid, manifest, chpid} <- Shard.Manager.list_shards, - %SApp.Chat.Manifest{channel: chan} = manifest, - do: {chan, chpid} + list = for {_chid, %SApp.Chat.Manifest{channel: chan}, chpid} <- Shard.Manager.list_shards, + do: {chan, chpid} pid = case List.keyfind(list, room_name, 0) do nil -> - {:ok, pid} = DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SApp.Chat, room_name}) + {:ok, pid} = Shard.Manifest.start %SApp.Chat.Manifest{channel: room_name} pid {_, pid} -> pid end socket = assign(socket, :pid, pid) + socket = assign(socket, :pk, Shard.Keys.get_any_identity) # TODO same as in session! GenServer.cast(pid, {:subscribe, self()}) send(self(), :after_join) @@ -29,16 +29,25 @@ defmodule ShardWeb.RoomChannel do def handle_info(:after_join, socket) do GenServer.call(socket.assigns.pid, {:read_history, nil, 100}) - |> Enum.each(fn {{_ts, nick, msg}, true} -> push(socket, "shout", %{ - name: nick, - message: msg, - }) end) + |> Enum.each(fn {{pk, msgbin, _sign}, true} -> + {_ts, msg} = SData.term_unbin msgbin + nick = SApp.Identity.get_nick pk + push(socket, "shout", %{ + name: nick, + pk16: Shard.Keys.pk_display(pk), + message: msg, + }) + end) {:noreply, socket} end - def handle_info({:chat_recv, _chan, {_ts, from, msg}}, socket) do + def handle_info({:chat_recv, _chan, {pk, msgbin, _sign}}, socket) do + {_ts, msg} = SData.term_unbin msgbin + nick = SApp.Identity.get_nick pk Logger.info("#{inspect self()} :chat_recv #{inspect msg}") - push socket, "shout", %{"name" => from, "message" => msg} + push socket, "shout", %{"name" => nick, + "pk16" => Shard.Keys.pk_display(pk), + "message" => msg} {:noreply, socket} end @@ -55,9 +64,13 @@ defmodule ShardWeb.RoomChannel do # It is also common to receive messages from the client and # broadcast to everyone in the current topic (room:lobby). def handle_in("shout", payload, socket) do + pk = socket.assigns.pk + nick = SApp.Identity.get_nick pk + payload = Map.put(payload, "name", nick) + payload = Map.put(payload, "pk16", Shard.Keys.pk_display pk) + broadcast socket, "shout", payload - Shard.Identity.set_nickname(payload["name"]) - GenServer.cast(socket.assigns.pid, {:chat_send, payload["message"]}) + GenServer.cast(socket.assigns.pid, {:chat_send, pk, payload["message"]}) {:noreply, socket} end diff --git a/shardweb/lib/shard_web/controllers/room_controller.ex b/shardweb/lib/shard_web/controllers/room_controller.ex index 4d9adb4..48ba2a4 100644 --- a/shardweb/lib/shard_web/controllers/room_controller.ex +++ b/shardweb/lib/shard_web/controllers/room_controller.ex @@ -1,12 +1,25 @@ defmodule ShardWeb.RoomController do use ShardWeb, :controller + require Logger import PhoenixGon.Controller def show(conn, %{"room" => room}) do + {pk, conn} = case get_session(conn, :pk) do + nil -> + pk = Shard.Keys.get_any_identity + conn = put_session(conn, :pk, pk) + {pk, conn} + x -> + {x, conn} + end + + name = SApp.Identity.get_nick pk + conn = put_gon(conn, chat_room: room) render conn, "show.html", room: room, - name: Shard.Identity.get_nickname + pk: pk, + name: name end end diff --git a/shardweb/lib/shard_web/templates/room/show.html.eex b/shardweb/lib/shard_web/templates/room/show.html.eex index 5f28cc5..bc1641a 100644 --- a/shardweb/lib/shard_web/templates/room/show.html.eex +++ b/shardweb/lib/shard_web/templates/room/show.html.eex @@ -1,11 +1,8 @@