Php + html5 websocket-based chat room method, html5websocket
This article describes how to implement chat rooms using websocket in php + html5. Share it with you for your reference. The details are as follows:
Html5 websocket implements two-way communication. After a few days, I got a chat room and shared it with you.
<? Phperror_reporting (E_ALL); ob_implicit_flush (); $ sk = new Sock ('2017. 0.0.1 ', 8000); $ sk-> run (); class Sock {public $ sockets; public $ users; public $ master; public function _ construct ($ address, $ port) {$ this-> master = $ this-> WebSocket ($ address, $ port ); $ this-> sockets = array ('s '=> $ this-> master);} function run () {while (true) {$ changes = $ this-> sockets; socket_select ($ changes, $ write = NULL, $ response T = NULL, NULL); forea Ch ($ changes as $ sock) {if ($ sock =$ this-> master) {$ client = socket_accept ($ this-> master ); // $ key = uniqid (); $ this-> sockets [] = $ client; $ this-> users [] = array ('socket '=> $ client, 'shou' => false);} else {$ len = socket_recv ($ sock, $ buffer, 2048,0); $ k = $ this-> search ($ sock ); if ($ len <7) {$ name = $ this-> users [$ k] ['ming']; $ this-> close ($ sock ); $ this-> send2 ($ name, $ k); continue;} if (! $ This-> users [$ k] ['shou']) {$ this-> woshou ($ k, $ buffer );} else {$ buffer = $ this-> uncode ($ buffer); $ this-> send ($ k, $ buffer) ;}}} function close ($ sock) {$ k = array_search ($ sock, $ this-> sockets); socket_close ($ sock); unset ($ this-> sockets [$ k]); unset ($ this-> users [$ k]); $ this-> e ("key: $ k close");} function search ($ sock) {foreach ($ this-> users as $ k => $ v) {if ($ sock = $ v ['socket ']) return $ k;} return false ;} Function WebSocket ($ address, $ port) {$ server = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); socket_set_option ($ server, SOL_SOCKET, SO_REUSEADDR, 1 ); socket_bind ($ server, $ address, $ port); socket_listen ($ server); $ this-> e ('server Started :'. date ('Y-m-d H: I: s'); $ this-> e ('listening on :'. $ address. 'Port '. $ port); return $ server;} function woshou ($ k, $ buffer) {$ buf = substr ($ buffer, strpos ($ buf Fer, 'sec-WebSocket-Key: ') + 18); $ key = trim (substr ($ buf, 0, strpos ($ buf, "\ r \ n"); $ new_key = base64_encode (sha1 ($ key. "258eafa5-e914-4710995ca-c5ab0dc85b11", true); $ new_message = "HTTP/1.1 101 Switching Protocols \ r \ n"; $ new_message. = "Upgrade: websocket \ r \ n"; $ new_message. = "Sec-WebSocket-Version: 13 \ r \ n"; $ new_message. = "Connection: Upgrade \ r \ n"; $ new_message. = "Sec-WebSocket-Accept :". $ new_k Ey. "\ r \ n"; socket_write ($ this-> users [$ k] ['socket '], $ new_message, strlen ($ new_message )); $ this-> users [$ k] ['shou'] = true; return true;} function uncode ($ str) {$ mask = array (); $ data = ''; $ msg = unpack ('H * ', $ str); $ head = substr ($ msg [1 ); if (hexdec ($ head {1}) = 8) {$ data = false;} else if (hexdec ($ head {1}) = 1) {$ mask [] = hexdec (substr ($ msg [1],); $ mask [] = hexdec (substr ($ msg [1], 6, 2) ); $ Mask [] = hexdec (substr ($ msg [1], 8, 2); $ mask [] = hexdec (substr ($ msg [1], 10, 2); $ s = 12; $ e = strlen ($ msg [1])-2; $ n = 0; for ($ I = $ s; $ I <= $ e; $ I + = 2) {$ data. = chr ($ mask [$ n % 4] ^ hexdec (substr ($ msg [1], $ I, 2); $ n ++ ;}} return $ data;} function code ($ msg) {$ msg = preg_replace (array ('/\ r$/', '/\ n $ /', '/\ r \ n $/',), ', $ msg); $ frame = array (); $ frame [0] = '81 '; $ len = strlen ($ msg); $ frame [1] = $ len <16? '0 '. dechex ($ len): dechex ($ len); $ frame [2] = $ this-> ord_hex ($ msg); $ data = implode ('', $ frame ); return pack ("H *", $ data);} function ord_hex ($ data) {$ msg = ''; $ l = strlen ($ data ); for ($ I = 0; $ I <$ l; $ I ++) {$ msg. = dechex (ord ($ data {$ I});} return $ msg;} function send ($ k, $ msg) {/* $ this-> send1 ($ k, $ this-> code ($ msg), 'all'); */parse_str ($ msg, $ g ); $ this-> e ($ msg); $ ar = array (); if ($ g ['type'] = 'add') {$ this -> Users [$ k] ['ming'] = $ g ['ming']; $ ar ['add'] = true; $ ar ['nrong '] = 'Welcome '. $ g ['ming']. 'join! '; $ Ar ['users'] = $ this-> getusers (); $ key = 'all ';} else if ($ g ['type'] = 'ltidao') {$ ar ['nrong '] = $ g ['nr']; $ key = $ g ['key'] ;}$ msg = json_encode ($ ar); $ this-> e ($ msg ); $ msg = $ this-> code ($ msg); $ this-> send1 ($ k, $ msg, $ key ); // socket_write ($ this-> users [$ k] ['socket '], $ msg, strlen ($ msg);} function getusers () {$ ar = array (); foreach ($ this-> users as $ k => $ v) {$ ar [$ k] = $ v ['ming'];} return $ ar;} function send1 ($ k, $ str, $ Key = 'all') {if ($ key = 'all') {foreach ($ this-> users as $ v) {socket_write ($ v ['socket '], $ str, strlen ($ str) ;}} else {if ($ k! = $ Key) socket_write ($ this-> users [$ k] ['socket '], $ str, strlen ($ str )); socket_write ($ this-> users [$ key] ['socket '], $ str, strlen ($ str) ;}} function send2 ($ ming, $ k) {$ ar ['delete'] = true; $ ar ['removekey'] = $ k; $ ar ['nrong '] = $ ming. 'Exit the chatroom '; $ str = $ this-> code (json_encode ($ ar); $ this-> send1 (false, $ str, 'all ');} function e ($ str) {$ path = dirname (_ FILE __). '/log.txt'; $ str = $ str. "\ n"; error_log ($ str, 3, $ path); echo I Conv ('utf-8', 'gbk // IGNORE ', $ str) ;}}?>
I hope this article will help you with php programming.