2012-09-19 19:31:19 +08:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
|
<title>WebSocket Test</title>
|
|
|
|
|
<script language="javascript" type="text/javascript">
|
2012-09-20 20:45:35 +08:00
|
|
|
|
|
|
|
|
|
// This javascript code creates a websocket to the URI "/foo",
|
|
|
|
|
// sends a message to it, waits for the reply, and sends an "exit" message.
|
|
|
|
|
// Server must terminate the conversation after receiving "exit" message.
|
|
|
|
|
|
2012-09-19 19:31:19 +08:00
|
|
|
|
var writeToScreen = function(message) {
|
|
|
|
|
var div = document.createElement('div');
|
|
|
|
|
div.innerHTML = message;
|
|
|
|
|
document.getElementById('output').appendChild(div);
|
|
|
|
|
};
|
|
|
|
|
window.onload = function() {
|
|
|
|
|
var url = 'ws://' + window.location.host + '/foo';
|
|
|
|
|
websocket = new WebSocket(url);
|
|
|
|
|
websocket.onopen = function(ev) {
|
|
|
|
|
writeToScreen('CONNECTED');
|
|
|
|
|
var message = 'Не всё подчиняется разуму. Но всё подчиняется упорству. ';
|
|
|
|
|
writeToScreen('SENT: ' + message);
|
|
|
|
|
websocket.send(message);
|
|
|
|
|
};
|
|
|
|
|
websocket.onclose = function(ev) {
|
|
|
|
|
writeToScreen('DISCONNECTED');
|
|
|
|
|
};
|
|
|
|
|
websocket.onmessage = function(ev) {
|
|
|
|
|
writeToScreen('<span style="color: blue;">RESPONSE: ' + ev.data +
|
|
|
|
|
' </span>');
|
2012-09-20 20:45:35 +08:00
|
|
|
|
websocket.send('exit');
|
2012-09-19 19:31:19 +08:00
|
|
|
|
};
|
|
|
|
|
websocket.onerror = function(ev) {
|
|
|
|
|
writeToScreen('<span style="color: red; ">ERROR: </span> ' + ev.data);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
<h2>WebSocket Test</h2>
|
|
|
|
|
<div id="output"></div>
|
|
|
|
|
</html>
|