├── README.md ├── chat.css ├── chat.js ├── index.html └── wsphp ├── app.php └── cron.php /README.md: -------------------------------------------------------------------------------- 1 |
This is simple HTML chat with the following features:
4 | 1) Unlimited number of connections (users).Network communications are done using Websockets. 16 | WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection. 17 | That is why we can read (via ws.onmessage callback) and send messages (via ws.send) using only one websocket connection. 18 | WSPHP server takes care about websocket protocol actions: handshake, encoding/decoding frames, etc. 19 | PHP role is small - it just re-sends received message to all connected users. 20 | Messages are wrapped to JSON format for convenience. 21 |
22 | -------------------------------------------------------------------------------- /chat.css: -------------------------------------------------------------------------------- 1 | .top { background:#E0E0E0; padding:10px; font-weight:bold; } 2 | .top form { margin:0px; } 3 | .top input, .top select { margin-right:10px; } 4 | #chat { font-family:monospace,monospace; } -------------------------------------------------------------------------------- /chat.js: -------------------------------------------------------------------------------- 1 | var ws = false; 2 | var connected = false; 3 | 4 | function Reconnect() 5 | { var url=false; 6 | 7 | url = document.getElementById("server").innerHTML; 8 | if(!url || !url.length) 9 | { url = location.host; 10 | if(!url || !url.length) url="127.0.0.1:30403"; 11 | document.getElementById("server").innerHTML = url; 12 | } 13 | 14 | if(connected) 15 | { ChatMessage(false, "#999", "disconnected from server"); 16 | connected = false; 17 | } 18 | if(ws) ws.close(); 19 | else ChatMessage(false, "#AAA", "connecting to server..."); 20 | connected = false; 21 | ws = new WebSocket("ws://"+url+"/wsphp"); 22 | ws.onopen = function(evt) { onOpen(evt); }; 23 | ws.onclose = function(evt) { onClose(evt); }; 24 | ws.onmessage = function(evt) { onMessage(evt); }; 25 | ws.onerror = function(evt) { onError(evt); }; 26 | } 27 | 28 | function onOpen(evt) 29 | { if(!connected) 30 | { ChatMessage(false, "#999", "connected to server"); 31 | connected = true; 32 | } 33 | } 34 | 35 | function onClose(evt) 36 | { setTimeout(Reconnect, 1000); 37 | } 38 | 39 | function onMessage(evt) 40 | { try 41 | { onMessageJSON(JSON.parse(evt.data)); 42 | } catch(e) 43 | { ChatMessage(false, "#999999", "JSON error: "+evt.data.replace(//g,">")); 44 | } 45 | } 46 | 47 | function onMessageJSON(json) 48 | { if(window.console) console.log(json); 49 | if(json) 50 | { if("undefined" !== typeof json.notification) 51 | { ChatMessage(false, "#999999", "Someone "+(json.notification?"joined the chat":"left the chat")); 52 | } 53 | if("undefined" !== typeof json.message) 54 | { ChatMessage(json.name, json.color, json.message); 55 | } 56 | } 57 | } 58 | 59 | function onError(evt) 60 | { if(connected) ChatMessage(false, "#999999", "Websocket error"); 61 | } 62 | 63 | function OnServerChange(link) 64 | { var newServer = prompt("Enter new server name and port number:", link.innerHTML); 65 | if(newServer) 66 | { link.innerHTML=newServer; 67 | Reconnect(); 68 | } 69 | return false; 70 | } 71 | 72 | function ChatMessage(nick, color, message) 73 | { var s = (new Date().toLocaleString())+" "; 74 | if(color) s += ""; 75 | if(nick) s += ""+nick+": "; 76 | if(message) s += ""+message+""; 77 | if(color) s += ""; 78 | var chat = document.getElementById("chat"); 79 | chat.innerHTML = s + "