Created
May 3, 2015 07:46
-
-
Save ajchemist/38ada9eeafdd3030b6e1 to your computer and use it in GitHub Desktop.
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> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> | |
| <script type="text/javascript"> | |
| $(function() { | |
| window.WebSocket = window.WebSocket || window.MozWebSocket; | |
| var websocket = new WebSocket('ws://127.0.0.1:9999','dumb-increment-protocol'); | |
| websocket.onopen = function () { | |
| $('h1').css('color', 'green'); | |
| }; | |
| websocket.onerror = function () { | |
| $('h1').css('color', 'red'); | |
| }; | |
| websocket.onmessage = function (message) { | |
| console.log(message.data); | |
| $('div').append($('<p>', { text: message.data })); | |
| }; | |
| $('button').click(function(e) { | |
| e.preventDefault(); | |
| websocket.send($('input').val()); | |
| $('input').val(''); | |
| }); | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <h1>WebSockets test</h1> | |
| <form> | |
| <input type="text" /> | |
| <button>Send</button> | |
| </form> | |
| <div></div> | |
| </body> | |
| </html> | |
| <!-- Local Variables: --> | |
| <!-- mode: web --> | |
| <!-- End: --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment