I’m using: SWI-Prolog version 7.3.27
I created a meta interpreter that functions as a rule engine for an agent. The server is non-prolog and prolog connects to it with a websocket client and keep listening for messages from the server. Each message triggers the rule engine to run and send conclusions back to the server.
I receive messages from the server in json format so I added the format(json)
option to the ws_receive
. This seems to work fine most of the time. It receives the exact same message every time, but for some reason every third time the message is received as a string instead of parsed json dict.
I handle the receive
receive(WS, State, NewState) :-
ws_receive(WS, Message, [ format(json) ]),
websocket{ opcode: OpCode, data: Payload } :< Message,
receive_message(OpCode, Payload, State, NewState, Response),
send_response(WS, Response).
And this predicate check whether the message is really a string or not.
%%
%% Handle a message with the text opcode.
%%
receive_text(in(Payload, State), out(none, State)) :-
string(Payload),
!, debug(warn/connection, '[CONNECTION][WARN] message received is string instead of dict "~w" ...', [Payload]).
receive_text(in(Payload, State), out(Response, State)) :-
is_dict(Payload),
_Key{ event: Event, dataType: DataType, data: Data } :< Payload,
...
The output below shows the debug log. An event is periodically send to the client form the server. Event “triggered” lines are successful runs, while the warn line failed. So the solver ran twice correctly and failed once. This keeps repeating.
% [CONNECTION] Calling solver for event "triggered" .
% [CONNECTION] Event "triggered" resulted in following actions: [action(say,0)].
% [CONNECTION] Calling solver for event "triggered" .
% [CONNECTION] Event "triggered" resulted in following actions: [action(say,0)].
% [CONNECTION][WARN] message received is string instead of dict "{"event":"triggered","data":"..." ...
Again this is in response to the exact same message. I am completely baffled.
Ralf