Created
September 2, 2014 00:30
-
-
Save mk270/a01d12178201a33475c3 to your computer and use it in GitHub Desktop.
Websocket handler attempt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(ws_handler). | |
-behaviour(cowboy_websocket_handler). | |
-export([init/3]). | |
-export([websocket_init/3]). | |
-export([websocket_handle/3]). | |
-export([websocket_info/3]). | |
-export([websocket_terminate/3]). | |
init({tcp, http}, _Req, _Opts) -> | |
{upgrade, protocol, cowboy_websocket}. | |
websocket_init(_TransportName, Req, _Opts) -> | |
io:format("INIT: ~p~n", [self()]), | |
{ok, Pid} = lobby_fsm:start_link(), | |
{ok, Req, {fsm_pid, Pid}}. | |
websocket_handle({text, Msg}, Req, State) -> | |
Msg_string = binary_to_list(Msg), | |
{fsm_pid, Pid} = State, | |
case lobby_fsm:command(Pid, Msg_string) of | |
{ok, Text} -> | |
Text_binary = list_to_binary(Text), | |
{reply, {text, << "Answer ", Text_binary/binary >>}, Req, State}; | |
_ -> {reply, {text, << "ERROR">>}, Req, State} | |
end; | |
websocket_handle(_Data, Req, State) -> | |
{ok, Req, State}. | |
websocket_info({timeout, _Ref, Msg}, Req, State) -> | |
{reply, {text, Msg}, Req, State}; | |
websocket_info(_Info, Req, State) -> | |
{ok, Req, State}. | |
websocket_terminate(_Reason, _Req, _State) -> | |
ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment