A WebSocket client written in Erlang
- 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
andon_error
.
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.
-
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.
-
Add a callback for received messages,
3>wsecli:on_message(fun(text, Message)-> io:format("Echoed message: ~s ~n", [Message]) end).
-
Send a message that will be echoed,
4> wsecli:send("Hello"). ok Echoed message: Hello
-
And finally to stop it
5>wsecli:stop().
Callbacks for the events: on_open, on_error, on_message and on_close can be added. Check the code or the documentation for details.
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/
- Support streaming of data.
- Support subprotocol and extensions.
###Author Farruco Sanjurjo. You can contact me at:
- Twitter @madtrick
- Mail [email protected]
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.
If you find or think that something isn't working properly, just open an issue.
Pull requests and patches (with tests) are welcome.