cowboy實現websocket

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   資料   

使用cowboy實現websocket主要實現以下回呼函數 下面的函數傳回值要具體弄清楚原因參考 websocket具體協議  主要就是兩個部分 握手和資料轉送
  -export([init/3]). 常見所有處理常式回調。建立WebSocket串連,這個函數必須返回 upgrade 的元組。  -export([websocket_init/3]). 初始socket狀態,也可以用於註冊的過程,啟動一個定時器,等返回參考doc.然後和WebSocket握手。  -export([websocket_handle/3]).處理用戶端發送的對應的訊息。返回格式 參考cow對應doc  -export([websocket_info/3]). 處理erlang之間派發的訊息  -export([websocket_terminate/3]). websocket 斷開 用於掃尾工作處理我在這裡還是採用otp方式構建整個項目 主要代碼 以及效果
-module(websocket_app).-behaviour(application).%% Application callbacks-export([start/2, stop/1]).%% ===================================================================%% Application callbacks%% ===================================================================start(_StartType, _StartArgs) ->    %%啟動儲存pid的樹據 可以採用 ets 表格處理 但是為了方便叢集處理 我採用的mnesia    ok = websocket_store:init(),    %% 配置路由    Dispatch = cowboy_router:compile([                      {‘_‘, [                         {"/", cowboy_static, {priv_file, websocket, "index.html"}},                         {"/websocket", websocket_hander, []},                         {"/static/[...]", cowboy_static, {priv_dir,  websocket, "static"}}                        ]}                     ]),    {ok, _} = cowboy:start_http(http, 100, [{port, 8080}],                [{env, [{dispatch, Dispatch}]}]),    %%啟動監督樹    websocket_sup:start_link().stop(_State) ->    ok.
-module(websocket_sup).-behaviour(supervisor).%% API-export([start_link/0]).%% Supervisor callbacks-export([init/1]).%% Helper macro for declaring children of supervisor-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 2000, Type, [I]}).%% ===================================================================%% API functions%% ===================================================================start_link() ->    supervisor:start_link({local, ?MODULE}, ?MODULE, []).%% ===================================================================%% Supervisor callbacks%% ===================================================================init([]) ->    Children = [],    {ok, { {one_for_one, 5, 10}, Children} }.
View Code
-module(websocket_hander).-behaviour(cowboy_websocket_handler).%% ------------------------------------------------------------------%% API Function Exports%% ------------------------------------------------------------------%% ------------------------------------------------------------------%% gen_server Function Exports%% -------------------------------------------------------------------export([init/3]).-export([websocket_init/3]).-export([websocket_handle/3]).-export([websocket_info/3]).-export([websocket_terminate/3]).%% ------------------------------------------------------------------%% API Function Definitions%% ------------------------------------------------------------------%% ------------------------------------------------------------------%% gen_server Function Definitions%% ------------------------------------------------------------------%%websocket 握手init({tcp, http}, _Req, _Opts) ->    %%插入資料    websocket_store:dirty_insert(self(),web),    {upgrade, protocol, cowboy_websocket}.%%串連初始websocket_init(_TransportName, Req, _Opts) ->    %%串連一秒後 發送訊息    erlang:start_timer(1000, self(), <<"Hello!">>),    {ok, Req, undefined_state}.%%處理用戶端發送投遞的訊息websocket_handle({text, Msg}, Req, State) ->    %% 把訊息投遞到其餘的進程 這裡可以 投遞到 tcp socket  也可以投遞的 websocket 這樣就可以實現 服務支援多協議處理     lists:foreach(fun([Pid,SocketType]) ->               case SocketType of                  tcp -> ok;                      web ->                  case Pid == self() of                      true -> ok;                      false -> Pid!{chat,Msg}                  end                                            end          end,websocket_store:dirty_lookall()),    {reply, {text, << "That‘s what she said! ", Msg/binary >>}, Req, State};websocket_handle(_Data, Req, State) ->    {ok, Req, State}.%% 處理erlang 發送的訊息 這裡是接收 46 行發送的資料 websocket_info({chat,Msg},_Req,State)->    {reply,{text,Msg},_Req,State};%% 處理     erlang:start_timer(1000, self(), <<"Hello!">>) 發送的訊息websocket_info({timeout, _Ref, Msg}, Req, State) ->    {reply, {text, Msg}, Req, State};websocket_info(_Info, Req, State) ->    {ok, Req, State}.%%斷開socketwebsocket_terminate(_Reason, _Req, _State) ->    websocket_store:dirty_delete(self()),    ok.%% ------------------------------------------------------------------%% Internal Function Definitions%% ------------------------------------------------------------------
%%%-------------------------------------------------------------------%%% @author thinkpad <>%%% @copyright (C) 2014, thinkpad%%% @doc%%%%%% @end%%% Created : 27 Jun 2014 by thinkpad <>%%%--------------------------------------------------------------------module(websocket_store).-include_lib("stdlib/include/qlc.hrl" ).%% API-export([dirty_insert/2,dirty_lookall/0,dirty_lookall_record/0,dirty_delete/1]).-export([init/0,insert/2,delete/1,lookup/1,lookall/0]).-record(socket_to_pid, {pid,socket_type}).-define(WAIT_FOR_TABLES, 10000).%%%===================================================================%%% API%%%===================================================================init()->    dynamic_db_init().    %--------------------------------------------------------------------%% @doc Insert a key and pid.%% @spec insert(Key, Pid) -> void()%% @end%%--------------------------------------------------------------------insert(Pid,SocketType) when is_pid(Pid) ->    Fun = fun() -> mnesia:write(#socket_to_pid{pid = Pid, socket_type = SocketType}) end,    {atomic, _} = mnesia:transaction(Fun).%--------------------------------------------------------------------%% @doc  dirty insert pid and socket%% @spec  dirty_insert(Pid socket)%% @end%%--------------------------------------------------------------------dirty_insert(Pid,SocketType) when is_pid(Pid) ->    mnesia:dirty_write(#socket_to_pid{pid = Pid, socket_type = SocketType}).%--------------------------------------------------------------------%% @doc dirty_read data%% @spec dirty_lookall()%% @end%%--------------------------------------------------------------------dirty_lookall()->    mnesia:dirty_select(socket_to_pid,[{#socket_to_pid{pid=‘$1‘,socket_type = ‘$2‘},[],[‘$$‘]}]).dirty_delete(Pid)->    mnesia:dirty_delete(socket_to_pid,Pid).%--------------------------------------------------------------------%% @doc look all record info%% @spec %% @end%%--------------------------------------------------------------------dirty_lookall_record()->         mnesia:dirty_select(socket_to_pid,[{#socket_to_pid{pid=‘$1‘,socket_type = ‘$2‘},[],[‘$_‘]}]).%%--------------------------------------------------------------------%% @doc Find a pid given a key.%% @spec lookup(Key) -> {ok, Pid} | {error, not_found}%% @end%%--------------------------------------------------------------------lookup(Pid) ->       do(qlc:q([{X#socket_to_pid.pid,X#socket_to_pid.socket_type} || X <- mnesia:table(socket_to_pid),X#socket_to_pid.pid==Pid])).%%--------------------------------------------------------------------%% @doc Find all list%% @spec lookall() -> {List} | {error, not_found}%% @end%%--------------------------------------------------------------------lookall() ->         do(qlc:q([[X#socket_to_pid.pid,X#socket_to_pid.socket_type] || X <- mnesia:table(socket_to_pid)])).%%--------------------------------------------------------------------%% @doc Delete an element by pid from the registrar.%% @spec delete(Pid) -> void()%% @end%%--------------------------------------------------------------------delete(Pid) ->    try        [#socket_to_pid{} = Record] = mnesia:dirty_read(socket_to_pid, Pid, #socket_to_pid.pid),        mnesia:dirty_delete_object(Record)    catch        _C:_E -> ok    end.   %%--------------------------------------------------------------------%% @doc%% @spec%% @end%%--------------------------------------------------------------------%%%===================================================================%%% Internal functions%%%===================================================================dynamic_db_init() ->    delete_schema(),    {atomic, ok} = mnesia:create_table(socket_to_pid, [{attributes, record_info(fields, socket_to_pid)}]),    ok.%% deletes a local schema.delete_schema() ->    mnesia:stop(),    mnesia:delete_schema([node()]),    mnesia:start().do(Query) ->    F = fun() -> qlc:e(Query) end,    {atomic, Value} = mnesia:transaction(F),    Value.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.