Hi,
I’ve based on the chat server example. The code is
:- module(logicserver, [server/1]).
:- use_module(library(http/websocket)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_files)).
:- use_module(library(http/http_server_files)).
:- use_module(library(http/http_json)).
:- use_module(library(http/hub)).
:- use_module(library(sandbox)).
:- http_handler(web(.), serve_files, [prefix]).
:- http_handler(data(.), serve_files, [prefix]).
:- http_handler(css(.), serve_files, [prefix]).
:- http_handler(lib(.), serve_files, [prefix]).
:- http_handler(root(.), main, [prefix]).
:- http_handler(ws(logic),
http_upgrade_to_websocket(acceptWS, []),
[id(logic_websocket)]).
:- multifile http_json/1.
%:- initialization(server(3045)).
http_json:json_type('application/x-javascript').
http_json:json_type('application/javascript').
http_json:json_type('text/javascript').
http_json:json_type('text/x-javascript').
http_json:json_type('text/x-json').
http_json:json_type('text/x-prolog').
http_json:json_type('text/prolog').
http:location(css, '/css', []).
http:location(lib, '/lib', []).
http:location(data, '/data', []).
http:location(web, '/web', []).
http:location(ws, '/cmd', []).
main(Request) :-
http_reply_from_files('.', [], Request).
acceptWS(WS) :-
hub_add(main, WS, ID),
format("WS added ~w", [ID]).
server(Port) :-
hub_create(main, WS , _{}),
thread_create(handle(WS), _, [alias(handle)]),
http_server(http_dispatch, [port(Port)]).
serve_files(Request) :-
http_reply_from_files(web, [], Request).
serve_files(Request) :-
http_reply_from_files(lib, [], Request).
serve_files(Request) :-
http_reply_from_files(css, [], Request).
serve_files(Request) :-
http_reply_from_files(data, [], Request).
serve_files(Request) :-
http_404([\p('ouch')], Request).
handle(WS) :-
format("Handle called", []),
thread_get_message(WS.queues.event, json(Message)),
msgHandler::decode(Message, Message2),
hub_broadcast(WS.name, Message),
handle(WS).
The message “WS added” is coming, but not the message “Handle called”, which means handle(WS) will never be called. And I don’t see what is wrong. BTW the Messages are JSONs
[UPDATE] Sorry, a typo in the original code has lead to a permanen false of handle/1. So it is called, but the problem is now, that the JSONs seems to induce trouble. The message is:
ERROR: [Thread httpd@3045_2] Unknown error term: websocket_error(unexpected_message,websocket{data:"{“from”:“D”,“to”:“L”,“cmd”:“result”,“data”:" Ego -1.0 0.0014213432822582917 0.0014213432822582917 direction L D pack “,“seq”:1}”,format:string,opcode:text})
Cheers
Hans