PHP客户端Web套接字发送消息
Posted
技术标签:
【中文标题】PHP客户端Web套接字发送消息【英文标题】:PHP client Web socket to send messages 【发布时间】:2017-08-14 18:24:36 【问题描述】:我有以下代码
index.php
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
.panel
margin-right: 3px;
.button
background-color: #4CAF50;
border: none;
color: white;
margin-right: 30%;
margin-left: 30%;
text-decoration: none;
display: block;
font-size: 16px;
cursor: pointer;
width:30%;
height:40px;
margin-top: 5px;
input[type=text]
width:100%;
margin-top:5px;
.chat_wrapper
width: 70%;
height:472px;
margin-right: auto;
margin-left: auto;
background: #3B5998;
border: 1px solid #999999;
padding: 10px;
font: 14px 'lucida grande',tahoma,verdana,arial,sans-serif;
.chat_wrapper .message_box
background: #F7F7F7;
height:350px;
overflow: auto;
padding: 10px 10px 20px 10px;
border: 1px solid #999999;
.chat_wrapper input
//padding: 2px 2px 2px 5px;
.system_msgcolor: #BDBDBD;font-style: italic;
.user_namefont-weight:bold;
.user_messagecolor: #88B6E0;
@media only screen and (max-width: 720px)
/* For mobile phones: */
.chat_wrapper
width: 95%;
height: 40%;
.button width:100%;
margin-right:auto;
margin-left:auto;
height:40px;
</style>
</head>
<body>
<?php
$colours = array('007AFF','FF7000','FF7000','15E25F','CFC700','CFC700','CF1100','CF00BE','F00');
$user_colour = array_rand($colours);
?>
<script src="jquery-3.1.1.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function()
//create a new WebSocket object.
var wsUri = "ws://localhost:9000/demo/server.php";
websocket = new WebSocket(wsUri);
websocket.onopen = function(ev) // connection is open
$('#message_box').append("<div class=\"system_msg\">Connected!</div>"); //notify user
$('#send-btn').click(function() //use clicks message send button
var mymessage = $('#message').val(); //get message text
var myname = $('#name').val(); //get user name
if(myname == "") //empty name?
alert("Enter your Name please!");
return;
if(mymessage == "") //emtpy message?
alert("Enter Some message Please!");
return;
document.getElementById("name").style.visibility = "hidden";
var objDiv = document.getElementById("message_box");
objDiv.scrollTop = objDiv.scrollHeight;
//prepare json data
var msg =
message: mymessage,
name: myname,
color : '<?php echo $colours[$user_colour]; ?>'
;
//convert and send data to server
websocket.send(JSON.stringify(msg));
);
//#### Message received from server?
websocket.onmessage = function(ev)
var msg = JSON.parse(ev.data); //PHP sends Json data
var type = msg.type; //message type
var umsg = msg.message; //message text
var uname = msg.name; //user name
var ucolor = msg.color; //color
if(type == 'usermsg')
$('#message_box').append("<div><span class=\"user_name\" style=\"color:#"+ucolor+"\">"+uname+"</span> : <span class=\"user_message\">"+umsg+"</span></div>");
if(type == 'system')
$('#message_box').append("<div class=\"system_msg\">"+umsg+"</div>");
$('#message').val(''); //reset text
var objDiv = document.getElementById("message_box");
objDiv.scrollTop = objDiv.scrollHeight;
;
websocket.onerror = function(ev)$('#message_box').append("<div class=\"system_error\">Error Occurred - "+ev.data+"</div>");;
websocket.onclose = function(ev)$('#message_box').append("<div class=\"system_msg\">Connection Closed</div>");;
);
</script>
<div class="chat_wrapper">
<div class="message_box" id="message_box"></div>
<div class="panel">
<input type="text" name="name" id="name" placeholder="Your Name" maxlength="15" />
<input type="text" name="message" id="message" placeholder="Message" maxlength="80"
onkeydown = "if (event.keyCode == 13)document.getElementById('send-btn').click()" />
</div>
<button id="send-btn" class=button>Send</button>
</div>
</body>
</html>
server.php
<?php
$host = 'localhost'; //host
$port = '9000'; //port
$null = NULL; //null var
//Create TCP/IP sream socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//reuseable port
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
//bind socket to specified host
socket_bind($socket, 0, $port);
//listen to port
socket_listen($socket);
//create & add listning socket to the list
$clients = array($socket);
//start endless loop, so that our script doesn't stop
while (true)
//manage multipal connections
$changed = $clients;
//returns the socket resources in $changed array
socket_select($changed, $null, $null, 0, 10);
//check for new socket
if (in_array($socket, $changed))
$socket_new = socket_accept($socket); //accpet new socket
$clients[] = $socket_new; //add socket to client array
$header = socket_read($socket_new, 1024); //read data sent by the socket
perform_handshaking($header, $socket_new, $host, $port); //perform websocket handshake
socket_getpeername($socket_new, $ip); //get ip address of connected socket
$response = mask(json_encode(array('type'=>'system', 'message'=>$ip.' connected'))); //prepare json data
send_message($response); //notify all users about new connection
//make room for new socket
$found_socket = array_search($socket, $changed);
unset($changed[$found_socket]);
//loop through all connected sockets
foreach ($changed as $changed_socket)
//check for any incomming data
while(socket_recv($changed_socket, $buf, 1024, 0) >= 1)
$received_text = unmask($buf); //unmask data
$tst_msg = json_decode($received_text); //json decode
$user_name = $tst_msg->name; //sender name
$user_message = $tst_msg->message; //message text
$user_color = $tst_msg->color; //color
//prepare data to be sent to client
$response_text = mask(json_encode(array('type'=>'usermsg', 'name'=>$user_name, 'message'=>$user_message, 'color'=>$user_color)));
send_message($response_text); //send data
break 2; //exist this loop
$buf = @socket_read($changed_socket, 1024, PHP_NORMAL_READ);
if ($buf === false) // check disconnected client
// remove client for $clients array
$found_socket = array_search($changed_socket, $clients);
socket_getpeername($changed_socket, $ip);
unset($clients[$found_socket]);
//notify all users about disconnected connection
$response = mask(json_encode(array('type'=>'system', 'message'=>$ip.' disconnected')));
send_message($response);
// close the listening socket
socket_close($socket);
function send_message($msg)
global $clients;
foreach($clients as $changed_socket)
@socket_write($changed_socket,$msg,strlen($msg));
return true;
//Unmask incoming framed message
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;
//handshake new client.
function perform_handshaking($receved_header,$client_conn, $host, $port)
$headers = array();
$lines = preg_split("/\r\n/", $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 Handshake\r\n" .
"Upgrade: websocket\r\n" .
"Connection: Upgrade\r\n" .
"WebSocket-Origin: $host\r\n" .
"WebSocket-Location: ws://$host:$port/demo/shout.php\r\n".
"Sec-WebSocket-Accept:$secAccept\r\n\r\n";
socket_write($client_conn,$upgrade,strlen($upgrade));
上面的代码工作正常。但是现在我正在寻找客户端作为 php 。任何人都可以为我提供一些使用 php 制作客户端 websocket 的示例
更新
我需要通过 websocket 使用 php 或 javascript 库发送字节数组
更新 2 我找到了简单的 websocket 类,但不知道如何使用
https://github.com/paragi/PHP-websocket-client/blob/master/websocket_client.php
也根据我尝试过的 Niket Pathak 回答,但出现以下错误
<?php
$host = 'ws://echo.websocket.org'; // your websocket server
$port = 80;
$local = "http://localhost"; // url where this script run | Client
$data = '"id": 2,"command": "server_info"'; // data to be sent
$head = "GET / HTTP/1.1"."\r\n".
"Upgrade: WebSocket"."\r\n".
"Connection: Upgrade"."\r\n".
"Origin: $local"."\r\n".
"Host: $host"."\r\n".
"Sec-WebSocket-Version: 13"."\r\n".
"Sec-WebSocket-Key: asdasdaas76da7sd6asd6as7d"."\r\n".
"Content-Length: ".strlen($data)."\r\n"."\r\n";
// WebSocket handshake
$sock = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($sock, $head ) or die('error:'.$errno.':'.$errstr);
$headers = fread($sock, 2000);
fwrite($sock, hybi10Encode($data)) or die('error:'.$errno.':'.$errstr);
$wsdata = fread($sock, 2000);
fclose($sock);
var_dump(hybi10Decode($wsdata));
// hibi10 decoding of data
function hybi10Decode($data)
$bytes = $data;
$dataLength = '';
$mask = '';
$coded_data = '';
$decodedData = '';
$secondByte = sprintf('%08b', ord($bytes[1]));
$masked = ($secondByte[0] == '1') ? true : false;
$dataLength = ($masked === true) ? ord($bytes[1]) & 127 : ord($bytes[1]);
if($masked === true)
if ($dataLength === 126)
$mask = substr($bytes, 4, 4);
$coded_data = substr($bytes, 8);
elseif ($dataLength === 127)
$mask = substr($bytes, 10, 4);
$coded_data = substr($bytes, 14);
else
$mask = substr($bytes, 2, 4);
$coded_data = substr($bytes, 6);
for ($i = 0; $i < strlen($coded_data); $i++)
$decodedData .= $coded_data[$i] ^ $mask[$i % 4];
else
if ($dataLength === 126)
$decodedData = substr($bytes, 4);
elseif ($dataLength === 127)
$decodedData = substr($bytes, 10);
else
$decodedData = substr($bytes, 2);
return $decodedData;
// hibi10 encoding of data
function hybi10Encode($payload, $type = 'text', $masked = true)
$frameHead = array();
$frame = '';
$payloadLength = strlen($payload);
switch ($type)
case 'text':
// first byte indicates FIN, Text-Frame (10000001):
$frameHead[0] = 129;
break;
case 'close':
// first byte indicates FIN, Close Frame(10001000):
$frameHead[0] = 136;
break;
case 'ping':
// first byte indicates FIN, Ping frame (10001001):
$frameHead[0] = 137;
break;
case 'pong':
// first byte indicates FIN, Pong frame (10001010):
$frameHead[0] = 138;
break;
// set mask and payload length (using 1, 3 or 9 bytes)
if ($payloadLength > 65535)
$payloadLengthBin = str_split(sprintf('%064b', $payloadLength), 8);
$frameHead[1] = ($masked === true) ? 255 : 127;
for ($i = 0; $i < 8; $i++)
$frameHead[$i + 2] = bindec($payloadLengthBin[$i]);
// most significant bit MUST be 0 (close connection if frame too big)
if ($frameHead[2] > 127)
$this->close(1004);
return false;
elseif ($payloadLength > 125)
$payloadLengthBin = str_split(sprintf('%016b', $payloadLength), 8);
$frameHead[1] = ($masked === true) ? 254 : 126;
$frameHead[2] = bindec($payloadLengthBin[0]);
$frameHead[3] = bindec($payloadLengthBin[1]);
else
$frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;
// convert frame-head to string:
foreach (array_keys($frameHead) as $i)
$frameHead[$i] = chr($frameHead[$i]);
if ($masked === true)
// generate a random mask:
$mask = array();
for ($i = 0; $i < 4; $i++)
$mask[$i] = chr(rand(0, 255));
$frameHead = array_merge($frameHead, $mask);
$frame = implode('', $frameHead);
// append payload to frame:
for ($i = 0; $i < $payloadLength; $i++)
$frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i];
return $frame;
错误
警告:fsockopen():无法连接到 ws://echo.websocket.org:80 (无法找到套接字传输“ws” - 您是否忘记启用 当你配置 PHP 的时候它?)在 G:\XAMPP\htdocs\a\server.php 在线 16
警告:fwrite() 期望参数 1 是资源,布尔值在 G:\XAMPP\htdocs\a\server.php 第 17 行错误:407024:无法找到 套接字传输“ws” - 您是否忘记启用它 配置PHP?
【问题讨论】:
主要是因为最后一段,这似乎是一个过于宽泛的问题。 "请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。避免一次提出多个不同的问题。请参阅How to Ask 页面以获得澄清此问题的帮助。" 您可以使用 Ratchet Websocket (socketo.me),但它还是一个服务器端实现。我在这里用 CodeIgniter 框架做了一些事情。请通过它。 github.com/kishor10d/CodeIgniter-Ratchet-Websocket @kishor10d.是否可以发送字节数组 试试pieSocket.com,如果你正在寻找库来做这件事,他们有PHP SDKs 【参考方案1】:如果您正在谈论使用 Php 连接到您的 Websocket,那么 是的,这是可行的。有一些库,如 Php WebSocket client 或 Websocket php,可以作为客户端连接到您的 websocket。
您也可以查看此Websocket Client 示例。
另一个工作示例,不使用任何库并按照specification 实现hybi10 帧编码:
$host = 'www.host.com'; // your websocket server
$port = 443;
$local = "http://localhost"; // url where this script runs | Client
$data = '"id": 2,"command": "server_info"'; // data to be sent
$head = "GET / HTTP/1.1"."\r\n".
"Upgrade: WebSocket"."\r\n".
"Connection: Upgrade"."\r\n".
"Origin: $local"."\r\n".
"Host: $host"."\r\n".
"Sec-WebSocket-Version: 13"."\r\n".
"Sec-WebSocket-Key: asdasdaas76da7sd6asd6as7d"."\r\n".
"Content-Length: ".strlen($data)."\r\n"."\r\n";
// WebSocket handshake
$sock = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($sock, $head ) or die('error:'.$errno.':'.$errstr);
$headers = fread($sock, 2000);
fwrite($sock, hybi10Encode($data)) or die('error:'.$errno.':'.$errstr);
$wsdata = fread($sock, 2000);
fclose($sock);
var_dump(hybi10Decode($wsdata));
// hibi10 decoding of data
function hybi10Decode($data)
$bytes = $data;
$dataLength = '';
$mask = '';
$coded_data = '';
$decodedData = '';
$secondByte = sprintf('%08b', ord($bytes[1]));
$masked = ($secondByte[0] == '1') ? true : false;
$dataLength = ($masked === true) ? ord($bytes[1]) & 127 : ord($bytes[1]);
if($masked === true)
if ($dataLength === 126)
$mask = substr($bytes, 4, 4);
$coded_data = substr($bytes, 8);
elseif ($dataLength === 127)
$mask = substr($bytes, 10, 4);
$coded_data = substr($bytes, 14);
else
$mask = substr($bytes, 2, 4);
$coded_data = substr($bytes, 6);
for ($i = 0; $i < strlen($coded_data); $i++)
$decodedData .= $coded_data[$i] ^ $mask[$i % 4];
else
if ($dataLength === 126)
$decodedData = substr($bytes, 4);
elseif ($dataLength === 127)
$decodedData = substr($bytes, 10);
else
$decodedData = substr($bytes, 2);
return $decodedData;
// hibi10 encoding of data
function hybi10Encode($payload, $type = 'text', $masked = true)
$frameHead = array();
$frame = '';
$payloadLength = strlen($payload);
switch ($type)
case 'text':
// first byte indicates FIN, Text-Frame (10000001):
$frameHead[0] = 129;
break;
case 'close':
// first byte indicates FIN, Close Frame(10001000):
$frameHead[0] = 136;
break;
case 'ping':
// first byte indicates FIN, Ping frame (10001001):
$frameHead[0] = 137;
break;
case 'pong':
// first byte indicates FIN, Pong frame (10001010):
$frameHead[0] = 138;
break;
// set mask and payload length (using 1, 3 or 9 bytes)
if ($payloadLength > 65535)
$payloadLengthBin = str_split(sprintf('%064b', $payloadLength), 8);
$frameHead[1] = ($masked === true) ? 255 : 127;
for ($i = 0; $i < 8; $i++)
$frameHead[$i + 2] = bindec($payloadLengthBin[$i]);
// most significant bit MUST be 0 (close connection if frame too big)
if ($frameHead[2] > 127)
$this->close(1004);
return false;
elseif ($payloadLength > 125)
$payloadLengthBin = str_split(sprintf('%016b', $payloadLength), 8);
$frameHead[1] = ($masked === true) ? 254 : 126;
$frameHead[2] = bindec($payloadLengthBin[0]);
$frameHead[3] = bindec($payloadLengthBin[1]);
else
$frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;
// convert frame-head to string:
foreach (array_keys($frameHead) as $i)
$frameHead[$i] = chr($frameHead[$i]);
if ($masked === true)
// generate a random mask:
$mask = array();
for ($i = 0; $i < 4; $i++)
$mask[$i] = chr(rand(0, 255));
$frameHead = array_merge($frameHead, $mask);
$frame = implode('', $frameHead);
// append payload to frame:
for ($i = 0; $i < $payloadLength; $i++)
$frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i];
return $frame;
最后,您可以将字节数据作为 JSON 对象发送,方法是先将其编码为 base64。 检查此SO Post 以了解如何执行此操作。希望这会有所帮助。
【讨论】:
@niket。感谢您的回答。但我只需要发送字节数组而不编码,因为 c 程序代码是这样写的 我通过更改主机运行您的上述代码,但出现错误。警告:fsockopen(): 无法连接到 G:\XAMPP\ 中的 ws://echo.websocket.org:80(无法找到套接字传输“ws” - 您是否在配置 PHP 时忘记启用它?)第 16 行的 htdocs\a\server.php 警告:fwrite() 期望参数 1 是资源,在第 17 行的 G:\XAMPP\htdocs\a\server.php 中给出的布尔值错误:407024:无法找到套接字传输"ws" - 你在配置 PHP 时忘记启用它了吗? 你的similar problem。也许您可以尝试那里列出的解决方案。或者也许使用图书馆。我已更新我的答案以包含一个易于使用的库。 @Niket.thanks ya 我已经尝试过了,但是我们只发送字符串,否则我们必须发送序列化数据 @iCoders,您已使用$host = 'ws://echo.websocket.org';
作为主机。您不能使用 ws:
进行握手。检查握手所需的fsockopen() 和supported transport protocols 的文档。【参考方案2】:
我没有回答。您不能完全使用 websocket 客户端逻辑,仅使用服务器部分(例如 php)。因为服务器端对浏览器一无所知。服务器 - 接收请求并返回静态结果。浏览器中的所有魔法都是 javascript。 Javascript 可以动态侦听所有用户事件,向服务器发布查询并将服务器的响应插入页面。如果没有浏览器中的 javascript,Websocket 将无法工作。 这是 WebSocket 定义:
WebSockets 是一种双向、全双工、持久的连接 从网络浏览器到服务器。一旦 WebSocket 连接 建立连接保持打开状态,直到客户端或服务器 决定关闭此连接。有了这个开放的连接, 客户端或服务器可以在任何给定时间向对方发送消息。 这使得网络编程完全由事件驱动,而不是(只是)用户 发起。它是有状态的。同样,此时,单次运行 服务器应用程序知道所有连接,允许您 在任何给定时间与任意数量的打开连接进行通信。
你唯一能做的就是学习node js。它将允许您使用一种语言(javascript)编写浏览器(客户端)部分和服务器部分。但是,这将是应用程序的不同部分,您将使用不同的方法来对每个部分进行编程
【讨论】:
@Nutscracker.thanks for answer.is there any php or javacript code to send byte array in wqebsocket.even library对我来说还可以 @iCoders 你可以使用javascript函数 ArrayBuffer developer.mozilla.org/en/docs/Web/JavaScript/Typed_arrays , developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/… 。 PHP 有方法打包和解包二进制数据 @Nutscracker。谢谢你能给我一个简单的例子来使用 php packe 和 unpack 发送字节数组。因为我已经尝试过了。***.com/questions/43092252/… @iCoders 也许这个库会帮助你github.com/jDataView/jBinary。【参考方案3】:这不是您想要的,但可以提供帮助。您可以将 Redis 用作中间件。使用 Redis 发布/订阅消息系统。 Redis publish/subscribe message system
【讨论】:
以上是关于PHP客户端Web套接字发送消息的主要内容,如果未能解决你的问题,请参考以下文章