aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/manual/gun_ws_protocol.asciidoc
blob: 417ba94fe25c2d317e519386e4f83bacd8497688 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
= gun_ws_protocol(3)

== Name

gun_ws_protocol - Websocket subprotocols

== Description

The `gun_ws_protocol` module provides the callback interface
and types for implementing Websocket subprotocols.

== Callbacks

Websocket subprotocols implement the following interface.

=== init

[source,erlang]
----
init(ReplyTo, StreamRef, Headers, Opts) -> {ok, State}

ReplyTo   :: pid()
StreamRef :: reference()
Headers   :: cow_http:headers()
Opts      :: gun:ws_opts()
State     :: protocol_state()
----

Initialize the Websocket protocol.

ReplyTo::

The pid of the process that owns the stream and to
which messages will be sent to.

StreamRef::

The reference for the stream. Must be sent in messages
to distinguish between different streams.

Headers::

Headers that were sent in the response establishing
the Websocket connection.

Opts::

Websocket options. Custom options can be provided in
the `user_opts` key.

State::

State for the protocol.

=== handle

[source,erlang]
----
handle(Frame, State) -> {ok, FlowDec, State}

Frame   :: cow_ws:frame()
State   :: protocol_state()
FlowDec :: non_neg_integer()
----

Handle a Websocket frame.

This callback may receive fragmented frames depending
on the protocol and may need to rebuild the full
frame to process it.

Frame::

Websocket frame.

State::

State for the protocol.

FlowDec::

How many messages were sent. Used to update the flow
control state when the feature is enabled.

== Types

=== protocol_state()

[source,erlang]
----
protocol_state() :: any()
----

State for the protocol.

As this part of the implementation of the protocol
the type may differ between different Websocket
protocol modules.

== Changelog

* *2.0*: Module introduced.

== See also

link:man:gun(7)[gun(7)],
link:man:gun(3)[gun(3)],
link:man:gun:ws_upgrade(3)[gun:ws_upgrade(3)]