The WebSocket protocol is a new network protocol based on TCP. It implements full-duplex communication between the browser and the server - allowing the server to proactively send information to the client.
In the process of implementing websocket connection, a websocket connection request needs to be sent through the browser, and then the server sends a response. This process is usually called "handshake". In the WebSocket API, the browser and the server only need to perform a handshake action, and then a fast channel is formed between the browser and the server. Data can be transmitted directly between the two.
In the previous message push mechanism, Ajax polling was used, and the browser automatically issued a request at a specific time interval. The server's messages are actively pulled back. This method consumes a lot of resources because it is essentially an HTTP request and is very clumsy. WebSocket completes a handshake between the browser and the server. After the connection is established, the server can actively transmit data to the client, and the client can also send data to the server at any time.
When establishing communication, the client actively initiates a connection request and the server passively listens.
The interaction mode is no longer the "request-response" mode, and the communication protocol is completely designed by the developer.
The communication data is based on "frame", which can transmit text data or binary data directly, with high efficiency. Of course, developers also have to consider technical details such as packaging, unpacking, and numbering.
Every interaction is: the client actively initiates a request (request), and the server passively responds (response).
#The communication data is based on text format. Binary data (such as pictures, etc.) must be converted into text using base64 and other means before it can be transmitted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
var websocket = var host = document.location.host; var username = //Determine whether the current browser supports WebSocket if } }
|
1. Core class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
System.out.println( "已连接" );
clients.remove(username);
<p><code> subOnlineCount();
JSONObject jsonTo = JSONObject.fromObject(message);
<p><code> String mes = (String) jsonTo.get( "message" );
<p><code>
<p><code> if (!jsonTo.get( "To" ).equals( "All" )){
<p><code> sendMessageTo(mes, jsonTo.get( "To" ).toString());
<p><code> } else {
<p><code> sendMessageAll( "给所有人" );
<p><code> }
error.printStackTrace();
// session.getBasicRemote().sendText(message);
<p><code> //session.getAsyncRemote().sendText(message);
<p><code> for (WebSocket item : clients.values()) {
<p><code> if (item.username.equals(To) )
<p><code> item.session.getAsyncRemote().sendText(message);
<p><code> }
for (WebSocket item : clients.values()) {
<p><code> item.session.getAsyncRemote().sendText(message);
<p><code> }
return onlineCount;
WebSocket.onlineCount ;
WebSocket.onlineCount--;
return clients;
|
2.在自己代码中的调用:
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
相关视频:
The above is the detailed content of The principles and basic knowledge of WebSocket to implement Java background message push. For more information, please follow other related articles on the PHP Chinese website!