2011-12-24 00:58:03 +01:00
|
|
|
%% Feel free to use, reuse and abuse the code in this file.
|
|
|
|
|
2013-04-22 15:55:22 +02:00
|
|
|
-module(ws_echo).
|
2012-08-27 12:46:42 +02:00
|
|
|
-behaviour(cowboy_websocket_handler).
|
2013-01-22 02:34:18 +01:00
|
|
|
-export([init/3]).
|
2011-12-24 00:58:03 +01:00
|
|
|
-export([websocket_init/3, websocket_handle/3,
|
|
|
|
websocket_info/3, websocket_terminate/3]).
|
|
|
|
|
|
|
|
init(_Any, _Req, _Opts) ->
|
2012-08-27 14:00:28 +02:00
|
|
|
{upgrade, protocol, cowboy_websocket}.
|
2011-12-24 00:58:03 +01:00
|
|
|
|
|
|
|
websocket_init(_TransportName, Req, _Opts) ->
|
2012-08-27 13:28:57 +02:00
|
|
|
Req2 = cowboy_req:compact(Req),
|
2011-12-24 00:58:03 +01:00
|
|
|
{ok, Req2, undefined}.
|
|
|
|
|
|
|
|
websocket_handle({text, Data}, Req, State) ->
|
|
|
|
{reply, {text, Data}, Req, State};
|
|
|
|
websocket_handle({binary, Data}, Req, State) ->
|
|
|
|
{reply, {binary, Data}, Req, State};
|
|
|
|
websocket_handle(_Frame, Req, State) ->
|
|
|
|
{ok, Req, State}.
|
|
|
|
|
|
|
|
websocket_info(_Info, Req, State) ->
|
|
|
|
{ok, Req, State}.
|
|
|
|
|
|
|
|
websocket_terminate(_Reason, _Req, _State) ->
|
|
|
|
ok.
|