Websocket stopped receiving messages from client after i spawned processes in another module

25 views
Skip to first unread message

me...@lanyardmarket.com

unread,
Oct 3, 2018, 2:12:51 PM10/3/18
to ChicagoBoss
Hi,

I am writing a game server with Chicagoboss and mnesia. I create two processes in another module where i created in lib folder, messaging each other every 15ms and sending match state to match watchers.

-module(engine).
-export([ spawn/1, receiver/1, sender/0  ]). 


spawn(MatchId)-> io:format("spwner activated"),
  Pid2 = spawn(engine, sender, []),
  Pid3 = spawn(engine, receiver, [Pid2]),
  Pid2 ! {Pid3, MatchId},
  receiver(Pid2).

receiver(Pid2) ->
  receive
    {Pid2, MatchId} ->
      io:format("Process Sender: ~p~n",[Pid2]),
      Pid2 ! {self(), MatchId}
  end,
  receiver(Pid2).

sender() ->
  receive
    {From, MatchId} ->
      %From ! {self(), Msg},
      io:format("Process Receiver: ~p and matchid:~p~n",[From,MatchId]),
      %SEND MATCH DATA
      case boss_db:find(jamir_match_data,[{match_id,'equals',MatchId}]) of
           [] -> [];
           EXTMATCH -> [{MatchDatabase,SqlMatchId,NewMatchId,Watchers,PlayersCount,ArenaData,PlayerData,BulletData,BombData,RobotData,VehicleData,CreatureData,GemData,ResponseData,Process}] = EXTMATCH,
                       [WATCHERS,DATA] = prepareData(EXTMATCH),
                       PACK = jsx:encode(DATA),
                       lists:map(fun(EACHWATCHERWS) -> 
                          EACHWATCHERWS ! {text,<< "e&" , PACK/binary , "&1&1&1&1&1&1&1"  >>}
                       end, WATCHERS),
                       NEWRESPONSELIST = [0,0,0,0,0,0,0,0],

                       [UpdateMatch] = EXTMATCH,
                       UpdatedRecord = UpdateMatch:set([{response_data, NEWRESPONSELIST}]),
                       UpdatedRecord2 = UpdatedRecord:set([{bullet_data, []}]),
                       UpdatedRecord2:save()
                                
      end,
      %LOOP
      erlang:send_after(15, From , {self(), MatchId});
    stop ->
      true
  end,
  sender().

Two processes working fine but after sending starts, handle_info in websocket module stopped getting messages from client. It can be a simple question but i am really stucked at this point and any help would be great.

Thanks

me...@lanyardmarket.com

unread,
Oct 3, 2018, 3:06:03 PM10/3/18
to ChicagoBoss
Sorry i meant handle_incoming

3 Ekim 2018 Çarşamba 21:12:51 UTC+3 tarihinde Lanyard Market yazdı:

Konstantin Gorshkov

unread,
Oct 3, 2018, 4:15:05 PM10/3/18
to chica...@googlegroups.com
Hi, 

It looks like the send_after() completly floods the mailboxes of both processes.

Why didn't you use OTP's gen_server behaviour in your module?

--
You received this message because you are subscribed to the Google Groups "ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chicagoboss...@googlegroups.com.
Visit this group at https://groups.google.com/group/chicagoboss.
To view this discussion on the web visit https://groups.google.com/d/msgid/chicagoboss/9240c954-3e3d-4c68-95b1-6429c214e049%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Konstantin Gorshkov

unread,
Oct 3, 2018, 4:57:03 PM10/3/18
to chica...@googlegroups.com
And one more thing. The spawn/1 produces an infinity loop because of the last function call in its body.

On Wed, 3 Oct 2018, 21:12 , <me...@lanyardmarket.com> wrote:
--

me...@lanyardmarket.com

unread,
Oct 4, 2018, 5:17:04 AM10/4/18
to ChicagoBoss
Hello Konstantin, thanks for your answer. Yes removing infinite loop sold the problem. I thought i can not use a gen_server behaviour in another module in CB when i use websocket module.
What i had in mind is, instead of creating an only one process mapping each match and updating clients, i decided to create two processes for each match looping each other with 15ms timeout to send data to clients. I think if i can make the modules behaviour as gen_server then i can just loop throuhg one process and send to ! self()

As webrtc is not available in CB, i am thinking many ways to handle concurrency and fast updates for a first person shooter browser game. I always worked with CB since 4 years and want to go on with it. So if you have a better way to send data to clients more efficiently in 15ms, it would be great.



3 Ekim 2018 Çarşamba 23:57:03 UTC+3 tarihinde Konstantin Gorshkov yazdı:
Reply all
Reply to author
Forward
0 new messages