1
+ <?php
2
+ /**
3
+ * Copyright (c) 2011 Martijn Croonen
4
+ *
5
+ * This file is part of PHP WebSockets.
6
+ *
7
+ * PHP WebSockets is an implementation of the WebSockets protocol.
8
+ * The WebSockets protocol is being standardized by the IETF and allows two-way
9
+ * communication between a client (e.g. Your web browser) and a remote host
10
+ * (e.g. a WebSockets Server).
11
+ *
12
+ * PHP WebSockets is licensed under the terms of the MIT license which can be
13
+ * found in the LICENSE file.
14
+ *
15
+ * @copyright Copyright (c) 2011 Martijn Croonen <[email protected] >
16
+ * @author Martijn Croonen <[email protected] >
17
+ */
18
+
19
+ namespace App \Net ;
20
+
21
+ /**
22
+ * The ConnectionObserver interface should be used to observe WebSocket
23
+ * connections. The methods in this interface are used like events. Since
24
+ * PHP is single-threaded, sockets operate in non-blocking mode. This means
25
+ * we have to make our code asynchronous and use events. The methods in this
26
+ * interface behave like events. Any class implementing this interface can
27
+ * observe one or more WebSocket connections and subscribe to changes or events.
28
+ * When these events or changes happen, the matching function will be called in
29
+ * all observers.
30
+ */
31
+ interface ConnectionObserver
32
+ {
33
+ /**
34
+ * The onMessage method is called when a WebSocket connection has read a full
35
+ * message.
36
+ *
37
+ * @param WebSocketConnection $pConnection The connection object in which the event
38
+ * accured.
39
+ * @param int $nType The event type.
40
+ * @param string|array $sData In case of a text message this will be a string. In case of
41
+ * binary data this will be a byte-array.
42
+ */
43
+ public function onMessage (WebSocketConnection $ pConnection , $ nType , $ sData );
44
+
45
+ /**
46
+ * The onClose method is called in all classes who subscribed to events of the
47
+ * WebSocket connection. Additionally, the reason is passed and whether the socket
48
+ * was cleaned cleanly (completed the closing handshake).
49
+ *
50
+ * @param WebSocketConnection $pConnection The connection object in which the event
51
+ * accured.
52
+ * @param int $nReason The reason for the connection close (numeric)
53
+ * @param string $sReason The reason for the connection close (text)
54
+ */
55
+ public function onClose (WebSocketConnection $ pConnection , $ nReason , $ sReason );
56
+
57
+ /**
58
+ * The onHandshakeRecieved method is called in all classes who subcribed to events
59
+ * of the WebSocket connection. It is called directly after the handshake was read
60
+ * and parsed. When a handshake appears to be invalid this method is never called
61
+ * but the connection will be closed and the onClose method will be called instead.
62
+ * Cookies can now be read and cookies can be send.
63
+ *
64
+ * @param WebSocketConnection $pConnection The connection object in which the event
65
+ * accured.
66
+ */
67
+ public function onHandshakeRecieved (WebSocketConnection $ pConnection );
68
+
69
+ /**
70
+ * The onOpen method is called in all classes who subscribed to the events of the WebSocket
71
+ * connection when the connection is opened. A connection is open when the handshake is read,
72
+ * parsed AND the server has send its handshake in return.
73
+ * When all this has happened, the connection is in OPEN state.
74
+ * You can start sending data now. Setting new cookies is useless since the protocol only
75
+ * supports -like http- the setting of cookies before any data is send.
76
+ *
77
+ * @param WebSocketConnection $pConnection The connection object in which the event
78
+ * accured.
79
+ */
80
+ public function onOpen (WebSocketConnection $ pConnection );
81
+
82
+ /**
83
+ * The onPing method is called in all classes who subscribed to the events of the WebSocket
84
+ * connection when a ping control frame is received. There is no need to manually send a
85
+ * pong back. Since it is requirement in the standard the WebSocketserver or client will do
86
+ * this implicitly.
87
+ *
88
+ * @param WebSocketConnection $pConnection The connection object in which the event
89
+ * accured.
90
+ */
91
+ public function onPing (WebSocketConnection $ pConnection );
92
+
93
+ /**
94
+ * The onPong method is called in all classes who subscribed to the events of the WebSocket
95
+ * connection when a pong control frame is received. The WebSockets procotol does not has any
96
+ * requirments on when to send a ping. Thus sending pings and detecting pongs should be handled
97
+ * on application level. However, replies to a ping are mandatory in the protocol and thus handled
98
+ * on protocol level.
99
+ *
100
+ * @param WebSocketConnection $pConnection The connection object in which the event
101
+ * accured.
102
+ */
103
+ public function onPong (WebSocketConnection $ pConnection );
104
+ }
0 commit comments