most recently because of the need, to do a web game. Certain period of time to time to refresh the page. At present the idea to do site are generally js polling mode. Because it is a small innovative projects proposed by the same group of friends html5 mentioned WebSocket, first conducted a technical investigation. Currently WebSocket support of java's not that much. Online can be found to implement the framework on websocket very little. java EE7 adding WebSocket have not seen. Therefore, since the package comes from tomcat support. Requires at least three packs tomcat-coyote, tomcat-catalina, tomcat-annotations-api, because tomcat version from 7.027 began better support websocket, in tomcat7.027 previous versions, has been able to use websocket, but there will be various type all sorts of problems. For example websocket standing a few seconds after connection is disconnected and so forth. Therefore, a better choice is to use a 7.027 or later. This 3 jar wrapped in the appropriate folder under tomcat's lib has. After contact with maven since graduate school, and slowly learned maven powerful, here have lamented about. Because it is a small, agile team, version control is a must. In this regard jar package control or want to control via maven. Go directly to the maven central repository search a search. Really still there. After the panel discussion decided to use tomcat7.039 (looks like 40 has been out), this solved the version control and configuration issues jar package. pom on tomcat3 a jar packages are as follows:
1 <dependency>
2 <groupId>org.apache.tomcat</groupId>
3 <artifactId>tomcat-coyote</artifactId>
4 <version>7.0.39</version>
5 </dependency>
6 <dependency>
7 <groupId>org.apache.tomcat</groupId>
8 <artifactId>tomcat-catalina</artifactId>
9 <version>7.0.39</version>
10 </dependency>
11 <dependency>
12 <groupId>org.apache.tomcat</groupId>
13 <artifactId>tomcat-annotations-api</artifactId>
14 <version>7.0.39</version>
15 </dependency>
Next is to solve architectural problems. Now available online can be found to practice on websocket few can be found to the basic architecture and websocket are very simple example (tomcat comes there websocket example), how to use them can websocket mechanism. First, the basic framework ready to use hibernate + spring mvc combined websocket, but in the actual test spring mvc and websocket will be part of a conflict. Because to some time ago Spring Framework 4.0 release in only the JDK 8 WebSocket support and programming support. So at this stage need other ways to achieve spring mvc + websocket. The simplest solution is to write a utility class to manually get bean. After solving spring and websocket support need to be addressed websocket interact. websocket two most direct way is onTextMessage and onBinaryMessage, byte stream is transmitted and the character stream. Best way is to design a set of its own transmission protocol. Through byte stream. Front and back, respectively, resolution protocol for interoperability. Second, can the onTextMessage character stream that is to make a fuss. It can be a good introduction json support.
configuration websocket steps:
1 to implement a class that inherits ContextLoaderListener, and is configured in web.xml
1 import javax.servlet.ServletContext;
2 import javax.servlet.ServletContextEvent;
3
4 import org.springframework.context.ApplicationContext;
5 import org.springframework.web.context.ContextLoaderListener;
6 import org.springframework.web.context.support.WebApplicationContextUtils;
7
8 public class SpringLoaderListener extends ContextLoaderListener{
9
10 @Override
11 public void contextInitialized(ServletContextEvent event) {
12 super.contextInitialized(event);
13 ServletContext context=event.getServletContext();
14 ApplicationContext ctx=WebApplicationContextUtils.getRequiredWebApplicationContext(context);
15 SpringContextutil.setContext(ctx);
16
17 }
18
19 }
web.xml
1 <listener>
2 <listener-class>
3 XXXXXX.utils.SpringLoaderListener
4 </listener-class>
5 </listener>
get spring bean tools:
1 import org.springframework.beans.BeansException;
2 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3 import org.springframework.context.ApplicationContext;
4
5 public class SpringContextUtil {
6 private static ApplicationContext context;
7
8 public static ApplicationContext getContext() {
9 return context;
10 }
11
12 public static void setContext(ApplicationContext context) {
13 SpringContextutil.context = context;
14 }
15
16 public static Object getBean(String name)throws BeansException{
17 return context.getBean(name);
18 }
19
20 @SuppressWarnings("unchecked")
21 public static Object getBean(String name, Class requiredType) throws BeansException {
22 return context.getBean(name, requiredType);
23 }
24
25 public static boolean containsBean(String name) {
26 return context.containsBean(name);
27 }
28
29 public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
30 return context.isSingleton(name);
31 }
32
33 @SuppressWarnings("unchecked")
34 public static Class getType(String name) throws NoSuchBeanDefinitionException {
35 return context.getType(name);
36 }
37
38 public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
39 return context.getAliases(name);
40 }
41
42
43 }
jar package required for the introduction of json:
1 <dependency>
2 <groupId>com.google.code.gson</groupId>
3 <artifactId>gson</artifactId>
4 <version>2.2.3</version>
5 </dependency>
6 <dependency>
7 <groupId>net.sf.json-lib</groupId>
8 <artifactId>json-lib</artifactId>
9 <version>2.4</version>
10 </dependency>
11 <dependency>
12 <groupId>net.sf.ezmorph</groupId>
13 <artifactId>ezmorph</artifactId>
14 <version>1.0.6</version>
15 </dependency>
background need to add two files an inheritance WebSocketServlet:
1 import javax.servlet.annotation.WebServlet;
2 import javax.servlet.http.HttpServletRequest;
3
4 import org.apache.catalina.websocket.StreamInbound;
5 import org.apache.catalina.websocket.WebSocketServlet;
6
9 @WebServlet("/room")
10 public class RoomSocketServlet extends WebSocketServlet {
11
12 private static final long serialVersionUID = -5853470534275847275L;
13
14 @Override
15 protected StreamInbound createWebSocketInbound(String arg0,HttpServletRequest request) {
16 return new RoomMessageInbound();
17 }
18
19 }
an inheritance MessageInbound:
1 public class RoomMessageInbound extends MessageInbound{
2 private static RoomModel room ;
3 private UserModel user;
4 private CommandDispatcherUtils commandDispatcher;
5
6 public RoomMessageInbound() {
7 if (commandDispatcher == null) {
8 commandDispatcher = (CommandDispatcherUtils) SpringContextutil.getBean("commandDispatcher");
9 room = RoomListModel.getInstance().getRoom(0);
10 }
11 }
12
13
14 @Override
15 protected void onOpen(WsOutbound outbound)
16 room.addUser(outbound.hashCode());
17 super.onOpen(outbound);
18 }
19
20 @Override
21 protected void onClose(int status) {
22 room.remove(getWsOutbound().hashCode());
23 super.onClose(status);
24 }
25
26 @Override
27 protected void onBinaryMessage(ByteBuffer buffer) throws IOException {
28
29 }
30
31 @Override
32 protected void onTextMessage(CharBuffer buffer) throws IOException {
33 String msg = buffer.toString();
34 JSONObject report = JSONObject.fromObject(msg);
35 TemplateCommand command = commandDispatcher.getCommandByKey(report.getString("command"));
36 command.execute(msg,user,room);
37 }
38
39 }
through JSONObject report = JSONObject.fromObject (msg) you can convert a string to json object. It is equal to the object through the websocket achieve real-time information transfer.
the front page when the page loads only need to add the js
1 $(function() {
2 roomsocket = new WebSocket('ws://127.0.0.1:8080/XXXX/room);
3 }
in the front to the back when sending data JSON.stringify (data) will be able to json technology.
The code to do a lotdeletion. So the code is not necessarily able to use copy. Only provides a solution. Moreover, the new version soon java or Spring Framework 4.0 will be able to easily support websocket implementation. Mainly or give yourself this time to do some research websocket summary. With the birth of various new technologies, real-time web technology has become increasingly sophisticated. websocket is an important feature of html5 is worth checking out, look.
Could you please send me a project for this tutorial? My email is m.a.ennami@gmail.com
回复删除Thank you in advance