[英]Can't send a message with the new websocket protocol
I am trying to develop a websocket server in php.我正在尝试在 php 中开发 websocket 服务器。
Here is what I have done so far: (server part)这是我到目前为止所做的:(服务器部分)
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($server, "localhost", 10000);
socket_listen($server);
$client = socket_accept($server);
$message = socket_read($client, 5000);
$matches = array();
preg_match('#Sec-WebSocket-Key: (.*)\r\n#', $message, $matches);
$new_key = new_key($matches[1]);
$new_message = "HTTP/1.1 101 Switching Protocols\r\n";
$new_message .= "Upgrade: websocket\r\n";
$new_message .= "Connection: Upgrade\r\n";
$new_message .= "Sec-WebSocket-Accept: " . $new_key . "\r\n\r\n";
socket_write($client, $new_message, strlen($new_message));
$new_message = "Test message !";
socket_write($client, $new_message, strlen($new_message));
function new_key($key)
{
$key .= "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
$key = sha1($key);
$new_key = '';
for ($i = 0; $i < strlen($key); $i+=2)
{
$new_key .= chr(intval($key[$i] . $key[$i+1], 16));
}
$new_key = base64_encode($new_key);
return $new_key;
}
/* End of file server.php */
(client part) (客户端部分)
window.onload = function() {
var ws = new WebSocket('ws://localhost:10000');
ws.onopen = function() {
alert('Connection open !');
setInterval(function() {
ws.send('lol');
}, 1000);
}
ws.onclose = function() {
alert('WebSocket close !');
}
ws.onmessage = function(e) {
alert(e.data);
}
ws.onerror = function(e) {
alert('Error');
}
};
The first socket_write
works perfectly (for the handshake) and the open
event is triggered correctly.第一个
socket_write
完美运行(用于握手)并且正确触发了open
事件。 But the second socket_write doesn't work.但是第二个 socket_write 不起作用。 I tried to do a
ws.send
on the open
event with a second socket_read
in server part, it doesn't work neither.我尝试在服务器部分使用第二个
socket_read
对open
事件执行ws.send
,但它也不起作用。
Thank you if you have an idea !谢谢你,如果你有一个想法!
I had a similar issue.我有一个类似的问题。 After checking out this project over here https://github.com/srchea/PHP-Push-WebSocket I noticed a function called "encode" in this file https://github.com/srchea/PHP-Push-WebSocket/blob/master/lib/Server.class.php before sending the message.
After checking out this project over here https://github.com/srchea/PHP-Push-WebSocket I noticed a function called "encode" in this file https://github.com/srchea/PHP-Push-WebSocket/blob /master/lib/Server.class.php在发送消息之前。
So try to change this所以试着改变这个
socket_write($client, $new_message, strlen($new_message));
$new_message = "Test message !";
socket_write($client, $new_message, strlen($new_message));
to this对此
socket_write($client, $new_message, strlen($new_message));
$new_message = "Test message !";
socket_write($client, chr(129) . chr(strlen($new_message)) ."". $new_message);
I think it is a INVALID_STATE_ERR: DOM Exception 11 meaning a sync problem.我认为这是一个 INVALID_STATE_ERR: DOM Exception 11 意味着同步问题。 Try this in your client:
在你的客户端试试这个:
var test = function(){
var myWebSocket = new WebSocket('ws://localhost:10000');
myWebSocket.onopen = function(e){
alert('connection open');
this.send('test1');
this.send('test2');
}
myWebSocket.onmessage = function(e){ alert('message ' + e.data) };
myWebSocket.onclose = function(e){ alert('connection closed'); this.close(); };
myWebSocket.send('test3');
};
test3 will fail because it is an asynchronous websocket. test3 将失败,因为它是异步 websocket。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.