更新Cowboy Websocket处理程序中的状态会崩溃或自动忽略

更新Cowboy Websocket处理程序中的状态会崩溃或自动忽略,websocket,erlang,gen-server,cowboy,Websocket,Erlang,Gen Server,Cowboy,我有一个接收websocket连接的ws_处理程序 此进程等待从开始的输入问题是cowboy的websocket_info/2处理程序将仅通过使用erlang内置消息操作符接收发送到websocket进程的消息(或者相当于erlang:send/{2,3}函数) 所以你应该: WS!myscreenupdate 而不是 gen\u服务器:cast(WS,myscreenupdate) 使用gen_server:cast时,消息可能会被cowboy消息循环丢弃,因为它不是可识别的消息。当您使用ge

我有一个接收websocket连接的ws_处理程序


此进程等待从
开始的输入问题是cowboy的
websocket_info/2
处理程序将仅通过使用erlang内置消息操作符
接收发送到websocket进程的消息
(或者相当于
erlang:send/{2,3}
函数)

所以你应该:

WS!myscreenupdate

而不是

gen\u服务器:cast(WS,myscreenupdate)


使用
gen_server:cast
时,消息可能会被cowboy消息循环丢弃,因为它不是可识别的消息。当您使用
gen\u server:call
时,您会遇到死锁。

看看您是否在此处找到任何有帮助的信息:
websocket_handle({text, <<"h", Name/binary>>}, State) ->
  {ok, PID} = player:go(self(), <<"profiledata", Name/binary>>),
  erlang:start_timer(1000, self(), <<"Hello!">>),
  {reply, {text, <<"You joined, ", Name/binary>>}, {State, PID, <<"turn1">>}};
websocket_handle({text, <<"myscreen">>}, S = {_, P, _}) ->
  gen_server:call(P, myscreen),
  {ok, S};
handle_call(myscreen, _, {WS, Profile, Cab}) ->
  gen_server:cast(WS, myscreenupdate),
  {reply, ok, {WS, Profile, Cab}};
websocket_info(myscreenupdate, State = {St,P, _}) ->
  {reply, {text, <<"My screen update">>}, {St, P, <<"turn2">>}};
websocket_info(myscreenupdate, State = {St,P, _}) ->
  {reply, {text, <<"My screen update">>}, {St, P, <<"turn2">>}};
ws_handler.erl:

-module(ws_handler).

-export([init/2]).
-export([websocket_init/1]).
-export([websocket_handle/2]).
-export([websocket_info/2]).

init(Req, Opts) ->
    {cowboy_websocket, Req, Opts}.

websocket_init(State) ->
    {ok, State}.

websocket_handle({text, <<"h", Name/binary>>}, State) ->
  {ok, PID} = player:go(self(), <<"profiledata", Name/binary>>),
  erlang:start_timer(1000, self(), <<"Hello!">>),
  {reply, {text, <<"You joined, ", Name/binary>>}, {State, PID, <<"turn1">>}};

websocket_handle({text, <<"myscreen">>}, S = {_, P, _}) ->
  gen_server:call(P, myscreen),
  {ok, S};

websocket_handle({text, <<"auth", Auth/binary>>}, S = {_St, P, _}) ->
  case s:s(P, Auth) of
    {ok, Valid} -> {reply, {text, << "Authorized">>}, S};
    _ -> {reply, {text, <<"Error">>}, S}
  end;
websocket_handle({text, Msg}, S = {_St, P, Outbox}) ->
    {reply, {text, Outbox}, S};
websocket_handle(_Data, State) ->
    {ok, State}.

websocket_info(myscreenupdate, State = {St,P, _}) ->
  {reply, {text, <<"My screen update">>}, {St, P, <<"turn2">>}};

websocket_info({timeout, _Ref, _Ignored}, State = {_, P, Outbox}) ->
    erlang:start_timer(1000, self(), <<"This is ignored">>),
  Msg = Outbox,
    {reply, {text, Msg}, State};
websocket_info(_Info, State) ->
    {ok, State}.
-module(player).
-compile(export_all).

handle_call(myscreen, _, {WS, Profile, Cab}) ->
  gen_server:cast(WS, myscreenupdate),
  {reply, ok, {WS, Profile, Cab}};
handle_call(get_profile, _, State = {_WSPID, Profile, _}) ->
  {reply, Profile, State}.

init([WSPID, Profile]) ->
  {ok, {WSPID, Profile, null}};
init([WSPID, Profile, CabinetPID]) ->
  {ok, {WSPID, Profile, CabinetPID}}.

go(WSPID, Profile, CabinetPID) ->
  gen_server:start_link(?MODULE, [WSPID, Profile, CabinetPID], []).
go(WSPID, Profile) ->
  gen_server:start_link(?MODULE, [WSPID, Profile], []).