Skip to content

madtrick/wsecli

Repository files navigation

Analytics Build Status

wsecli

A WebSocket client written in Erlang

Features

  • Built using wsock
  • Supports both "ws://" and "wss://" schemes.
  • Offers callbacks for events that might occur during the client life: on_open, on_close, on_message and on_error.

Supported protocol versions

Only the protocol specificied at RFC6455 (version 13) is supported. Notice that currently, neither subprotocols nor extensions are supported.

Build ###

Add this repo as a dependency to your rebar.config file

{wsecli, ".*", {git, "https://github.com/madtrick/wsecli", {tag, master}}}

and then

./rebar compile

Usage ###

I will demostrate its usage with the echo service at www.websocket.org.

  1. Start it, using the function wsecli:start/4

    1>wsecli:start("echo.websocket.org", 80, "/", []).

    or the function wsecli:start/2

    2>wsecli:start("ws://echo.websocket.org/", []).

    The valid options for the client are:

    • {registered, true | false | atom()}. When true the client will be registered using the default name wsecli, when false it will not be registered and any other atom will be used as the name to register with. By default it will not be registered.
  2. Add a callback for received messages,

    3>wsecli:on_message(fun(text, Message)-> io:format("Echoed message: ~s ~n", [Message]) end).
  3. Send a message that will be echoed,

    4> wsecli:send("Hello").
    ok
    Echoed message: Hello
  4. And finally to stop it

    5>wsecli:stop().

Callbacks

Callbacks for the events: on_open, on_error, on_message and on_close can be added. Check the code or the documentation for details.

Tests

Unit tests

Unit test where done with the library espec by lucaspiller.

To run them

rake spec

or, in case you don't have rake installed,

./rebar compile && ERL_LIBS='deps/' ./espec test/spec/

TODO

  • Support streaming of data.
  • Support subprotocol and extensions.

###Author Farruco Sanjurjo. You can contact me at:

License

Copyright [2013] [Farruco Sanjurjo Arcay]

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Contribute

If you find or think that something isn't working properly, just open an issue.

Pull requests and patches (with tests) are welcome.