aboutsummaryrefslogtreecommitdiff
path: root/shardweb/assets/js
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-09-01 16:07:22 +0200
committerAlex Auvolat <alex@adnab.me>2018-09-01 16:07:22 +0200
commit81b5a844a2a155e28c497a8ce671eb5f02803e5d (patch)
tree15e878ebe47d5993e7af46a48a1ccdc1dd1e4b2f /shardweb/assets/js
parentc6ec33d6e612168e14d77007915a4ea423c55a2e (diff)
downloadshard-81b5a844a2a155e28c497a8ce671eb5f02803e5d.tar.gz
shard-81b5a844a2a155e28c497a8ce671eb5f02803e5d.zip
Import shardweb0.0.2
Diffstat (limited to 'shardweb/assets/js')
-rw-r--r--shardweb/assets/js/app.js58
-rw-r--r--shardweb/assets/js/socket.js62
2 files changed, 120 insertions, 0 deletions
diff --git a/shardweb/assets/js/app.js b/shardweb/assets/js/app.js
new file mode 100644
index 0000000..59a0acd
--- /dev/null
+++ b/shardweb/assets/js/app.js
@@ -0,0 +1,58 @@
+// Brunch automatically concatenates all files in your
+// watched paths. Those paths can be configured at
+// config.paths.watched in "brunch-config.js".
+//
+// However, those files will only be executed if
+// explicitly imported. The only exception are files
+// in vendor, which are never wrapped in imports and
+// therefore are always executed.
+
+// Import dependencies
+//
+// If you no longer want to use a dependency, remember
+// to also remove its path from "config.paths.watched".
+import "phoenix_html"
+
+// Import local files
+//
+// Local files can be imported directly using relative
+// paths "./socket" or full ones "web/static/js/socket".
+
+import socket from "./socket"
+
+var room_name = window.Gon.getAsset('chat_room');
+if (room_name != undefined)
+{
+ var channel = socket.channel('room:' + room_name, {}); // connect to chat "room"
+
+ 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
+
+ console.log(ul.scrollTop + ' ' + ul.scrollHeight + ' ' + ul.clientHeight);
+ var must_scroll = (ul.scrollTop >= ul.scrollHeight - ul.clientHeight - 10);
+
+ ul.appendChild(li); // append to list
+
+ if (must_scroll) ul.scrollTop = ul.scrollHeight;
+ });
+
+ channel.join(); // join the channel.
+
+
+ var ul = document.getElementById('msg-list'); // list of messages.
+ var name = document.getElementById('name'); // name of message sender
+ var msg = document.getElementById('msg'); // message input field
+
+ // "listen" for the [Enter] keypress event to send a message:
+ 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/assets/js/socket.js b/shardweb/assets/js/socket.js
new file mode 100644
index 0000000..5088721
--- /dev/null
+++ b/shardweb/assets/js/socket.js
@@ -0,0 +1,62 @@
+// NOTE: The contents of this file will only be executed if
+// you uncomment its entry in "assets/js/app.js".
+
+// To use Phoenix channels, the first step is to import Socket
+// and connect at the socket path in "lib/web/endpoint.ex":
+import {Socket} from "phoenix"
+
+let socket = new Socket("/socket", {params: {token: window.userToken}})
+
+// When you connect, you'll often need to authenticate the client.
+// For example, imagine you have an authentication plug, `MyAuth`,
+// which authenticates the session and assigns a `:current_user`.
+// If the current user exists you can assign the user's token in
+// the connection for use in the layout.
+//
+// In your "lib/web/router.ex":
+//
+// pipeline :browser do
+// ...
+// plug MyAuth
+// plug :put_user_token
+// end
+//
+// defp put_user_token(conn, _) do
+// if current_user = conn.assigns[:current_user] do
+// token = Phoenix.Token.sign(conn, "user socket", current_user.id)
+// assign(conn, :user_token, token)
+// else
+// conn
+// end
+// end
+//
+// Now you need to pass this token to JavaScript. You can do so
+// inside a script tag in "lib/web/templates/layout/app.html.eex":
+//
+// <script>window.userToken = "<%= assigns[:user_token] %>";</script>
+//
+// You will need to verify the user token in the "connect/2" function
+// in "lib/web/channels/user_socket.ex":
+//
+// def connect(%{"token" => token}, socket) do
+// # max_age: 1209600 is equivalent to two weeks in seconds
+// case Phoenix.Token.verify(socket, "user socket", token, max_age: 1209600) do
+// {:ok, user_id} ->
+// {:ok, assign(socket, :user, user_id)}
+// {:error, reason} ->
+// :error
+// end
+// end
+//
+// Finally, pass the token on connect as below. Or remove it
+// from connect if you don't care about authentication.
+
+socket.connect()
+
+// Now that you are connected, you can join channels with a topic:
+// let channel = socket.channel("topic:subtopic", {})
+// channel.join()
+// .receive("ok", resp => { console.log("Joined successfully", resp) })
+// .receive("error", resp => { console.log("Unable to join", resp) })
+
+export default socket