socket 0.2.8

Socket handling library for Elixir

Platform: Hex

Language: Elixir

License: WTFPL

Repository: https://github.com/meh/elixir-socket

View on registry: https://hex.pm/packages/socket/

Documentation: http://hexdocs.pm/socket/0.2.8


Elixir sockets made decent

This library wraps gen_tcp, gen_udp and gen_sctp, ssl and implements websockets and socks.

Examples

defmodule HTTP do
 def get(uri) when is_binary(uri) or is_list(uri) do
 get(URI.parse(uri))
 end
 def get(%URI{host: host, port: port, path: path}) do
 sock = Socket.TCP.connect! host, port, packet: :line
 sock |> Socket.Stream.send! "GET #{path || "/"} HTTP/1.1\r\nHost: #{host}\r\n\r\n"
 [_, code, text] = Regex.run ~r"HTTP/1.1 (.*?) (.*?)\s*$", sock |> Socket.Stream.recv!
 headers = headers([], sock) |> Enum.into(%{})
 sock |> Socket.packet! :raw
 body = sock |> Socket.Stream.recv!(String.to_integer(headers["Content-Length"]))
 { { String.to_integer(code), text }, headers, body }
 end
 defp headers(acc, sock) do
 case sock |> Socket.Stream.recv! do
 "\r\n" ->
 acc
 line ->
 [_, name, value] = Regex.run ~r/^(.*?):\s*(.*?)\s*$/, line
 headers([{ name, value } | acc], sock)
 end
 end
end

Websockets

Client

socket = Socket.Web.connect! "echo.websocket.org"
socket |> Socket.Web.send! { :text, "test" }
socket |> Socket.Web.recv! # => {:text, "test"}

Server

server = Socket.Web.listen! 80
client = server |> Socket.Web.accept!
# here you can verify if you want to accept the request or not, call
# `Socket.Web.close!` if you don't want to accept it, or else call
# `Socket.Web.accept!`
client |> Socket.Web.accept!
# echo the first message
client |> Socket.Web.send!(client |> Socket.Web.recv!)
веселые картинки развлекательные гифки интресные факты смешные видео смешные истории из соцсетей

GitHub Repository

meh/elixir-socket meh/elixir-socket

Socket wrapping for Elixir.

Language: Elixir

Created: May 05, 2013 16:41

Last updated: March 12, 2015 02:02

Last pushed: March 11, 2015 19:54

Size: 983 KB

Stars: 135

Forks: 23

Watchers: 8

Open issues: 9

Top Contributors

meh. Yuki Ito Sonny Scroggin Sébastien Merle Adam Howard chameco Thomas Moulia Yurii Rashkovskii Matt Williams Adam Jones Renato Aguiar Julian Duque Matt Enlow

Releases

  • 0.2.8 - September 02, 2014 09:44
  • 0.2.7 - August 03, 2014 13:45
  • 0.2.6 - July 13, 2014 13:03
  • 0.2.5 - July 03, 2014 20:20
  • 0.2.4 - June 17, 2014 12:58
  • 0.2.3 - June 13, 2014 15:25
  • 0.2.2 - May 11, 2014 19:03
  • 0.2.1 - May 11, 2014 18:45
  • 0.2.0 - May 06, 2014 16:54

Related Projects

array 1.0.1
An elixir wrapper library for Erlang's array.
Hex - Elixir - Apache-2.0 - Updated 6 months ago - 4 stars
bencoder 0.0.7
a library to handle bencode in elixir
Hex - Elixir - MIT - Updated 3 months ago - 1 stars
cauldron 0.1.5
Web server library written in Elixir
Hex - Elixir - WTFPL - Updated 7 months ago - 47 stars
cesso 0.1.3
CSV handling library for Elixir.
Hex - Elixir - WTFPL - Updated 8 months ago - 7 stars
dexts 0.2.1
dets wrapper for Elixir
Hex - Elixir - WTFPL - Updated 8 months ago - 2 stars
веселые картинки развлекательные гифки интресные факты смешные видео смешные истории из соцсетей