diff options
Diffstat (limited to 'shardweb')
-rw-r--r-- | shardweb/assets/js/app.js | 3 | ||||
-rw-r--r-- | shardweb/lib/shard_web/channels/room_channel.ex | 37 | ||||
-rw-r--r-- | shardweb/lib/shard_web/controllers/room_controller.ex | 15 | ||||
-rw-r--r-- | shardweb/lib/shard_web/templates/room/show.html.eex | 13 |
4 files changed, 45 insertions, 23 deletions
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 = '<b>' + name + '</b>: ' + payload.message; // set li contents + li.innerHTML = '<b>' + name + '</b> <small>' + payload.pk16 + '</small>: ' + 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 @@ <ul class="nav nav-tabs"> - <%= for shard <- shard_list() do %> - <%= case shard do %> - <%= {_, %SApp.Chat.Manifest{channel: name}, _} -> %> - <li class="<%= if name == @room do "active" else "" end %>"> - <a href="<%= room_path(@conn, :show, name) %>">#<%= name %></a> - </li> - <% end %> + <%= for {_, %SApp.Chat.Manifest{channel: name}, _} <- shard_list() do %> + <li class="<%= if name == @room do "active" else "" end %>"> + <a href="<%= room_path(@conn, :show, name) %>">#<%= name %></a> + </li> <% end %> <li> <a href="#" onclick="if(new_room=prompt('Enter name of room to join, without preceding # sign:'))window.location.href='/room/'+new_room;">Join room</a> @@ -17,7 +14,7 @@ <div class="row"> <div class="col-xs-3"> - <input type="text" value="<%= @name %>" id="name" class="form-control" placeholder="Your Name"> + <strong><%= @name %></strong> </div> <div class="col-xs-9"> <input type="text" id="msg" class="form-control" placeholder="Your Message" autofocus> |