WebSocket Simple Example __web

Source: Internet
Author: User

what is websocket.

WebSocket is a real-time technology for resolving server - side and client communication. Ajax can also communicate, but he can't do it in real time. We want real-time effects before websocket, all through polling, which is obviously a waste of resources.

WebSocket's strength is that, compared to Ajax needs to initiate requests from the client, WebSocket's server and client can push messages to each other in real time.


how to use WebSocket.

First you have to have a browser that supports it. (IE is not supported)

After the socket is created, the client can respond to the socket by onopen,onmessage,onclose,onerror these four times.


Give me a chestnut:

Environment: TOMCAT7 or above. Jdk7 or above

1.websockettest.java

Package instance;

Import java.io.IOException;
Import Javax.websocket.OnClose;
Import Javax.websocket.OnMessage;
Import Javax.websocket.OnOpen;
Import javax.websocket.Session;
 
Import Javax.websocket.server.ServerEndpoint; <span style= "color: #ff6666;" > @ServerEndpoint ("/websocket") </span> public class Websockettest {@OnMessage public void OnMessage (String message, session sessions) throws IOException, interruptedexception {//Print the client message for testing p
   
    Urposes System.out.println ("Received:" + message);
   
    Send the "the" to "the" Client Session.getbasicremote (). SendText ("This is the The");
    Send 3 messages to the client every 5 seconds int sentmessages = 0;
      while (Sentmessages < 3) {Thread.Sleep (5000);
        Session.getbasicremote (). SendText ("This is a intermediate server message.")
      Count: "+ sentmessages);
    sentmessages++; }//Send a final mesSage to the client Session.getbasicremote (). SendText ("This is the last Server message");
  @OnOpen public void OnOpen () {System.out.println ("Client connected");
  @OnClose public void OnClose () {System.out.println ("Connection closed");
 }
}

2.page.html

<! DOCTYPE html>  

Here to note:

var webSocket =
      new WebSocket (' Ws://localhost:8080/webscoket/websocket ');
Where: One is the project name, one is the server name of the annotation


Here you start the server, run page.html

As shown in the picture, it means success.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.