wslでelixir その60

# 概要
wsl(wsl2じゃない)で、elixirやってみた。
練習問題、やってみた。

# 練習問題
Phoenixで、WebSocketを実装せよ。

# 方針
phx.gen.Channel 使う。

# 手順

- phx.gen.Channelを実行

```
$ mix phx.gen.channel Room
* creating lib/hello2_web/channels/room_channel.ex
* creating test/hello2_web/channels/room_channel_test.exs
* creating test/support/channel_case.ex

The default socket handler - Hello2Web.UserSocket - was not found.

Do you want to create it? [Yn] y
* creating lib/hello2_web/channels/user_socket.ex
* creating assets/js/user_socket.js

Add the socket handler to your `lib/hello2_web/endpoint.ex`, for example:

		socket "/socket", Hello2Web.UserSocket,
			websocket: true,
			longpoll: false

For the front-end integration, you need to import the `user_socket.js`
in your `assets/js/app.js` file:

		import "./user_socket.js"


```

- endpoint.exを修正

```
		socket "/socket", Hello2Web.UserSocket,
			websocket: true,
			longpoll: false

```

- assets/js/app.jsを修正

```
import "./user_socket.js"

```


- index.html.heexを修正

```
		<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/app.css")}/>
		<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js")}></script>

```

- user_socket.jsの中身

```
// NOTE: The contents of this file will only be executed if
// you uncomment its entry in "assets/js/app.js".

// Bring in Phoenix channels client library:
import {Socket} from "phoenix"

// And connect to the path in "lib/hello2_web/endpoint.ex". We pass the
// token for authentication. Read below how it should be used.
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/hello2_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/hello2_web/templates/layout/app.html.heex":
//
//     <script>window.userToken = "<%= assigns[:user_token] %>";</script>
//
// You will need to verify the user token in the "connect/3" function
// in "lib/hello2_web/channels/user_socket.ex":
//
//     def connect(%{"token" => token}, socket, _connect_info) do
//       # max_age: 1209600 is equivalent to two weeks in seconds
//       case Phoenix.Token.verify(socket, "user socket", token, max_age: 1_209_600) do
//         {:ok, user_id} ->
//           {:ok, assign(socket, :user, user_id)}
//
//         {:error, reason} ->
//           :error
//       end
//     end
//
// Finally, connect to the socket:
socket.connect()

// Now that you are connected, you can join channels with a topic.
// Let's assume you have a channel with a topic named `room` and the
// subtopic is its id - in this case 42:
let channel = socket.channel("room:42", {})
channel.join()
  .receive("ok", resp => { console.log("Joined successfully", resp) })
  .receive("error", resp => { console.log("Unable to join", resp) })

export default socket


```

以上。