Add some web socket goodness.
This commit is contained in:
parent
04d90b7c7a
commit
74b370f19b
4 changed files with 73 additions and 5 deletions
25
public/chat.js
Normal file
25
public/chat.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
function sendMessage()
|
||||
{
|
||||
var msg = document.getElementById("inputLine")
|
||||
socket.send(msg.value);
|
||||
msg.value = "";
|
||||
return false;
|
||||
}
|
||||
|
||||
function connect(room, name)
|
||||
{
|
||||
socket = new WebSocket("ws://127.0.0.1:8080/ws?room="+encodeURIComponent(room)+"&name="+encodeURIComponent(name));
|
||||
|
||||
socket.onmessage = function(message) {
|
||||
var history = document.getElementById("history");
|
||||
var previous = history.innerHTML.trim();
|
||||
if (previous.length) previous = previous + "\n";
|
||||
history.innerHTML = previous + message.data;
|
||||
history.scrollTop = history.scrollHeight;
|
||||
}
|
||||
|
||||
socket.onclose = function() {
|
||||
console.log("socket closed - reconnecting...");
|
||||
connect();
|
||||
}
|
||||
}
|
||||
7
public/main.css
Normal file
7
public/main.css
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
textarea, input {
|
||||
width: 40em;
|
||||
}
|
||||
|
||||
textarea {
|
||||
resize: vertical;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue