Git Product home page Git Product logo

websocket_client's Introduction

Erlang Websocket Client

Build Status

Coverage Status

Existing features

  1. Client to Server Masking
  2. OTP compliant
  3. Callback-driven behaviour
  4. Handshake validation
  5. TCP and SSL support
  6. Handling of text, binary, ping, pong, and close frames
  7. Handling of continuation frames
  8. Automated ping/pong and keepalive

Usage

For basic usage, see examples/sample_ws_handler.erl:

-module(sample_ws_handler).

-behaviour(websocket_client).

-export([
         start_link/0,
         init/1,
         onconnect/2,
         ondisconnect/2,
         websocket_handle/3,
         websocket_info/3,
         websocket_terminate/3
        ]).

start_link() ->
    crypto:start(),
    ssl:start(),
    websocket_client:start_link("wss://echo.websocket.org", ?MODULE, []).

init([]) ->
    {once, 2}.

onconnect(_WSReq, State) ->
    websocket_client:cast(self(), {text, <<"message 1">>}),
    {ok, State}.

ondisconnect({remote, closed}, State) ->
    {reconnect, State}.

websocket_handle({pong, _}, _ConnState, State) ->
    {ok, State};
websocket_handle({text, Msg}, _ConnState, 5) ->
    io:format("Received msg ~p~n", [Msg]),
    {close, <<>>, "done"};
websocket_handle({text, Msg}, _ConnState, State) ->
    io:format("Received msg ~p~n", [Msg]),
    timer:sleep(1000),
    BinInt = list_to_binary(integer_to_list(State)),
    {reply, {text, <<"hello, this is message #", BinInt/binary >>}, State + 1}.

websocket_info(start, _ConnState, State) ->
    {reply, {text, <<"erlang message received">>}, State}.

websocket_terminate(Reason, _ConnState, State) ->
    io:format("Websocket closed in state ~p wih reason ~p~n",
              [State, Reason]),
    ok.

The above code will send messages to the echo server that count up from 1 through 4. It will also print all replies from the server:

Received msg <<"this is message 1">>
Received msg <<"hello, this is message #2">>
Received msg <<"hello, this is message #3">>
Received msg <<"hello, this is message #4">>

This client implements a cowboy like websocket_client_handler to interact with a websocket server. Currently, it can connect via tcp or ssl via the ws and wss protocols. It can also send and receive contiguous text or binary websocket frames.

TODO

The client has been significantly reworked, now backed by gen_statem. There may still be bugs. Please report them.

  1. Stop using verify_none by default
  2. Add more complete testing - preferably based on / using Autobahn.

websocket_client's People

Contributors

aeden avatar edgurgel avatar enewhuis avatar fhunleth avatar flaviogrossi avatar hanssv avatar idorobots avatar inou avatar jcomellas avatar jeremyong avatar jjcarstens avatar lanastasov avatar maximvl avatar maxlapshin avatar mihawk avatar puzza007 avatar sanmiguel avatar seanhinde avatar tholschuh avatar unrealhoang avatar velzevur avatar ypaq avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.