开发者

How can I have instant chat capabilities on my site using PHP?

I'm looking to create a chat using PHP. Would using MySQL be a good idea? I realize that you'll need more than just PHP to build a c开发者_StackOverflow社区hat but how do I do this? How can this be easily done? Where do I start?


By "instant" you might want to look into sockets

When information is sent across the Internet, it is usually split into packets. This allows the sending of large files in many smaller pieces of information to be later assembled on the other end.

There are two different protocols for splitting the information into packets, depending on the information type being sent and delivery requirements. TCP (Transmission Control Protocol) – the transmitted packets are numbered and are assembled on the other end, they are assembled in order to form the entire message. TCP usually runs over IP (Internet Protocol) hence the term TCP/IP.

TCP ensures that no data is lost (if a packet is lost it will be re-transmitted), and is therefore well suited for sending images, files or other information that must be received whole and integral (like your email). UDP (User Datagram Protocol) – this is a connectionless protocol. Like TCP it can run over the IP protocol. The difference is that UDP provides few error recovery services and so there is no guarantee that a particular packet will be received on the other side, or in what order the packets will be received.

Sample:

<?php 
// Set time limit to indefinite execution 
set_time_limit (0); 

// Set the ip and port we will listen on 
$address = '192.168.0.100'; 
$port = 9000; 

// Create a TCP Stream socket 
$sock = socket_create(AF_INET, SOCK_STREAM, 0); 
// Bind the socket to an address/port 
socket_bind($sock, $address, $port) or die('Could not bind to address'); 
// Start listening for connections 
socket_listen($sock); 

/* Accept incoming requests and handle them as child processes */ 
$client = socket_accept($sock); 

// Read the input from the client &#8211; 1024 bytes 
$input = socket_read($client, 1024); 

// Strip all white spaces from input 
$output = ereg_replace("[ \t\n\r]","",$input).chr(0); 

// Display output back to client 
socket_write($client, $output); 

// Close the client (child) socket 
socket_close($client); 

// Close the master sockets 
socket_close($sock); 
?>

You can read more in depth on how to code a socket-based chat server here: http://devzone.zend.com/article/1086


You can download APE, it's really good and cool :)
Or try to build your own one with PHP/MySQL,AJAX but the problem with DB is high usage :(


You can use sockets and @AlienWebguy said, however there isn't much around about how you can integrate socket based chats with MySQL.

I have done a few tests and have a basic example working here: https://github.com/andrefigueira/PHP-MySQL-Sockets-Chat

It makes use of Ratchet (http://socketo.me/) for the creation of the chat server in PHP.

And you can send chat messages to the DB by sending the server JSON with the information of who is chatting, (if of course you have user sessions)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜