Created
December 15, 2011 03:32
-
-
Save abarth500/1479710 to your computer and use it in GitHub Desktop.
(Client) WebSocket Chat App. using node.js + websoket modute
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>node-websocket-server test</title> | |
</head> | |
<body> | |
<div><input id="msg" type="textfield"/><a href="#" id="send">Send</a></div> | |
<div id="log"></div> | |
<script type="text/javascript"> | |
var host = "foo.bar.com";// Your WebSocket Server | |
var port = 8123; // Server port | |
function log(data){ | |
document.getElementById("log").innerHTML += data+"<br />"; | |
} | |
var conn; | |
var connect = function() { | |
if (window["WebSocket"]) { | |
conn = new WebSocket("ws://"+host+":"+port+"/test"); | |
conn.onmessage = function(evt) { | |
log("RECV<<"+evt.data); | |
}; | |
conn.onerror = function() { | |
log("error", arguments); | |
}; | |
conn.onclose = function() { | |
log("closed"); | |
}; | |
conn.onopen = function() { | |
log("opened"); | |
}; | |
} | |
}; | |
document.getElementById("send").addEventListener("click", function(e) { | |
if (conn) { | |
var msg = document.getElementById("msg").value; | |
log("SEND>>"+msg); | |
conn.send(msg); | |
} | |
return false; | |
}, false); | |
window.onload = connect; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment