In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you how to implement the java WebSocket server. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. What is WebSocket?
WebSocket protocol is a new network protocol based on TCP. It implements full-duplex (full-duplex) communication between the browser and the server-allowing the server to actively send information to the client.
two。 Realization principle
In the process of implementing a websocket connection, you need to issue a websocket connection request through a browser, and then the server sends a response, a process often referred to as a "handshake". In WebSocket API, the browser and the server only need to do a handshake, and then a fast channel is formed between the browser and the server. Data can be transmitted directly between the two.
3. Advantages
In the previous message push mechanism, Ajax polling (polling) was used, and the browser automatically issued the request at a specific time interval to actively pull the message back from the server, which is very resource-consuming because it is essentially a http request and appears to be very clumsy. While WebSocket completes a handshake between the browser and the server. After establishing the connection, the server can actively transmit data to the client, and the client can also send data to the server at any time.
The specific code is as follows:
Package org.fh.plugins.ChatServer;import java.net.InetSocketAddress;import java.net.UnknownHostException;import org.java_websocket.WebSocket;import org.java_websocket.framing.Framedata;import org.java_websocket.handshake.ClientHandshake;import org.java_websocket.server.WebSocketServer;/** * description: websocket server * author: FH Admin * from:fhadmin.cn * / public class ChatServer extends WebSocketServer {public BulletChatServer (int port) throws UnknownHostException {super (new InetSocketAddress (port)) } public BulletChatServer (InetSocketAddress address) {super (address); / * * event triggered when the client sends a message to the server * / @ Override public void onMessage (WebSocket conn, String message) {message = message.toString () If (null! = message & & message.startsWith ("[video313596790]")) {this.userjoin (message.replaceFirst ("\\ [video313596790\\]", "), conn);} else {BulletChatServerPlool.sendMessage (message.toString ()) / / send messages to all online users} public void onFragment (WebSocket conn, Framedata fragment) {* user join processing * @ param user public void userjoin (String user, WebSocket conn) {BulletChatServerPlool.addUser (user,conn) / / add the current connection object to the connection pool * user offline processing public void userLeave (WebSocket conn) {BulletChatServerPlool.removeUser (conn) / / remove connections from the connection pool * trigger connection event public void onOpen (WebSocket conn, ClientHandshake handshake) {} * trigger shutdown event public void onClose (WebSocket conn, int code, String reason, boolean remote) {userLeave (conn) * trigger exception event public void onError (WebSocket conn, Exception ex) {if (conn! = null) {} public void onStart () {}}
-Custom form
twenty-eight。 Define template: drag and drop the left form element to the right area, edit the form element, and save the form template
twenty-nine。 Form template: edit and maintain form template, copy form template, modify template type, preview form template
thirty。 My form: select form template, edit form rules, whether to upload pictures, attachments, turn on rich text, attach process switch, etc.
thirty-one。 Form data: from my form, you can add, delete, modify and check form data, modify form rules
thirty-two。 Attachment record: record form data and process instance ID associated record, which can be deleted
Thank you for reading! This is the end of the article on "how to implement the java WebSocket server". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.