【问题标题】:Connect to TCP clients Elixir连接 TCP 客户端 Elixir
【发布时间】:2017-04-25 09:30:49
【问题描述】:

尝试连接到 TCP 服务器,发送命令并获得响应。

我在 Ruby 中有类似的东西

TCPSocket.open("127.0.0.1", 3344)
 s.send(JSON.dump({"id" => 1, "method" => "Responder.Status", "params" => [""]}),0)

任何指向可以在 Elixir 中实现类似(或更强大)功能的文档/库的指针?

【问题讨论】:

标签: erlang elixir


【解决方案1】:

对于TCP连接,可以使用erlang标准库gen_tcp

{:ok, socket} = :gen_tcp.connect('localhost', 6379, [:binary])
data = {"id" => 1, "method" => "Responder.Status", "params" => [""]}
:ok = :gen_tcp.send(socket, Poison.encode!(data))
:ok = :gen_tcp.close(socket)

【讨论】:

  • 谢谢。更新了我的问题。我将如何实现这样的目标。试过 Poison.encode ,但出错了……
  • gen_tcp.send(socket, Poison.encode(%{id: 1, method: "Responder.Status"}) |> elem(1)) 应该可以工作。
  • 或者就像回复的OP建议使用Poison.encode!
猜你喜欢
  • 2010-11-18
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
  • 2016-03-31
相关资源
最近更新 更多