#!/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);
?>
到“ws://example.app:8080/”的WebSocket连接失败:在收到握手响应之前连接已关闭
WebSocket协议不仅仅是一个原始的套接字连接,这是您的脚本试图实现的。
有用于PHP的websocket服务器库。我建议使用棘轮。这个库是一个很好的实现。
如果您真的想要自己实现WebSockets,那么您可能想看看RFC6455或其他标准,这取决于您的需要。
问题内容: 我的本地Chrome 67 Python 3.5.0 Selenium 3.12.0具有以下环境 我已经下载了2.39版的chromedriver 我有.py文件,如下所示 我收到以下错误。 我也尝试过使用其他网络驱动程序,如geckodriver.exe仍然相同的错误。 请帮助我解决此错误。 谢谢! 问题答案: 乍一看,您的代码试用似乎在 参数 execute_path* 的 值 中
我在设置打开班次时遇到问题,并在连接到我的服务器域后收到以下错误: 我不确定这是在告诉我做什么。我尝试按字面意思使用指令,但它无法识别命令。 有什么想法吗?
问题内容: 我不确定如何解决此问题 我不知道为什么在尝试以下操作时会出现此错误: 当我尝试连接到postgres时: 问题答案: 可能是一些问题: PostgreSQL没有运行。用sudo检查 你的PostgresSQl不在端口5432上运行。你可以检查其键入 尝试连接到数据库时出现错误,例如用户名,密码或数据库名。检查它们是否是postgres要求你连接的对象,并且这是你要访问的db_name。
在我做了和之后,我的postgres遇到了一些问题。我试着卸载postgres并重新安装它,但它并没有那么好用。 这就是我所做的: 现在,在我重新安装homebrew之后,当我使用时,它不显示任何错误消息。 但是我在我的Rails应用程序中运行,它显示: 更新 这对我也管用。
注意:连接是LAN,在隧道模式下工作正常,但速度较慢