我是websockets技术的新手。我试图创建一个websockets php服务器,并与javascript客户端连接到服务器。我使用的是xampp 1.8.3.
我做了这个简单的PHP服务器:
<?php
error_reporting(E_ALL);
set_time_limit(0);
$address = "127.0.0.1";
$port = 1777;
$maxConnections = 10;
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0))){
$errorCode = socket_last_error();
$errorMsg = socket_strerror($errorCode);
die("socket_create() failed -> {$errorCode}:{$errorMsg}n");
}
echo "Server created.n";
if(!(socket_bind($sock, $address, $port))){
$errorCode = socket_last_error();
$errorMsg = socket_strerror($errorCode);
die("socket_bind() failed -> {$errorCode}:{$errorMsg}n");
}
echo "Server opened on {$address}:{$port}n";
if(!(socket_listen($sock, $maxConnections))){
$errorCode = socket_last_error();
$errorMsg = socket_strerror($errorCode);
die("socket_listen() failed -> {$errorCode}:{$errorMsg}n");
}
echo "Waiting for connections...n";
$client = socket_accept($sock);
if(socket_getpeername($client, $address, $port)){
echo "The client {$address}:{$port} is online.n";
}
$msg = socket_read($client, 1024000);
if(!(socket_write($client, $msg, strlen($msg)))){
$errorCode = socket_last_error();
$errorMsg = socket_strerror($errorCode);
die("socket_write() failed -> {$errorCode}:{$errorMsg}n");
}
echo "Message sentn";
socket_close($client);
socket_close($sock);
?>
我用下面的表达式运行这个php文件:
php htdocs/server.php
,我在shell上得到了这个消息:
php htdocs/server.php
Server created.
Server opened on 127.0.0.1:1777
Waiting for connections...
然后我在chrome上打开我的client.html(我支持websockets)。我的client.html代码是:
<html>
<head>
<meta charset="UTF-8" />
<title>Websockets web-server connection</title>
</head>
<body>
<p>Websockets connection. Status: <span id="status"></span></p>
<script type="text/javascript">
var webSockets = new WebSocket("ws://127.0.0.1:1777/");
webSockets.onopen = function(){
document.getElementById("status").innerHTML="connected";
webSockets.send("ping");
}
</script>
</body>
</html>
我在javascript控制台日志中收到了这个消息:
WebSocket connection to 'ws://127.0.0.1:1777/' failed: client.html:9
我的shell有这样的消息:
php htdocs/server.php
Server created.
Server opened on 127.0.0.1:1777
Waiting for connections...
The client 127.0.0.1:64446 is online
Message sent
但是我在客户端没有收到任何消息,我甚至没有得到连接状态。
发生了什么事?我的错误在哪里?
谢谢。
WebSockets不是原始的TCP socket。它们需要相当复杂的类似http的握手来建立连接,并且需要以非常特殊的方式对通过它们传输的数据进行编码和框架。协议定义在RFC 6455中。
除非你感到难以置信的受虐,否则你不会想自己实现这个。使用像Ratchet这样的库在PHP中实现WebSockets
我有同样的问题,兄弟,但不需要使用棘轮或其他库,你可以自己编写简单的代码。握手过程和消息的掩码—解掩码相当困难,因此我复制了
的代码function perform_handshaking($receved_header,$client_conn, $host, $port)
{
$headers = array();
$lines = preg_split("/rn/", $receved_header);
foreach($lines as $line)
{
$line = chop($line);
if(preg_match('/A(S+): (.*)z/', $line, $matches))
{
$headers[$matches[1]] = $matches[2];
}
}
$secKey = $headers['Sec-WebSocket-Key'];
$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
//hand shaking header
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshakern" .
"Upgrade: websocketrn" .
"Connection: Upgradern" .
"WebSocket-Origin: $hostrn" .
"WebSocket-Location: ws://$host:$port/rn".
"Sec-WebSocket-Accept:$secAcceptrnrn";
socket_write($client_conn,$upgrade,strlen($upgrade));
return $upgrade;
}
function unmask($text) {
$length = ord($text[1]) & 127;
if($length == 126) {
$masks = substr($text, 4, 4);
$data = substr($text, 8);
}
elseif($length == 127) {
$masks = substr($text, 10, 4);
$data = substr($text, 14);
}
else {
$masks = substr($text, 2, 4);
$data = substr($text, 6);
}
$text = "";
for ($i = 0; $i < strlen($data); ++$i) {
$text .= $data[$i] ^ $masks[$i%4];
}
return $text;
}
//Encode message for transfer to client.
function mask($text)
{
$b1 = 0x80 | (0x1 & 0x0f);
$length = strlen($text);
if($length <= 125)
$header = pack('CC', $b1, $length);
elseif($length > 125 && $length < 65536)
$header = pack('CCn', $b1, 126, $length);
elseif($length >= 65536)
$header = pack('CCNN', $b1, 127, $length);
return $header.$text;
}
代替socket_accept用户socket_read获取包含网页请求的http头,它想要将其http conn升级为websocket,然后使用上面的握手函数写一个accept头消息,然后你的连接将建立,但仍然在客户端你必须添加像这样的事件
if("WebSocket" in window){
var a = "ws://"+serverip+":9000";
var ws = new WebSocket(a);
var error = null;
ws.onopen = function(){
open();
}
ws.onerror = function(err){
errorhandler(err);
}
ws.onmessage = function(e){
messagehandler(e);
}
ws.onclose = function(){
close();
}
}
function open(){
document.getElementById('logs').innerHTML+='<p>WebSocket Connection OPened</p>';
}
function errorhandler(err){
document.getElementById('logs').innerHTML+='<p>WebSocket Connection Error occured  '+err.data+'</p>';
}
function messagehandler(a){
document.getElementById('logs').innerHTML+="<p>"+a.data+"</p>";
}
function close(){
document.getElementById('logs').innerHTML+='<p>WebSocket Connection Closed</p>';
ws = null;
}