0
votes

I tried the code below and I got:

ERROR: No permission to open websocket `'ws://localhost:9999''.

Why?

If I use root(.), it's ok.

UPDATED (correct code):

:- use_module(library(http/websocket)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_path)).
:- use_module(library(http/http_authenticate)).

:- initialization main.

main :-
    run.

:- http_handler(root(ws), http_upgrade_to_websocket(echo, []), [spawn([])]).

echo(WebSocket) :-
    ws_receive(WebSocket, Message),
    (   Message.opcode == close
    ->  true
    ;   ws_send(WebSocket, Message),
        echo(WebSocket)
    ).

run :-
    run(9999).

run(Port) :-
    http_server(http_dispatch, [port(Port)]).

stop :-
    stop(9999).

stop(Port) :-
    http_stop_server(Port, []).
1
Thank you for completing the example code!mat

1 Answers

2
votes

In abstract terms, If you use root(X), then this means that the path where this is available is /X.

So, if you specify root(ws), then in your case, you would have to access:

ws://localhost:9999/ws

If you specify root(.) then / suffices etc.

Next time, please specify a complete example, so that others can actually try out your code without having to guess the remainder of your snippet.