从名字上也可看出,这是一个 WebSocket 的 PHP 实现。
示例客户端代码:
1 | var host =
"ws://localhost:12345/websocket/server.php" ; |
3 | socket =
new WebSocket(host); |
4 | log( 'WebSocket - status ' +socket.readyState); |
5 | socket.onopen =
function (msg){ log( "Welcome - status " +this.readyState); }; |
6 | socket.onmessage =
function (msg){ log( "Received: " +msg.data); }; |
7 | socket.onclose =
function (msg){ log( "Disconnected - status " +this.readyState); }; |
示例服务器端代码:
02 | list( $resource , $host , $origin ) = getheaders( $buffer ); |
03 | $upgrade =
"HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
. |
04 | "Upgrade: WebSocket\r\n"
. |
05 | "Connection: Upgrade\r\n"
. |
06 | "WebSocket-Origin: "
. $origin
. "\r\n" . |
07 | "WebSocket-Location: ws://"
. $host
. $resource . "\r\n"
. |
10 | socket_write( $socket , $upgrade . chr ( ), strlen ( $upgrade . chr (
))); |