出于学习目的,我创建了这个简单的 TODO 应用程序,它使用 Phoenix Channels 而不是控制器到 CRUDlists
和todos
:
https
://chandothis.herokuapp.com/
代码在这里:
https ://github.com/iacobson/chan_do_this
现在我想继续实验并将频道消息从不同的 Phoenix(甚至只是 Elixir)应用程序发送到现有的 Heroku 应用程序,假设创建一个新列表。但是在这里我遇到了麻烦。
连接到 Heroku 套接字的方法是什么?(wss://chandothis.herokuapp.com/socket/websocket?token=undefined&vsn=1.0.0) 并加入频道"lists"
更新 1
尝试使用 elixir_socket ( https://github.com/meh/elixir-socket )
{:ok, socket} = Socket.connect("wss://chandothis.herokuapp.com/socket/websocket")
{:ok,
%Socket.Web{extensions: nil, headers: %{}, key: "Zm9yayB0aGUgZG9uZ2xlcw==",
mask: true, origin: nil, path: "/socket/websocket", protocols: nil,
socket: {:sslsocket, {:gen_tcp, #Port<0.7049>, :tls_connection, :undefined},
#PID<0.372.0>}, version: 13}}
{_, _, pid} = socket.socket
send pid, {:send, %{topic: "phoenix", event: "phx_join", payload: %{}}}
但得到这个:
[info] [83, 83, 76, 58, 32, 71, 111, 116, 32, 117, 110, 101, 120, 112, 101, 99, 116, 101, 100, 32, 105, 110, 102, 111, 58, 32, [123, [[123, ['send', 44, [35, 123, [['event', ' => ', [60, 60, '"phx_join"', 62, 62]], 44, 10, [' ', [' ', 32, 32, 32], 32, [32, [' ', 32, 32, ...], ' ', 32, 32, ...], 32, [' ', 32, 32, ...], ' ', 32, 32, ...], ['payload', ' => ', '\#{}'], 44, 10, [' ', [' ', 32, 32, ...], 32, [32, ...], 32, ...], ['topic', ' => ', [60, 60, '"phoenix"', ...]]], 125]], 125], 44, 10, [32, [32, [' ', 32, 32, 32], ' ', 32, 32, 32], 32, [' ', 32, 32, 32], ' ', 32, 32, 32], 'tcp_error', 44, '#Port<0.7049>'], 125], 32, '\n']
更新 2 Erlang websocket_client ( https://github.com/sanmiguel/websocket_client )的
另一个尝试,
并使用以下示例:https://github.com/phoenixframework/phoenix/blob/master/test/support/websocket_client.exs
{:ok, socket} = Phx.WebsocketClient.start_link(self(), "wss://chandothis.herokuapp.com/socket/websocket?token=undefined&vsn=1.0.0")
Phx.WebsocketClient.join(socket, "lists", %{})
收到以下错误:
[error] State machine #PID<0.391.0> terminating
Last message in was {:send, %{event: "phx_join", payload: %{}, ref: "1", topic: "lists"}}
When State == :disconnected
Data == {:context, {:websocket_req, :wss, 'chandothis.herokuapp.com', 443, '/socket/webs`enter code here`ocket?token=undefined&vsn=1.0.0', :infinity, :undefined, 1, :undefined, {:transport, :ssl, :ssl, :ssl_closed, :ssl_error, [mode: :binary, active: true, verify: :verify_none, packet: 0]}, "mR8BigxTXTlwBgxhiE63gA==", :undefined, :undefined, :undefined, :undefined, :undefined}, {:transport, :ssl, :ssl, :ssl_closed, :ssl_error, [mode: :binary, active: true, verify: :verify_none, packet: 0]}, [], {:wss, 'chandothis.herokuapp.com', 443, '/socket/websocket?token=undefined&vsn=1.0.0'}, {Phx.WebsocketClient, %{ref: 1, sender: #PID<0.384.0>}}, "", false, 0}
Reason for termination = {:error, :disconnected}