Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
- <style>
- h3{
- font-size:12px;
- margin-bottom:3px;
- margin-top:0px;
- }
- </style>
- </head>
- <body>
- <input type="text" value="" id="txtMsg" />
- <button id="btnSend">Send</button>
- <div id="divHistory">
- <h3> </h3>
- </div>
- <script type="text/javascript">
- (function () {
- 'use strict';
- var socket = new WebSocket("ws://" + window.location.host + "/CustomWS.ashx"),
- log = function (text) {
- var d = new Date();
- $('<h3>' + d.getMinutes() + ':' + d.getSeconds() + '.' + d.getMilliseconds() + ' - ' + text + '</h3>').insertBefore("#divHistory h3:first-child");
- };
- socket.addEventListener("open", function (evt) {
- log('Connection Opened with the Echo server.');
- }, false);
- socket.addEventListener("message", function (evt) {
- log(evt.data);
- }, false);
- socket.addEventListener("error", function (evt) {
- log('Unexpected Error.');
- }, false);
- $("#btnSend").click(function () {
- if (socket.readyState == WebSocket.OPEN) {
- socket.send($("#txtMsg").val());
- }
- else {
- log('The underlying connection is closed.');
- }
- });
- })();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment