Skip to content

Instantly share code, notes, and snippets.

@afshinm
Created October 21, 2012 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afshinm/3928101 to your computer and use it in GitHub Desktop.
Save afshinm/3928101 to your computer and use it in GitHub Desktop.
Simple Chat Server with Socket.IO - Client Code
socket.on('connect', function () {
socket.on('message', function (data) {
var newElement = document.createElement('li');
newElement.innerHTML = data.message[0] + ' says: ' + data.message[1];
document.getElementById("list").appendChild(newElement);
});
socket.on('announcement', function (data) {
var newElement = document.createElement('li');
newElement.className = 'announcement';
newElement.innerHTML = data;
document.getElementById("list").appendChild(newElement);
});
socket.on('history', function (data) {
data.buffer.forEach(function (item) {
var newElement = document.createElement('li');
newElement.innerHTML = item.message[0] + ' says: ' + item.message[1];
document.getElementById("list").appendChild(newElement);
});
});
});
function send_message() {
socket.emit('message', document.getElementById("msg_input").value);
document.getElementById("msg_input").value = "";
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment