Skip to content

Commit 7b09af8

Browse files
committed
PHP WebSockets
1 parent a4ade3f commit 7b09af8

17 files changed

+3449
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store?
2+
ehthumbs.db
3+
Icon?
4+
Thumbs.db

App/Net/ConnectionObserver.php

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

App/Net/Frameable.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 Frameable interface should be implemented by all classes who behave
23+
* like frames. This is useful when you want to do the framing yourself
24+
* but still would like the rest of the WebSocket server or client.
25+
*/
26+
interface Frameable
27+
{
28+
/**
29+
* The asByteStream method should always return a FULL frame as a
30+
* byte array. When you pass an object to the WebSocketConnection :: sendFrame
31+
* method, this method will be used to get a byte stream (as an array) to
32+
* send over the socket. Always make sure you send complete frames, if
33+
* your frame is invalid or incomplete the connection will timeout or be
34+
* closed 'dirty' because frame send after the corrupt frame will not be
35+
* read correctly.
36+
*
37+
* @return array A full WebSockets frame.
38+
*/
39+
public function asByteStream();
40+
}

App/Net/ServerObserver.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 ServerObserver interface should be used to observe WebSocket
23+
* servers. 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 servers 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 ServerObserver
32+
{
33+
/**
34+
* This method will be called in all observers who subscribed to events for
35+
* the WebSockets server when it recieves a new connection. The new connection
36+
* is a TCP connection, the handshake has not yet been read or send.
37+
* You can close() the connection here rather then disconnect()-ing it since
38+
* the WebSocketConnection is not yet in OPEN state. Only the TCP connection is
39+
* open and in case of a secure server the TLS handshake is done.
40+
*
41+
* @param WebSocketServer $pServer The instance of the WebSocketServer in
42+
* which the event accured.
43+
* @param WebSocketConnection $pConnection Instance of the new connection
44+
*/
45+
public function onNewConnection(WebSocketServer $pServer, WebSocketConnection $pConnection);
46+
47+
/**
48+
* This method is called in all observers who subscribed to events for
49+
* the WebSockets server when the server is opened and starts listening.
50+
*
51+
* @param WebSocketServer $pServer The instance of the WebSocketServer in
52+
* which the event accured.
53+
*/
54+
public function onServerOpen(WebSocketServer $pServer);
55+
56+
/**
57+
* the onServerClose method is called in all observers who subscribed to events for
58+
* the WebSockets server when the server is closed and stops listening.
59+
*
60+
* @param WebSocketServer $pServer The instance of the WebSocketServer in
61+
* which the event accured.
62+
*/
63+
public function onServerClose(WebSocketServer $pServer);
64+
}

0 commit comments

Comments
 (0)