Created
November 9, 2016 21:13
-
-
Save nodox/a78d41e682e704f6bf8a0cd2b4ff0ede to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="/socket.io/socket.io.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> | |
<script> | |
var socket = io.connect('http://localhost:8080'); | |
// on connection to server, ask for user's name with an anonymous callback | |
socket.on('connect', function(){ | |
// call the server-side function 'adduser' and send one parameter (value of prompt) | |
socket.emit('adduser', prompt("What's your name?")); | |
}); | |
// listener, whenever the server emits 'updatechat', this updates the chat body | |
socket.on('updatechat', function (username, data) { | |
$('#conversation').append('<b>'+username + ':</b> ' + data + '<br>'); | |
}); | |
// listener, whenever the server emits 'updaterooms', this updates the room the client is in | |
socket.on('updaterooms', function(rooms, current_room) { | |
$('#rooms').empty(); | |
$.each(rooms, function(key, value) { | |
if(value == current_room){ | |
$('#rooms').append('<div>' + value + '</div>'); | |
} | |
else { | |
$('#rooms').append('<div><a href="#" onclick="switchRoom(\''+value+'\')">' + value + '</a></div>'); | |
} | |
}); | |
}); | |
function switchRoom(room){ | |
socket.emit('switchRoom', room); | |
} | |
// on load of page | |
$(function(){ | |
// when the client clicks SEND | |
$('#datasend').click( function() { | |
var message = $('#data').val(); | |
$('#data').val(''); | |
// tell server to execute 'sendchat' and send along one parameter | |
socket.emit('sendchat', message); | |
}); | |
// when the client hits ENTER on their keyboard | |
$('#data').keypress(function(e) { | |
if(e.which == 13) { | |
$(this).blur(); | |
$('#datasend').focus().click(); | |
} | |
}); | |
}); | |
</script> | |
<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;"> | |
<b>ROOMS</b> | |
<div id="rooms"></div> | |
</div> | |
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;"> | |
<div id="conversation"></div> | |
<input id="data" style="width:200px;" /> | |
<input type="button" id="datasend" value="send" /> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment