All Projects → meh → Elixir Socket

meh / Elixir Socket

Socket wrapping for Elixir.

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to Elixir Socket

Yasio
A multi-platform support c++11 library with focus on asio (asynchronous socket I/O) for any client application.
Stars: ✭ 483 (-24.77%)
Mutual labels:  tcp, socket, udp, ssl
T Io
解决其它网络框架没有解决的用户痛点,让天下没有难开发的网络程序
Stars: ✭ 1,331 (+107.32%)
Mutual labels:  websocket, tcp, socket, udp
Beetlex
high performance dotnet core socket tcp communication components, support TLS, HTTP, HTTPS, WebSocket, RPC, Redis protocols, custom protocols and 1M connections problem solution
Stars: ✭ 802 (+24.92%)
Mutual labels:  websocket, tcp, socket, ssl
Asio2
Header only c++ network library, based on asio,support tcp,udp,http,websocket,rpc,ssl,icmp,serial_port.
Stars: ✭ 202 (-68.54%)
Mutual labels:  websocket, tcp, udp, ssl
Bigq
Messaging platform in C# for TCP and Websockets, with or without SSL
Stars: ✭ 18 (-97.2%)
Mutual labels:  websocket, tcp, socket, ssl
Hp Socket
High Performance TCP/UDP/HTTP Communication Component
Stars: ✭ 4,420 (+588.47%)
Mutual labels:  tcp, socket, udp, ssl
Swiddler
TCP/UDP debugging tool.
Stars: ✭ 56 (-91.28%)
Mutual labels:  ssl, tcp, udp
ronin-support
A support library for Ronin. Like activesupport, but for hacking!
Stars: ✭ 23 (-96.42%)
Mutual labels:  ssl, tcp, udp
RRQMSocket
TouchSocket是.Net(包括 C# 、VB.Net、F#)的一个整合性的、超轻量级的网络通信框架。包含了 tcp、udp、ssl、http、websocket、rpc、jsonrpc、webapi、xmlrpc等一系列的通信模块。一键式解决 TCP 黏分包问题,udp大数据包分片组合问题等。使用协议模板,可快速实现「固定包头」、「固定长度」、「区间字符」等一系列的数据报文解析。
Stars: ✭ 286 (-55.45%)
Mutual labels:  socket, tcp, udp
socket
Dazzle Async Socket
Stars: ✭ 19 (-97.04%)
Mutual labels:  socket, tcp, udp
AndroidNetMonitor
This project aims to collect and analyze traffic information of Android.(采集手机发送和接收的报文简要信息,并且根据socket记录每个报文对应哪个手机app)
Stars: ✭ 25 (-96.11%)
Mutual labels:  socket, tcp, udp
KingNetwork
KingNetwork is an open source library to facilitate the creation and communication of clients and servers via TCP, UDP, WebSocket and RUDP sockets.
Stars: ✭ 78 (-87.85%)
Mutual labels:  socket, tcp, udp
Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (-89.56%)
Mutual labels:  socket, tcp, udp
Blinksocks
A framework for building composable proxy protocol stack.
Stars: ✭ 587 (-8.57%)
Mutual labels:  websocket, tcp, udp
gnb udp over tcp
gnb_udp_over_tcp 是一个为GNB开发的通过tcp链路中转UDP分组转发的服务
Stars: ✭ 32 (-95.02%)
Mutual labels:  socket, tcp, udp
XAsyncSockets
XAsyncSockets is an efficient Python/MicroPython library of managed asynchronous sockets.
Stars: ✭ 28 (-95.64%)
Mutual labels:  ssl, tcp, udp
Socket
The Hoa\Socket library.
Stars: ✭ 61 (-90.5%)
Mutual labels:  socket, tcp, udp
Netcat
💻 Netcat client and server modules written in pure Javascript for Node.js.
Stars: ✭ 315 (-50.93%)
Mutual labels:  tcp, socket, udp
ddos
Simple dos attack utility
Stars: ✭ 36 (-94.39%)
Mutual labels:  socket, tcp, udp
Laravel S
LaravelS is an out-of-the-box adapter between Swoole and Laravel/Lumen.
Stars: ✭ 3,479 (+441.9%)
Mutual labels:  websocket, tcp, udp

Elixir sockets made decent

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

Installation

In your mix.exs file

defp deps do
  [
    # ...
    {:socket, "~> 0.3"},
    # ...
  ]
end

Then run mix deps.get to install

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"}

In order to connect to a TLS websocket, use the secure: true option:

socket = Socket.Web.connect! "echo.websocket.org", secure: true

The connect! function also accepts other parameters, most notably the path parameter, which is used when the websocket server endpoint exists on a path below the domain ie. "example.com/websocket":

socket = Socket.Web.connect! "example.com", path: "/websocket"

Note that websocket servers send ping messages. A pong reply from your client tells the server to keep the connection open and to send more data. If your client doesn't send a pong reply then the server will close the connection. Here's an example of how to get get both the data you want and reply to a server's pings:

socket = Socket.Web.connect! "echo.websocket.org"
case socket |> Socket.Web.recv! do
  {:text, data} ->
    # process data
  {:ping, _ } ->
    socket |> Socket.Web.send!({:pong, ""})
end

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!)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].