File: simple.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (59 lines) | stat: -rw-r--r-- 1,780 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
:- module(ex1,
          [ connect/1
          ]).
:- use_module(user:library(stomp)).    % make available on toplevel
:- use_module(library(stomp)).

/** <module> Simple STOMP example

This file allows some simple interaction with STOMP.  For example:

```
swipl ex1.pl
?- connect(C).
C = 'a09b8dea-de39-11eb-8887-cbf5d67759f6'.

?- stomp_send($C, '/queue/test', _{}, "hello").
X = 'a09b8dea-de39-11eb-8887-cbf5d67759f6'.

26 ?- Received "hello"
```

@license This code is in the public domain
*/

% :- debug(stomp(_)).
% :- debug(ex1).

connect(Connection) :-
    stomp_connection('127.0.0.1':32772,
                     '/',
                     _{'heart-beat': '5000,5000',
                       login: guest,
                       passcode: guest
                      },
                     on_frame, Connection),
    stomp_connect(Connection).

:- det((on_frame/4)).

on_frame(connected, Connection, _Header, _Body) =>
    Destination = '/queue/test',
    stomp_subscribe(Connection, Destination, 0, _{}).
on_frame(message, Connection, Header, Body) =>
    debug(ex1, 'message from connection ~p~n~p~n~p',
          [Connection, Header, Body]),
    format('Received ~p~n', [Body]).
on_frame(disconnected, Connection, _Header, _Body) =>
    debug(ex1, 'on_disconnected from connection ~p', [Connection]),
    stomp_teardown(Connection).
on_frame(error, Connection, Header, Body) =>
    debug(ex1, 'on_error from connection ~p~n~p~n~p',
          [Connection, Header, Body]).
on_frame(heartbeat_timeout, Connection, _Header, _Body) =>
    debug(ex1, 'on_heartbeat_timeout from connection ~p', [Connection]),
    stomp_teardown(Connection).
on_frame(Event, Connection, _Header, _Body) =>
    debug(ex1, 'Unknown event ~p on connection ~p',
          [Event, Connection]).