1<html> 2<head> 3<title>Server Test</title> 4<script language="JavaScript"> 5 6// Send a query to the browser process. 7function sendMessage(request, success_callback) { 8 // Results in a call to the OnQuery method in server_test.cc 9 window.cefQuery({ 10 request: JSON.stringify(request), 11 onSuccess: function(response) { 12 success_callback(response.length == 0 ? {} : JSON.parse(response)); 13 }, 14 onFailure: function(error_code, error_message) { 15 alert("Request failed with error " + error_message + "(" + error_code + ")"); 16 } 17 }); 18} 19 20function setButtonState(start_enabled, stop_enabled) { 21 document.getElementById('start').disabled = !start_enabled; 22 document.getElementById('stop').disabled = !stop_enabled; 23 document.getElementById('open').disabled = !stop_enabled; 24} 25 26function setup() { 27 if (location.origin != 'http://tests') { 28 document.getElementById('warning').style.display = 'block'; 29 return; 30 } 31 32 // Query the current server state. 33 sendMessage({'action':'query'}, function(response) { 34 if (response['result'] == 'success') { 35 var running = (response['status'] == 'running') 36 setButtonState(!running, running); 37 38 var port_element = document.getElementById('port'); 39 port_element.value = response['port']; 40 port_element.disabled = false; 41 } 42 }); 43} 44 45function startServer() { 46 var port = parseInt(document.getElementById('port').value); 47 if (port < 1025 || port > 65535) { 48 alert('Specify a port number between 1025 and 65535'); 49 return; 50 } 51 52 setButtonState(false, false); 53 54 sendMessage({'action':'start', 'port':port}, function(response) { 55 if (response['result'] == 'success') { 56 setButtonState(false, true); 57 } else { 58 setButtonState(true, false); 59 alert(response['message']); 60 } 61 }); 62} 63 64function stopServer() { 65 setButtonState(false, false); 66 67 sendMessage({'action':'stop'}, function(response) { 68 if (response['result'] == 'success') { 69 setButtonState(true, false); 70 } else { 71 setButtonState(false, true); 72 alert(response['message']); 73 } 74 }); 75} 76 77function openServer() { 78 var port = document.getElementById('port').value; 79 window.open('http://localhost:' + port); 80} 81 82</script> 83 84</head> 85<body bgcolor="white" onload="setup()"> 86<div id="warning" style="display:none;color:red;font-weight:bold;"> 87This page can only be run from the http://tests origin. 88</div> 89<p> 90This page starts an HTTP/WebSocket server on localhost with the specified port number. 91After starting the server click the "Open Example" button to open the WebSocket Client test in a popup window. 92</p> 93<p> 94With this example each browser window can create/manage a separate server instance. 95The server will be stopped automatically when the managing browser window is closed. 96</p> 97<form> 98Server port: <input type="text" id="port" value="" disabled="true"> 99<br/><input type="button" id="start" onclick="startServer()" value="Start Server" disabled="true"> 100<input type="button" id="stop" onclick="stopServer()" value="Stop Server" disabled="true"> 101<input type="button" id="open" onclick="openServer()" value="Open Example" disabled="true"> 102</form> 103</body> 104</html> 105