• Stars
    star
    183
  • Rank 202,823 (Top 5 %)
  • Language
    Erlang
  • License
    MIT License
  • Created about 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Erlang websocket client (ws and wss supported)

Erlang Websocket Client

Build Status

Existing features

  1. Client to Server Masking
  2. gen_server like callback behaviour
  3. Handshake validation
  4. tcp and ssl support
  5. Handling of text, binary, ping, pong, and close frames
  6. Handling of continuation frames

Usage

For basic usage, please see the source files in the examples/ directory. Writing a handler is easy:

-module(sample_ws_handler).

-behaviour(websocket_client_handler).

-export([
         start_link/0,
         init/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([], _ConnState) ->
    websocket_client:cast(self(), {text, <<"message 1">>}),
    {ok, 2}.

websocket_handle({pong, _}, _ConnState, State) ->
    {ok, State};
websocket_handle({text, Msg}, _ConnState, 5) ->
    io:format("Received msg ~p~n", [Msg]),
    {close, <<>>, 10};
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({close, Code, Payload}, _ConnState, State) ->
    io:format("Websocket closed in state ~p wih code ~p and payload ~p~n",
              [State, Code, Payload]),
    ok.

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

Received msg <<"this is message 1">>
Received msg <<"hello, this is message #3">>
Received msg <<"hello, this is message #4">>
Received msg <<"hello, this is message #5">>
Received msg <<"hello, this is message #6">>
...

Erlang is typically used to write server applications. Now that applications like cowboy supporting websocket applications are more commonplace, it is important to have a compliant websocket client for benchmarking and debugging purposes.

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 as is is still missing a lot of functionality. We still need to:

  1. Close the connection in a number of error cases (malformed headers, etc).
  2. Add tests!! (hint hint)
  3. Stop using verify_none by default

This is being released without the above functionality in case it is useful as is for benchmarking or debugging as mentioned above (this is the case for me). The hope is that the community (and me, as time permits) will contribute key pieces to the codebase so that the major pieces are taken care of.

More Repositories

1

Selene

Simple C++11 friendly header-only bindings to Lua
C++
812
star
2

klein

P(R*_{3, 0, 1}) specialized SIMD Geometric Algebra Library
C++
717
star
3

cpp_nn_in_a_weekend

Article and source code reference to construct a C++ neural network in a weekend without any dependencies
C++
145
star
4

coop

C++20 coroutines-based cooperative multitasking library
C++
97
star
5

gal

Geometric Algebra Library
C++
89
star
6

paperbug

An indexed compendium of graphics programming papers, articles, blog posts, presentations, and more
JavaScript
69
star
7

flop

FLOꟼ - An MIT-licensed image viewer equipped with a GPU-accelerated perceptual image diffing algorithm based on ꟻLIP
C++
58
star
8

opencl_in_action

Corrected source for the OpenCL in Action book (work in progress)
C
58
star
9

SDLpp

Lightweight C++11 bindings to SDL2
TeX
36
star
10

mc_ruler

Seamless llvm-mca CMake integration
CMake
25
star
11

spool

A C and C++ string pooling CMake-integrated preprocessor
C++
22
star
12

exprotoc

Elixir Protocol Buffers Compiler
Elixir
22
star
13

sharded_eredis

Erlang
20
star
14

eprotoc

Erlang proto file parser and code generator
Erlang
18
star
15

learningit

An overview of my git workflow. Feel free to suggest changes or fork your own version.
3
star
16

Yaiba

Yet Another Implementation of Buchberger's Algorithm
Haskell
2
star
17

ninepoints

Code snippets
C++
2
star
18

jeremyong.github.com

CSS
2
star
19

google-coredumper

Automatically exported from code.google.com/p/google-coredumper
Shell
1
star
20

o3de-extension

C
1
star
21

crdts_in_production

1
star
22

simple_chat_server_tutorial

Erlang
1
star
23

treebook

Ruby
1
star
24

simpleipc

Simple interprocess communication for C++ programs
C++
1
star