Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <script type="text/javascript">
- var TestWebSocket = {
- socket: null,
- connect: (function() {
- var host = 'ws://' + window.location.host + '/TomcatWebSocketTest/testWebSocketServlet';
- if ('WebSocket' in window) {
- TestWebSocket.socket = new WebSocket(host);
- } else if ('MozWebSocket' in window) {
- TestWebSocket.socket = new MozWebSocket(host);
- } else {
- alert('Error: WebSocket is not supported by this browser.');
- return;
- }
- // process message from server
- TestWebSocket.socket.onmessage = function (message) {
- document.getElementById('content').innerHTML = message.data;
- };
- /* optional
- // do something while onopen/onclose if needed
- TestWebSocket.socket.onopen = function () {
- alert('WebSocket opened.');
- };
- TestWebSocket.socket.onclose = function () {
- alert('WebSocket closed.');
- };
- */
- }),
- // send message to server
- trigger: (function() {
- TestWebSocket.socket.send('trigger');
- }),
- };
- TestWebSocket.connect();
- </script>
- </head>
- <body>
- <div id="content"></div>
- <button id="btn" onclick="TestWebSocket.trigger();">trigger</button>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment