Last active
August 29, 2015 13:56
-
-
Save IgorMuzyka/9250677 to your computer and use it in GitHub Desktop.
node.js socket.io chat in 44 lines
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script> | |
</head> | |
<body> | |
<input id="messageToSend" type="" /> | |
<input type="button" value="send" onclick="send()" /> | |
<br /><br /><br /> | |
<p id="talk"> | |
</p> | |
<script type="text/javascript"> | |
var messageToSend = document.querySelector("#messageToSend"); | |
var talk = document.querySelector("#talk"); | |
var chat = io.connect("http://localhost:8080/chat"); | |
chat.on("connect", function() { | |
console.log("connected"); | |
chat.emit("message", "hi"); | |
}); | |
chat.on("message", function(message) { | |
newText = JSON.stringify(message).split("\"")[1]; | |
talk.innerHTML += "<br />" + newText; | |
}); | |
var send = function() { | |
chat.emit("message", messageToSend.value); | |
messageToSend.value = ""; | |
} | |
</script> | |
</body> | |
</html> |
This file contains 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
io = require "socket.io" .listen 8080 | |
chat = io.of "/chat" .on "connection", (socket) -> | |
socket.on "message", (message) -> | |
chat.emit "message", "#{socket.id} says: #{message}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment