1515 *
1616 * @package WebSocket
1717 * @author Gemblue
18- *
1918 */
2019
2120class WebSocket {
2221
23- public $ address ;
24- public $ port ;
22+ /** Props */
2523 public $ server ;
2624 public $ client ;
27- public $ key ;
2825
2926 /**
30- * Construct a socket
27+ * Create a socket
3128 */
32- public function __construct (string $ address , int $ port ) {
29+ public function create (string $ address , int $ port ) {
3330
34- $ this -> address = $ address ;
35- $ this ->post = $ port ;
31+ // Create socket.
32+ $ this ->server = socket_create ( AF_INET , SOCK_STREAM , SOL_TCP ) ;
3633
37- if (!$ this ->server = socket_create (AF_INET , SOCK_STREAM , SOL_TCP )) {
38- throw new Exception ('Failed to create a socket ' );
39- }
40- }
41- 42- public function setOption () {
43- 44- if (!socket_set_option ($ this ->server , SOL_SOCKET , SO_REUSEADDR , 1 )) {
45- throw new Exception ('Failed to set option ' );
46- }
47- 48- return $ this ;
49- 50- }
51- 52- public function bind () {
53- 54- 55- if (!socket_bind ($ this ->server , $ this ->address , $ this ->port )) {
56- throw new Exception ('Failed to bind ' );
57- }
58- 59- return $ this ;
60- 61- }
62- 63- public function listen () {
34+ socket_set_option ($ this ->server , SOL_SOCKET , SO_REUSEADDR , 1 );
35+ socket_bind ($ this ->server , $ address , $ port );
36+ socket_listen ($ this ->server );
6437
65- if (!socket_listen ($ this ->server )) {
66- throw new Exception ('Failed to listen ' );
67- }
68- 69- if (!$ this ->client = socket_accept ($ this ->server )) {
70- throw new Exception ('Failed to accept ' );
71- }
72- 73- return $ this ;
74- 7538 }
7639
7740 /**
78- * Handshake
41+ * Handshake.
7942 *
80- * Writing websocket protocol headers
43+ * Simple handshake to client with headers.
8144 */
8245 public function handshake () {
46+ // Set client.
47+ $ this ->client = socket_accept ($ this ->server );
8348
49+ // Read client.
8450 $ request = socket_read ($ this ->client , 5000 );
85- 86- // Match websocket key
87- preg_match ('#Sec-WebSocket-Key: (.*)\r\n# ' , $ request , $ matches );
8851
89- $ this ->key = base64_encode (pack (
52+ preg_match ('#Sec-WebSocket-Key: (.*)\r\n# ' , $ request , $ matches );
53+ $ key = base64_encode (pack (
9054 'H* ' ,
9155 sha1 ($ matches [1 ] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11 ' )
9256 ));
93- 94- // Build websocket content header!
57+
58+ // Setup headers.
9559 $ headers = "HTTP/1.1 101 Switching Protocols \r\n" ;
9660 $ headers .= "Upgrade: websocket \r\n" ;
97- $ headers .= "server : Upgrade \r\n" ;
61+ $ headers .= "Connection : Upgrade \r\n" ;
9862 $ headers .= "Sec-WebSocket-Version: 13 \r\n" ;
99- $ headers .= "Sec-WebSocket-Accept: { $ this -> key } \r\n\r\n" ;
63+ $ headers .= "Sec-WebSocket-Accept: $ key \r\n\r\n" ;
10064
101- // Writing a content .
65+ // Write headers .
10266 socket_write ($ this ->client , $ headers , strlen ($ headers ));
103- 104- // Return log message, outside the loop.
105- echo "Writing headers .. \n" ;
106- 67+ 10768 }
10869
10970 /**
110- * Broadcast
71+ * Emit
11172 *
112- * Writing socket content with loop .
73+ * Method to emit message .
11374 */
114- public function broadcast ( string $ message ) {
75+ public function emit ( $ message ) {
11576
116- socket_write ($ this ->client , chr (129 ) . chr (strlen ($ message )) . $ message );
117- 118- }
119- 120- /**
121- * Close.
122- */
123- public function __destruct ()
124- {
125- socket_close ($ this ->socket );
126- }
77+ $ response = chr (129 ) . chr (strlen ($ message )) . $ message ;
78+ socket_write ($ this ->client , $ response );
12779
80+ }
12881}
12982
130- // Mantap, sekarang kita coba.
131- $ websocket = new WebSocket ("127.0.0.1 " , 12345 );
132- 133- $ websocket ->setOption ()
134- ->bind ()
135- ->listen ()
136- ->handshake ();
83+ // Let's consume the class.
84+ $ websocket = new WebSocket ();
13785
138- $ websocket ->broadcast ('Halo kakak! ' );
86+ $ websocket ->create ("127.0.0.1 " , 12345 );
87+ $ websocket ->handshake ();
88+ $ websocket ->emit ('Halo kakak, lagi apa! ' );
0 commit comments