Learn jsSocket to send real-time messages from the background to the foreground

When we were working on a customer management system before, our customers often asked us to implement communication. At that time, the solution we discussed was that the client sends requests at intervals to access the application of the server, and if the state changes, a pop-up message is displayed to the user.

Today, I found a post on the Hidden Heart Technology Forum. The author is a master, using js+flex+java to develop and implement communication sockets.

Instructions for using the JsSocket component simulated by Js+Flex+Java

Note: Pictures and attachments need to be logged in to view and download. If you have not registered, please register an account first and then log in.

JsSocket

      Demonstration address 1    Demonstration address 2 Hidden chat  <----     At everyone's request, the demo project "Hidden Chat" has been open sourced and shared, code sharing address: TaoCode  svn : http://code.taobao.org/svn/yisin- webchat/ If you don't have an account, please register yourself. Open source Chinese OSC  svn:     

      

  1. svn://git.oschina.net/yisin/webchat
copy code



JsSocket  is a set of web -based socket communication components developed using js + flex + java , the best choice for developing server message push

It mainly solves the problem that everyone needs to use the foreground and background to communicate constantly when developing web projects. In addition to HTML5 WebSocket , the methods that everyone used before are generally: 1. Intermittent page refresh; 2. Use ajax to send requests intermittently; 3 , Using the comet long connection simulation implementation of the DWR framework , the shortcomings are well known to everyone

The latest HTML5 WebSocket , although it solves this problem very well, but IE9 and below versions are not compatible with HTML5 , that is, WebSocket cannot be used to achieve the purpose, this component is compatible with almost all browsers due to the use of  js + flex . The method of use is as follows

1. Backstage:

1. Import the jar package: JsSocket.jar

2. Write a class SocketClient.java that inherits the  com.yinsin.method.CallBack.java class. This class is mainly used to process client sending and receiving requests. The sample code is as follows:

You can do whatever you want with these 3 methods

3. You can enable the JsSocket service when the web service starts :

a. Configure the web service startup listener in web.xml

<listener>

<listener-class>com.yinsin.frame.init.InitCustomData</listener-class>

</listener>

b. Start the JsSocket service in the contextInitialized method of InitCustomData

/**  Receive two parameters, the first parameter is not a port, and the second parameter is a subclass of CallBack.java  */

TcpServerSocket.getInstance().startServer(10000,new SocketClient());

4. Finally start the web service. If the console outputs " JsSocket # TCP Socket Server Started! ", it means that the background service of JsSocket is completed.

 

Second, the front end

1. Import the required flash resource files into the project , the directory structure is: script/JsSocket/JsSocket.swf and script/JsSocket/playerProductInstall.swf , put this directory under WebContent or WebRoot .

2. The page that needs to use JsSocket needs to load the code required by the component:

<span><%=com.yinsin.util.CodeLoader.loaderCode(request.getContextPath())
%></span>

3. js calls the initialization function, initializes the component, and connects to the server :

4. Send a message to the server:

yinsin.sendData('Hello World!');



5、服务器向前端推送消息(补充):
      sendDataToClient(Socket, String);
    在SocketClient类中,调用此方法即可

 

本文章是转载文章,原文在隐心技术论坛http://yisin.88448.com/viewthread.php?tid=39949

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326346870&siteId=291194637