mcp-core.php
8 months ago
mcp-rest.php
1 year ago
mcp.conf
1 year ago
mcp.js
8 months ago
mcp.md
8 months ago
mcp.php
8 months ago
oauth.php
9 months ago
realtime.php
1 year ago
realtime.php
127 lines
| 1 | <?php |
| 2 | |
| 3 | //require_once(dirname(__FILE__) . '/../../../wp-load.php'); |
| 4 | |
| 5 | $wpLoad = dirname( __FILE__ ) . 'wp-load.php'; |
| 6 | echo $wpLoad; |
| 7 | require_once( dirname( __FILE__ ) . './wp-load.php' ); |
| 8 | |
| 9 | // WebSocket server settings |
| 10 | $host = '0.0.0.0'; // Bind to all IPs (adjust as necessary) |
| 11 | $port = 8080; // Port for WebSocket server (adjust as necessary) |
| 12 | |
| 13 | // Create a WebSocket server socket |
| 14 | $server = stream_socket_server( "tcp://$host:$port", $errno, $errstr ); |
| 15 | |
| 16 | if ( !$server ) { |
| 17 | die( "Error creating server: $errstr ($errno)\n" ); |
| 18 | } |
| 19 | |
| 20 | echo "WebSocket server started at $host:$port\n"; |
| 21 | |
| 22 | $clients = []; |
| 23 | |
| 24 | // Main loop to accept incoming WebSocket connections and handle messages |
| 25 | while ( true ) { |
| 26 | // Prepare an array of streams to check for new activity |
| 27 | $read = array_merge( [$server], $clients ); |
| 28 | $write = null; |
| 29 | $except = null; |
| 30 | |
| 31 | if ( stream_select( $read, $write, $except, null ) > 0 ) { |
| 32 | // Check for new connections |
| 33 | if ( in_array( $server, $read ) ) { |
| 34 | $client = stream_socket_accept( $server ); |
| 35 | |
| 36 | if ( $client ) { |
| 37 | // Perform WebSocket handshake |
| 38 | $request = fread( $client, 1024 ); |
| 39 | preg_match( '#Sec-WebSocket-Key: (.*)\r\n#', $request, $matches ); |
| 40 | $key = base64_encode( pack( 'H*', sha1( $matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' ) ) ); |
| 41 | |
| 42 | $handshakeResponse = |
| 43 | "HTTP/1.1 101 Switching Protocols\r\n" . |
| 44 | "Upgrade: websocket\r\n" . |
| 45 | "Connection: Upgrade\r\n" . |
| 46 | "Sec-WebSocket-Accept: $key\r\n\r\n"; |
| 47 | |
| 48 | fwrite( $client, $handshakeResponse ); |
| 49 | $clients[] = $client; |
| 50 | |
| 51 | echo "New client connected!\n"; |
| 52 | |
| 53 | // Send welcome message to the client |
| 54 | $site_name = get_bloginfo( 'name' ); |
| 55 | $welcome_message = "Welcome to $site_name server"; |
| 56 | $response = encodeWebSocketData( $welcome_message ); |
| 57 | fwrite( $client, $response ); |
| 58 | } |
| 59 | |
| 60 | unset( $read[array_search( $server, $read )] ); |
| 61 | } |
| 62 | |
| 63 | // Handle existing client messages |
| 64 | foreach ( $read as $client ) { |
| 65 | $data = fread( $client, 1024 ); |
| 66 | |
| 67 | if ( !$data ) { |
| 68 | fclose( $client ); |
| 69 | unset( $clients[array_search( $client, $clients )] ); |
| 70 | echo "Client disconnected.\n"; |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | // Decode WebSocket message |
| 75 | $decodedData = decodeWebSocketData( $data ); |
| 76 | echo "Received: $decodedData\n"; |
| 77 | |
| 78 | // Echo back the message to the client |
| 79 | $response = encodeWebSocketData( "Echo: $decodedData" ); |
| 80 | fwrite( $client, $response ); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Function to decode WebSocket frame |
| 86 | function decodeWebSocketData( $data ) { |
| 87 | $unmaskedPayload = ''; |
| 88 | $decodedData = unpack( 'H*', $data ); |
| 89 | $bytes = $decodedData[1]; |
| 90 | |
| 91 | $mask = [ |
| 92 | hexdec( substr( $bytes, 4, 2 ) ), |
| 93 | hexdec( substr( $bytes, 6, 2 ) ), |
| 94 | hexdec( substr( $bytes, 8, 2 ) ), |
| 95 | hexdec( substr( $bytes, 10, 2 ) ) |
| 96 | ]; |
| 97 | |
| 98 | $data = substr( $bytes, 12 ); |
| 99 | for ( $i = 0; $i < strlen( $data ); $i += 2 ) { |
| 100 | $unmaskedPayload .= chr( $mask[( $i / 2 ) % 4] ^ hexdec( substr( $data, $i, 2 ) ) ); |
| 101 | } |
| 102 | |
| 103 | return $unmaskedPayload; |
| 104 | } |
| 105 | |
| 106 | // Function to encode WebSocket frame |
| 107 | function encodeWebSocketData( $data ) { |
| 108 | $frame = []; |
| 109 | $frame[0] = 129; |
| 110 | $length = strlen( $data ); |
| 111 | |
| 112 | if ( $length <= 125 ) { |
| 113 | $frame[1] = $length; |
| 114 | } |
| 115 | else if ( $length >= 126 && $length <= 65535 ) { |
| 116 | $frame[1] = 126; |
| 117 | $frame[2] = ( $length >> 8 ) & 255; |
| 118 | $frame[3] = $length & 255; |
| 119 | } |
| 120 | |
| 121 | for ( $i = 0; $i < $length; $i++ ) { |
| 122 | $frame[] = ord( $data[$i] ); |
| 123 | } |
| 124 | |
| 125 | return call_user_func_array( 'pack', array_merge( ['C*'], $frame ) ); |
| 126 | } |
| 127 |