Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
- <style type="text/css">
- body {
- margin: 0;
- height: 100%;
- }
- </style>
- <script type="text/javascript">
- "use strict";
- //Place global variables here
- var wsUri = "ws://localhost:8080/EchoChamber/echo/1";
- var output;
- var websocket;
- var inter;
- function init ()
- {
- testWebSocket();
- }
- function testWebSocket()
- {
- websocket = new WebSocket(wsUri);
- //Two ways of registering events, addEventListener is prefered - can attach multiple handlers to single event
- // websocket.onopen = function(evt) { onOpen(evt) };
- // websocket.onmessage = function(evt) { onMessage(evt) };
- // websocket.onclose = function(evt) { onClose(evt) };
- // websocket.onerror = function(evt) { onError(evt) };
- websocket.addEventListener("open", onOpen, false);
- websocket.addEventListener("close", onClose, false);
- websocket.addEventListener("message", onMessage, false);
- websocket.addEventListener("error", onError, false);
- }
- //Websocket connection was successfully opened
- function onOpen(evt)
- {
- writeToConsole("CONNECTED");
- doSend("WebSocket ");
- }
- //Websocket connection was closed
- function onClose(evt)
- {
- writeToConsole("DISCONNECTED");
- }
- //Recieved reply from the server
- function onMessage(evt)
- {
- writeToConsole(evt.data);
- if (evt.data.toLowerCase() == "jasio"){
- websocket.close();
- clearInterval(inter);
- }
- }
- //Error occured
- function onError(evt)
- {
- writeToConsole(evt.data);
- }
- //Sent something to websocket server
- function doSend(message)
- {
- var count = 0;
- inter = setInterval(function(){
- websocket.send(message + String(count++));
- }, 1000/60);
- writeToConsole("SENT: " + message);
- }
- //Write the message to the console
- function writeToConsole(message)
- {
- console.log(message);
- }
- //run the function only after the whole page has been loaded
- window.addEventListener("load", init, false);
- </script>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement