gpt4 book ai didi

javascript - 浏览器无法连接到 php websocket 服务器

转载 作者:行者123 更新时间:2023-11-28 01:04:06 25 4
gpt4 key购买 nike

我想做的是设置一个 php websocket 服务器来运行聊天应用程序。我尝试过使用 Ratchet php,它可以工作,我可以启动并运行一个简单的聊天应用程序,但我意识到它不符合我的特定需求。

我的开发环境是在 ubuntu vm Homestead 盒子上设置的。

我将使用 this site 中的一个示例。我已经更改了我的网站的地址和端口。当我启动服务器时,它运行良好,并且我可以使用 telnet 进行连接。但是,当我通过 telnet 发送消息时,握手失败并且断开连接。

这是 php websocket 服务器代码(这些评论是给我的,这样我可以尝试更好地了解正在发生的事情):

#!/usr/local/bin/php
<?php
$port = 8080;

// create a streaming socket, of type TCP/IP
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// set the option to reuse the port
socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);

// "bind" the socket to the address to "localhost", on port $port
// so this means that all connections on this port are now our resposibility to send/recv data, disconnect, etc..
socket_bind($sock, 'example.app', $port);

// start listen for connections
socket_listen($sock);

// create a list of all the clients that will be connected to us..
// add the listening socket to this list
$clients = array($sock);

while (true) {
// create a copy, so $clients doesn't get modified by socket_select()
$read = $clients;

// get a list of all the clients that have data to be read from
// if there are no clients with data, go to next iteration
if (socket_select($read, $write = NULL, $except = NULL, 0) < 1)
continue;

// check if there is a client trying to connect
if (in_array($sock, $read)) {
// accept the client, and add him to the $clients array
$clients[] = $newsock = socket_accept($sock);

// send the client a welcome message
socket_write($newsock, "no noobs, but ill make an exception :)\n" .
"There are " . (count($clients) - 1) . " client(s) connected to the server\n");

socket_getpeername($newsock, $ip);
echo "New client connected: {$ip}\n";

// remove the listening socket from the clients-with-data array
$key = array_search($sock, $read);
unset($read[$key]);
}

// loop through all the clients that have data to read from
foreach ($read as $read_sock) {
// read until newline or 1024 bytes
// socket_read while show errors when the client is disconnected, so silence the error messages
$data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);

// check if the client is disconnected
if ($data === false) {
// remove client for $clients array
$key = array_search($read_sock, $clients);
unset($clients[$key]);
echo "client disconnected.\n";
// continue to the next client to read from, if any
continue;
}

// trim off the trailing/beginning white spaces
$data = trim($data);

// check if there is any data after trimming off the spaces
if (!empty($data)) {

// send this to all the clients in the $clients array (except the first one, which is a listening socket)
foreach ($clients as $send_sock) {

// if its the listening sock or the client that we got the message from, go to the next one in the list
if ($send_sock == $sock || $send_sock == $read_sock)
continue;

// write the message to the client -- add a newline character to the end of the message
socket_write($send_sock, $data . "\n");
} // end of broadcast foreach
}
} // end of reading foreach
}

// close the listening socket
socket_close($sock);
?>

浏览器控制台向我显示此错误:

WebSocket connection to 'ws://example.app:8080/' failed: Connection closed before receiving a handshake response

最佳答案

WebSocket 协议(protocol)不仅仅是原始套接字连接,这正是您的脚本试图完成的任务。

有 PHP 的 websocket 服务器库。我建议使用 Ratchet 。这个库是一个很好的实现。

如果您确实想自己实现 WebSockets,您可能需要看看 RFC 6455或其他标准之一,具体取决于您的需要。

另一方面,如果您正在寻找发布/订阅和/或远程过程调用功能,您可以查看 Thruway这是 WAMP (Web 应用程序消息传递协议(protocol))PHP 的客户端和服务器库。

披露:我是 Thruway 的维护者。

关于javascript - 浏览器无法连接到 php websocket 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25279695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com