Thursday, March 15, 2018

Javascript WebSocket Client Example

In javascript WebSocket API below are the methods that you may need to implement when communicating with a server.
  websocket.onopen - This method gets executed when a connection between the client and the server is created.
  websocket.onclose - This method gets executed when a connection is closed.
  websocket.onmessage This method gets executed when a message is received from server.
  websocket.onerror - This will be called if an unhandled error is thrown in above methods.

Below HTML page contains a sample code.

<!DOCTYPE HTML>
<html>
   <head>
      <script type="text/javascript">
         var ws;
         function loadWebsocket() {
            if (window.WebSocket) {
               //The browser supports WebSockets");
               // Open a web socket
               ws = new WebSocket("ws://localhost:8080/WebsocketServer1/chat");
               ws.onopen = function() {
                  console.log("Connection is opened...");
               };

               ws.onmessage = function (evt) { 
                  var message = evt.data;
                  console.log("Message is received...:" + message);
               };

               ws.onclose = function() { 
                  console.log("Connection is closed..."); 
               };

ws.onerror = function() {
console.log("Error occured...");
};
               
               window.onbeforeunload = function(event) {
                  socket.close();
               };
            } else {
               // The browser doesn't support WebSocket
               console.log("Your browser doesn't support WebSockets!");
            }
         }
 
         function sendMessage() {
          ws.send("Hello server!");
        }
        
         loadWebsocket();
      </script>

   </head>
   <body>
   
      <div id="sse">
         <a href="javascript:sendMessage()">Send message</a>
      </div>
      
   </body>
</html>
  
  

29 comments:

  1. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
    full stack developer training in chennai

    ReplyDelete
  2. Vulpith being the best freelance jobs website online platform, We offers both information security as well as escrow protection and we observed major challenges in working virtually or remotely.
    visit:
    https://vulpith.com/

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I love the blog. Great post. It is very true, people must learn how to learn before they can learn. lol i know it sounds funny but its very true. . .
    Click here:
    python training in chennai


    Click here:
    python training in tambaram

    ReplyDelete
  5. Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Blueprism training in Chennai

    Blueprism training in Bangalore

    Blueprism training in Pune

    Blueprism online training

    ReplyDelete
  6. You got an extremely helpful website I actually have been here reading for regarding an hour. I’m an initiate and your success is incredibly a lot of a concept on behalf of me.
    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    ReplyDelete
  7. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    python training in pune | python training institute in chennai | python training in Bangalore

    ReplyDelete
  8. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. I am really thankful for posting such useful information. It really made me understand lot of important concepts in the topic. Keep up the good work!
    Oracle Training in Chennai | Oracle Course in Chennai

    ReplyDelete
  11. Appreciating the persistence you put into your blog and detailed information you provide.
    Data science training in chennai |Data science course in chennai

    ReplyDelete
  12. Here is the site(bcomexamresult.in) where you get all Bcom Exam Results. This site helps to clear your all query.
    BA 3rd year Result 2019-20
    Kerala University B.Com 6th Sem Result 2020

    ReplyDelete
  13. I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
    uniraj ba 3rd year result
    Shekhawati University ba final result

    ReplyDelete
  14. very useful article.Technical contents are so good.Keep going
    Escort Service in Noida

    ReplyDelete
  15. Very useful article.Technical contents are so good.Keep going
    Escort Services in Gurgaon

    ReplyDelete
  16. hey this is amazing article post. thanks for sharing a valuable post. You can visit here for search engine marketing agency in Delhi and why shoyld you hire an seo company in delhi.

    ReplyDelete
  17. Impressive! I finally found a great post here. Good article on Data Science. It is really a great experience to read your post. Thank you for sharing your innovative ideas with Our Vision.
    online flower shop
    orchid wedding bouquet
    sunflower wedding bouquet
    Wedding Flower Delivery
    wedding flowers near me
    orchid wedding bouquet
    cheap wedding flowers
    wedding bouquet flowers online
    wedding flowers order online
    wedding bouquets near me
    flower delivery Dublin
    flower delivery Dublin Ireland
    flower delivery Dublin
    flower delivery Dublin
    birthday flowers delivery Dublin
    same day birthday flower delivery
    flower shop Dublin
    birthday bouquet delivery Ireland
    send birthday flowers online Ireland
    same day birthday flower delivery Ireland
    next day birthday flower delivery Ireland
    birthday flowers delivery Ireland
    Send Birthday Flowers Ireland
    order birthday flowers online Ireland
    Birthday Flowers Ireland
    birthday flowers delivery near me

    ReplyDelete
  18. සුපිරියි බ්ලොග් එක..තව අලුතෙන් පෝස්ට්ස් දාන්න

    ReplyDelete