From 033d07a519bac03fdfd28ca15e09cc287e80fd14 Mon Sep 17 00:00:00 2001 From: neodarz Date: Tue, 24 Jan 2017 17:09:18 +0100 Subject: Initial commit with non-functional pieces of code about file upload --- web/static/js/app.js | 21 +++++++++++++++++ web/static/js/socket.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 web/static/js/app.js create mode 100644 web/static/js/socket.js (limited to 'web/static/js') diff --git a/web/static/js/app.js b/web/static/js/app.js new file mode 100644 index 0000000..e7549b9 --- /dev/null +++ b/web/static/js/app.js @@ -0,0 +1,21 @@ +// 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" diff --git a/web/static/js/socket.js b/web/static/js/socket.js new file mode 100644 index 0000000..0f8d461 --- /dev/null +++ b/web/static/js/socket.js @@ -0,0 +1,62 @@ +// NOTE: The contents of this file will only be executed if +// you uncomment its entry in "web/static/js/app.js". + +// To use Phoenix channels, the first step is to import Socket +// and connect at the socket path in "lib/my_app/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 "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 "web/templates/layout/app.html.eex": +// +// +// +// You will need to verify the user token in the "connect/2" function +// in "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 -- cgit v1.2.1