Created
July 9, 2017 04:45
-
-
Save downgoon/6fc30feec54976d72524672b5aa1482e to your computer and use it in GitHub Desktop.
websocket
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
<!DOCTYPE html> | |
<head> | |
<title>WebSocketd交互界面</title> | |
</head> | |
<body> | |
<pre id="vstdout"></pre> | |
<input type="text" name="cmd" id="vstdin" onkeydown = "if (event.keyCode == 13) | |
document.getElementById('btnsend').click()"/> | |
<button type="button" id="btnsend">send</button> | |
<script> | |
// helper function: log message to screen | |
function log(msg) { | |
document.getElementById('vstdout').textContent += msg + '\n'; | |
} | |
// setup websocket with callbacks | |
var ws = new WebSocket('ws://localhost:10086/'); | |
ws.onopen = function() { | |
log('CONNECT'); | |
}; | |
ws.onclose = function() { | |
log('DISCONNECT'); | |
}; | |
// be notified from server and log the message into 'vscreen' element | |
ws.onmessage = function(event) { | |
log('MESSAGE: ' + event.data); | |
}; | |
// add 'onclick' handler on 'send' Button | |
document.getElementById('btnsend').onclick = function () { | |
var cmdTxt = document.getElementById('vstdin').value; | |
// alert(cmdTxt); | |
ws.send(cmdTxt); | |
document.getElementById('vstdin').value = null; | |
}; | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment