Netty-socketio-based web PUSH Service, netty-socketioweb
For Real-Time Message pushing, the PC-side push technology can be achieved by using a socket to establish a persistent connection. In traditional web Services, the client sends requests and the server responds. However, the intuitive requirement is to allow the server to actively push messages to the client if no client initiates a request within a specific time period.
Which of the following technologies can push web messages:
-
Continuously round robin (also known as "pull", polling) is a means of obtaining real-time messages:Ajax queries the server to see if there is any change after a period of time (usually using the setTimeout Function of JavaScript) to perform incremental updates. However, the interval between queries becomes a problem, because the performance and real-time result in a serious inverse relationship. If the interval is too short, continuous requests will crash the server. If the interval is too long, the more time the new data on the server needs to arrive at the client.
-
Advantage: Simple Server logic;
-
Disadvantages: Most of the requests may be invalid requests, which puts a lot of pressure on the server when a large number of users frequently round-robin;
-
Application: the number of concurrent users is small, and the real-time messaging is not high, which is rarely used;
-
Long-polling technology): The client sends an Ajax request to the server. After receiving the request, the server holds the connection and returns the response information and closes the connection until a new message or timeout (SET) exists, after processing the response information, the client sends a new request to the server.
-
Streaming Based on Iframe and htmlfile: The iframe stream mode is to insert a hidden iframe in the page, and use its src attribute to create a long link between the server and the client. The server transmits data to the iframe (usually HTML, is used to update pages in real time.
-
Plug-in socket: For example, Flash XMLSocket, Java Applet set interface, and Activex packed socket.
-
WebSocket: It is a network technology provided by HTML5 for full duplex communication between browsers and servers.
-
Advantages: it can save server resources and bandwidth and achieve real-time communication;
-
Disadvantages: it is not yet popular, and browser support is poor;
In summary, considering browser compatibility and performance issues, using long-polling is a good method.
Netty-socketio is a java implementation of an open-source Socket. io server. It is based on the Netty framework. Project address: https://github.com/mrniko/netty-socketio
The following is a web chat system push example from netty-socketio.
Server push server:
Message entity:
Package com. nettysocketio. test; public class ChatObject {private String userName; private String message; public ChatObject () {} public ChatObject (String userName, String message) {super (); this. userName = userName; this. message = message;} public String getUserName () {return userName;} public void setUserName (String userName) {this. userName = userName;} public String getMessage () {return message;} public void setMessage (String message) {this. message = message ;}}View Code
Listener event:
Package com. nettysocketio. test; import com. corundumstudio. socketio. ackRequest; import com. corundumstudio. socketio. socketIOClient; import com. corundumstudio. socketio. socketIOServer; import com. corundumstudio. socketio. listener. dataListener; public class CharteventListener implements DataListener <ChatObject> {SocketIOServer server; public void setServer (SocketIOServer server) {this. server = server;} public void onData (SocketIOClient client, ChatObject data, AckRequest ackSender) throws Exception {// chatevent is the name of the event, and data is the content sent this. server. getBroadcastOperations (). sendEvent ("chatevent", data );}}
Push Service:
Package com. nettysocketio. test; import com. corundumstudio. socketio. configuration; import com. corundumstudio. socketio. socketIOServer; public class App {public static void main (String [] args) throws InterruptedException {Configuration config = new Configuration (); config. setHostname ("localhost"); config. setPort (9092); SocketIOServer server = new SocketIOServer (config); CharteventListener listner = new CharteventListener (); listner. setServer (server); // chatevent is the event name server. addEventListener ("chatevent", ChatObject. class, listner); // start the service server. start (); Thread. sleep (Integer. MAX_VALUE); server. stop ();}}
Browser Client:
The client uses socket. io-client and the Project address is: https://github.com/Automattic/socket.io-client
Client chat code:
<! DOCTYPE html>
Run push server first, and then open chat html to view the connection information and chat information pushed by the server.
Refer:
Https://github.com/mrniko/netty-socketio
Https://github.com/mrniko/netty-socketio-demo