UPDATE 2019: many servers requires the key to be more unique that the original example, resulting in failure to establish upgrade connection. The key generation is now changed accordingly. Your header must contain:
to connect successfully. I have made this working example:
You can get the full version here: https://github.com/paragi/PHP-websocket-client gan tau gak erornya kenapa ni, di web socket.PHP Fatal error: Call to undefined function socket_create() in C:\xampp5.6\htdocs\dashboardreal\websocket\class.PHPWebSocket.php on line 103ini script servernya jika saya jalanin eror kayak diatas wsClose($clientID); return; } if ( sizeof($Server->wsClients) != 1 ) { //Send the message to everyone but the person who said it foreach ( $Server->wsClients as $id => $client ) { if ( $id != $clientID ) { $Server->wsSend($id, $message); } } } } // when a client connects function wsOnOpen($clientID) { global $Server; $ip = long2ip( $Server->wsClients[$clientID][6] ); $Server->log( "$ip ($clientID) has connected." ); } // when a client closes or lost connection function wsOnClose($clientID, $status) { global $Server; $ip = long2ip( $Server->wsClients[$clientID][6] ); $Server->log( "$ip ($clientID) has disconnected." ); } // start the server $Server = new PHPWebSocket(); $Server->bind('message', 'wsOnMessage'); $Server->bind('open', 'wsOnOpen'); $Server->bind('close', 'wsOnClose'); // for other computers to connect, you will probably need to change this to your LAN IP or external IP, // alternatively use: gethostbyaddr(gethostbyname($_SERVER['SERVER_NAME'])) $Server->wsStartServer('127.0.0.1', 9300); ?>ini class websocketnya function wsStartServer($host, $port) { if (isset($this->wsRead[0])) return false; if (!$this->wsRead[0] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) { return false; } if (!socket_set_option($this->wsRead[0], SOL_SOCKET, SO_REUSEADDR, 1)) { socket_close($this->wsRead[0]); return false; } if (!socket_bind($this->wsRead[0], $host, $port)) { socket_close($this->wsRead[0]); return false; } if (!socket_listen($this->wsRead[0], 10)) { socket_close($this->wsRead[0]); return false; } $write = array(); $except = array(); $nextPingCheck = time() + 1; while (isset($this->wsRead[0])) { $changed = $this->wsRead; $result = socket_select($changed, $write, $except, 1); if ($result === false) { socket_close($this->wsRead[0]); return false; } elseif ($result > 0) { foreach ($changed as $clientID => $socket) { if ($clientID != 0) { // client socket changed $buffer = ''; $bytes = @socket_recv($socket, $buffer, 4096, 0); if ($bytes === false) { // error on recv, remove client socket (will check to send close frame) $this->wsSendClientClose($clientID, self::WS_STATUS_PROTOCOL_ERROR); } elseif ($bytes > 0) { // process handshake or frame(s) if (!$this->wsProcessClient($clientID, $buffer, $bytes)) { $this->wsSendClientClose($clientID, self::WS_STATUS_PROTOCOL_ERROR); } } else { // 0 bytes received from client, meaning the client closed the TCP connection $this->wsRemoveClient($clientID); } } else { // listen socket changed $client = socket_accept($this->wsRead[0]); if ($client !== false) { // fetch client IP as integer $clientIP = ''; $result = socket_getpeername($client, $clientIP); $clientIP = ip2long($clientIP); if ($result !== false && $this->wsClientCount < self::WS_MAX_CLIENTS && (!isset($this->wsClientIPCount[$clientIP]) || $this->wsClientIPCount[$clientIP] < self::WS_MAX_CLIENTS_PER_IP)) { $this->wsAddClient($client, $clientIP); } else { socket_close($client); } } } } } if (time() >= $nextPingCheck) { $this->wsCheckIdleClients(); $nextPingCheck = time() + 1; } } return true; // returned when wsStopServer() is called } |