All the websocket functions take a user-defined State
argument used for keeping your own state between function calls. The state is discarded when the websocket is closed.
You initialize it in the return value of websocket_init/3
, websocket_handle/3
and websocket_info/3
takes the state as argument where you can lookup the data you need. In the return value of websocket_init/3
and websocket_handle/3
you pass on the new state (or the same if no state update was needed). A sketch:
websocket_init(_TransportName, Req, _Opts) ->
MyInitialState=just_started, % The state can be any erlang term,e.g. a record
{ok, Req, MyInitialState}.
websocket_handle({text, _Msg}, Req, State) ->
NewState = case State of
just_started -> up_and_running;
_ -> still_running
end,
{ok, Req, NewState}.
persist
. Persisting over the lifetime of the socket connection, or does the state has to go to a database somewhere? – I GIVE CRAP ANSWERS