Netty-socketio Instant Messenger

Source: Internet
Author: User
Tags button type

Jar packages and dependencies on 360 cloud disks: All Files > Learning > Jar packages > netty-socketio-1.7.10 and Dependencies

Original link: http://www.cnblogs.com/always-online/p/4131635.html

Fireworks _ Netty-socketio

First, Introduction

Netty-socketio is an open source Socket.io server-side implementation of Java, which is based on the Netty framework. The project address is: Https://github.com/mrniko/netty-socketio. Socket.io is a cross-browser use WebSocket for real-time real-time applications. This example uses MAVEN to build, depending on the following:

<dependency>    <groupId>com.corundumstudio.socketio</groupId>    <artifactId> Netty-socketio</artifactid>    <version>1.6.5</version></dependency>

The client is using Socket.io-client, which is: Https://github.com/Automattic/socket.io-client.

Second, the relevant code

Server-side:

Message entity

1 package com.test.socket; 2  3 public class Chatobject {4  5     private string userName; 6     private string message; 7  8 public     Ch Atobject () {9     }10 one public     chatobject (string userName, String message) {         n ();         this.username = username;14         this.message = message;15     }16     -Public String getusername () {         return username;19     }20 public     void Setusername (String userName) {         this.username = username;23     }24     Public String GetMessage () {         message;27}28, public     void Setmessage (String message) {         This.message = message;31     }32 33}

Listener

1 package com.test.socket; 2  3 import com.corundumstudio.socketio.AckRequest; 4 import com.corundumstudio.socketio.SocketIOClient; 5 import Com.corundumstudio.socketio.SocketIOServer; 6 Import Com.corundumstudio.socketio.listener.DataListener; 7  8 public class Charteventlistener implements datalistener<chatobject> {9     socketioserver server;11 12 Public     void Setserver (Socketioserver server) {         this.server = server;14}15-public     void OnData (socketioclient client, chatobject data,17             ackrequest acksender) throws Exception {//chatevent         is the name of the event , data is sent for content         this.server.getBroadcastOperations (). Sendevent ("chatevent", data);     }21 22}

Server-side startup code

1 package com.test.socket; 2  3 import com.corundumstudio.socketio.Configuration; 4 import com.corundumstudio.socketio.SocketIOServer; 5  6 public class Socketserver {7 public     static void Main (string[] args) throws Interruptedexception {8  9         Configuration config = new configuration ();         config.sethostname ("localhost");         Config.setport (9092); 12 13         socketioserver Server = new Socketioserver (config);         Charteventlistener listner = new Charteventlistener ();         listner.setserver (server);   Chatevent is the event name         Server.addeventlistener ("Chatevent", Chatobject.class, Listner);         //Start service         Server.start ();         Thread.Sleep (Integer.max_value);         server.stop ();     }26 27}28                                                 

Run the class and the server can start

The client message.html code is as follows:

 1 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 2 
  
  

The client needs to introduce jquery and socket.io.js files.

After the browser opens the HTML file, enters the user name, and sends the message content, the server sends the message to the client, the client receives the message returned from the server, or adds the message to the page and displays it. Operating effects such as:

Netty-socketio Instant Messenger

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.