Skip to content

Commit

Permalink
Fix indentation and some other improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
simonewebdesign committed Dec 18, 2012
1 parent 15cbe21 commit eb60b07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 55 deletions.
64 changes: 11 additions & 53 deletions index.html
@@ -1,59 +1,17 @@
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>WebSocket Client</title>
<meta charset="utf-8">
<title>WebSocket Client Demo</title>
</head>
<body>
<h1>WebSocket Client</h1>
<p>Open your web console to see the response!</p>
<form>
<label for="message">Send a message</label>
<input id="message" name="message" type="text">
<button id="send" name="send">Send</button>
</form>

<!-- latest jQuery -->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<script>
var ws = new WebSocket("ws://localhost:1337");

ws.onopen = function(ev) {
console.log('Connection opened.');
}

ws.onmessage = function(ev) {
console.log('Response from server: ' + ev.data);
}

ws.onclose = function(ev) {
console.log('Connection closed.');
}

ws.onerror = function(ev) {
console.log('An error occurred. Sorry for that.');
}

WebSocket.prototype.sendMessage = function(msg) {
this.send(msg);
console.log('Message sent: ' + msg);
}

var sendBtn = document.getElementById('send');
var message = document.getElementById('message').value;

sendBtn.addEventListener('click', function(ev) {
ws.sendMessage(message);
ev.preventDefault();
});

/*
$('#send').on('click', function(ev) {
ws.sendMessage( $('#message').val() );
ev.preventDefault();
});
*/
</script>
<h1>WebSocket Client</h1>
<p>Open the JavaScript console to see the response!</p>
<form>
<label for="message">Send a message</label>
<input id="message" name="message" type="text">
<button id="send" name="send">Send</button>
</form>
<script src="ws_client.js"></script>
</body>
</html>
</html>
6 changes: 4 additions & 2 deletions ws_server.js
Expand Up @@ -18,7 +18,8 @@ wsServer = new WebSocketServer({
});

function isAllowedOrigin(origin) {
if (origin === 'http://localhost') {
valid_origins = ['http://localhost', '127.0.0.1'];
if (valid_origins.indexOf(origin) != -1) {
console.log('Connection accepted from origin ' + origin);
return true;
}
Expand Down Expand Up @@ -56,7 +57,8 @@ wsServer.on('request', function(request) {
response = 'Keep typing, man. Keep typing.';
break;
default:
response = "Hello. Uh... what am I supposed to do with '" + message.utf8Data + "'?";
response = "Hello. Uh... what am I supposed to do with '" +
message.utf8Data + "'?";
}
connection.sendUTF(response);
}
Expand Down

0 comments on commit eb60b07

Please sign in to comment.