Skip to content

Commit

Permalink
fix send client
Browse files Browse the repository at this point in the history
  • Loading branch information
ronalfei committed Mar 22, 2014
1 parent 167f5e9 commit 9aa3586
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
34 changes: 30 additions & 4 deletions src/client/send_client.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-module(send_client).

-module(send_client).
-include("../ses.hrl").
-behaviour(websocket_client_handler).

-export([
Expand Down Expand Up @@ -27,27 +28,45 @@ websocket_handle({pong, _}, _ConnState, State) ->

websocket_handle({text, <<"GO">>}, _ConnState, State) ->
%{reply, {text, Msg}, State}.
websocket_client:cast(self(), {text, <<"GO">>}),
{reply, {binary, <<"bbbbbbbb">>}, _ConnState, State};
lager:notice("1111111111111111111"),
Self = self(),
spawn(fun() -> loop(Self) end),
lager:notice("22222222222222222"),
%{reply, {binary, <<"bbbbbbbb">>}, _ConnState, State};
{ok, State};


websocket_handle({text, Msg}, _ConnState, State) ->
io:format("receive from server : ~p~n", [Msg]),
lager:notice("receive from server : ~p~n", [Msg]),
%{reply, {text, Msg}, State}.
{ok, State}.


websocket_info({text, <<"GO">>}, _ConnState, State) ->
lager:notice("3333333333333333"),
lager:notice("self() :~p", [self()]),
websocket_client:cast(self(), {text, <<"GO">>}),
{reply, {binary, <<"bbbbbbbb">>}, _ConnState, State};


websocket_info({text, Msg}, _ConnState, State) ->
lager:notice("4444444444444"),
{reply, {text, Msg}, _ConnState, State};

websocket_info({timeout, _Ref, _Msg}, _ConnState, State) ->
%erlang:start_timer(1000, self(), <<"alive2">>),
%{reply, {text, <<"alive">>}, _ConnState, State};
{ok, State};


websocket_info(start, _ConnState, State) ->
%{reply, {text, <<"from client, erlang message received">>}, State}.
{ok, State};


websocket_info(Info, _ConnState, State) ->
%{reply, {text, <<"from client, erlang message received">>}, State}.
lager:notice("555555555555555"),
{ok, State}.

websocket_terminate(Reason, _ConnState, State) ->
Expand All @@ -57,6 +76,13 @@ websocket_terminate(Reason, _ConnState, State) ->

send() ->
{ok, Pid} = ?MODULE:start_link("127.0.0.1"),
lager:notice("pid:~p", [Pid]),
websocket_client:cast(Pid , {text, <<"SEND#ronalfei">>}),
ok.

loop(Pid) ->
websocket_client:cast(Pid , {binary, <<"bbbbbbbb">>}),
receive
after 3000 ->
loop(Pid)
end.
11 changes: 9 additions & 2 deletions src/receive_handler.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-module(receive_handler).
-include("ses.hrl").
-behaviour(cowboy_websocket_handler).

-export([init/3]).
Expand Down Expand Up @@ -34,13 +35,19 @@ websocket_handle(_Data, Req, State) ->



websocket_info({timeout, _Ref, {binary, Bin}}, Req, State) ->
websocket_info({binary, Bin}, Req, State) ->
{reply, {binary, Bin}, Req, State};


websocket_info({text, Msg}, Req, State) ->
{reply, {text, Msg}, Req, State};


websocket_info({timeout, _Ref, Msg}, Req, State) ->
{reply, {text, Msg}, Req, State};



websocket_info(Info, Req, State) ->
lager:warning("receive info : ~p", [Info]),
{ok, Req, State}.
Expand All @@ -56,7 +63,7 @@ init_ets(TableName) ->
Options = [set, public, named_table, {read_concurrency, true} ],
ets:new(Name, Options),
Leader = group_leader(),
lager:debug(" group leader for this connnection is ~p", [Leader]),
lager:warning(" group leader for this connnection is ~p", [Leader]),
ets:give_away(Name, Leader, []).


Expand Down
15 changes: 10 additions & 5 deletions src/send_handler.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-module(send_handler).
-include("ses.hrl").
-behaviour(cowboy_websocket_handler).

-export([init/3]).
Expand All @@ -12,6 +13,7 @@ init({tcp, http}, _Req, _Opts) ->

websocket_init(_TransportName, Req, _Opts) ->
erlang:start_timer(0, self(), <<"HI">>),
lager:warning("self:~p", [self()]),
{ok, Req, {} }.


Expand All @@ -21,18 +23,21 @@ websocket_handle({text, <<"SEND#", ToUserid/binary>>}, Req, State) ->
{ok, Pid} ->
NewState = {<<"SEND#">>, Pid},
insert_ets(pid_map, {NewState, self()}),%% 把发送流的进程插入到ets, 目前没有太大意义
lager:warning("111111111: ~p", [NewState]),
{reply, {text, <<"GO">>}, Req, NewState};
{error, Reason} ->
{reply, {text, Reason}, Req, State}
end;

websocket_handle({text, Msg}, Req, State) ->
%{reply, {text, << "receive", Msg/binary >>}, Req, State};
{ok, Req, State};
websocket_handle({text, Msg}, Req, {<<"SEND#">>, ToPid}) ->
lager:warning("msg to Pid:~p", [ToPid]),
ToPid ! {text, Msg},
{ok, Req, {<<"SEND#">>, ToPid}};


websocket_handle({binary, Bin}, Req, {<<"SEND#">>, ToPid}) ->
erlang:start_timer(0, ToPid, {binary, Bin}),
ToPid ! {binary, Bin},
lager:warning("binary to Pid:~p", [ToPid]),
{ok, Req, {<<"SEND#">>, ToPid}};

websocket_handle({binary, Bin}, Req, State) ->
Expand Down Expand Up @@ -61,7 +66,7 @@ init_ets(TableName) ->
Options = [set, public, named_table, {read_concurrency, true} ],
ets:new(Name, Options),
Leader = group_leader(),
%lager:debug(" group leader for this connnection is ~p", [Leader]),
lager:warning(" group leader for this connnection is ~p", [Leader]),
ets:give_away(Name, Leader, []).


Expand Down
2 changes: 1 addition & 1 deletion src/ses.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test() ->
lager:critical("asdfasdf"),
lager:alert("asdfasdf"),
lager:emergency("asdfasdf"),
f(10000),
%f(10000),

ok.

Expand Down

0 comments on commit 9aa3586

Please sign in to comment.