@@ -26,16 +26,26 @@ websocket_handle(_Frame = {text, Msg}, State) ->
26
26
% chose to use this so I could reuse the shell.erl code from the book
27
27
% without having to modify it to handle binary keys.
28
28
Data = jsx :decode (Msg , [{labels , atom }, return_maps ]),
29
- io :format (" Received message: ~p~n " , [Data ]),
30
29
shell1 ! {self (), Data },
31
30
{ok , State };
32
31
websocket_handle (Frame , State ) ->
33
- io :format (" Received frame: ~p~n " , [Frame ]),
34
32
{ok , State }.
35
33
36
34
websocket_info ({log , Text }, State ) ->
37
35
{reply , {text , Text }, State };
38
- websocket_info (Info , State ) ->
36
+ websocket_info (Info , State ) when is_map (Info ) ->
37
+ % If Info is a map we assume it is a message from shell1
39
38
Bin = jsx :encode ([Info ]),
40
- io :format (" Sending message: ~p~n " , [Bin ]),
41
- {reply , {text , Bin }, State }.
39
+ {reply , {text , Bin }, State };
40
+ websocket_info ({'EXIT' , _Pid , Failure }, State ) ->
41
+ % Show the user the crash
42
+ BV = shell1 :bf (" #> <font color='red'>~p </font><br>" , [Failure ]),
43
+ Message = #{cmd => append_div , id => scroll , txt => BV },
44
+ Bin = jsx :encode ([Message ]),
45
+
46
+ % Restart
47
+ Pid = spawn_link (shell1 , start , [self ()]),
48
+ _ = register (shell1 , Pid ),
49
+ {reply , {text , Bin }, State };
50
+ websocket_info (_Info , State ) ->
51
+ {ok , State }.
0 commit comments