Commit 9af6d59d2e41245dd80094af4cc10cc6f67a725d

Authored by 张宗朋
1 parent 63e8b99d

socket

src/main/java/com/cnlive/mz/live/action/VideoAction.java
@@ -107,7 +107,7 @@ public class VideoAction { @@ -107,7 +107,7 @@ public class VideoAction {
107 tLive.setDescription(description); 107 tLive.setDescription(description);
108 tLive.setStart_time(new Date()); 108 tLive.setStart_time(new Date());
109 tLive.setUser_uuid(params.getString("name")); 109 tLive.setUser_uuid(params.getString("name"));
110 - tLive.setRelation_id(Integer.parseInt(relation_id)); 110 + tLive.setRelation_id(relation_id);
111 tLive.setCustom_args(custom_args); 111 tLive.setCustom_args(custom_args);
112 uuid = tLive.getUuid(); 112 uuid = tLive.getUuid();
113 if (StringUtils.isEmpty(direct)) { 113 if (StringUtils.isEmpty(direct)) {
src/main/java/com/cnlive/mz/live/servlet/InitServlet.java
1 package com.cnlive.mz.live.servlet; 1 package com.cnlive.mz.live.servlet;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
  4 +
  5 +import javax.servlet.Servlet;
4 import javax.servlet.ServletConfig; 6 import javax.servlet.ServletConfig;
5 import javax.servlet.ServletException; 7 import javax.servlet.ServletException;
6 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.annotation.WebServlet;
@@ -8,7 +10,7 @@ import javax.servlet.http.HttpServlet; @@ -8,7 +10,7 @@ import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpServletResponse;
10 12
11 -import com.cnlive.mz.live.action.SocketServer; 13 +import com.cnlive.socket.SocketServer;
12 14
13 /** 15 /**
14 * Servlet implementation class InitServlet 16 * Servlet implementation class InitServlet
@@ -29,9 +31,9 @@ public class InitServlet extends HttpServlet { @@ -29,9 +31,9 @@ public class InitServlet extends HttpServlet {
29 * @see Servlet#init(ServletConfig) 31 * @see Servlet#init(ServletConfig)
30 */ 32 */
31 public void init(ServletConfig config) throws ServletException { 33 public void init(ServletConfig config) throws ServletException {
32 - System.out.println(".......init"); 34 + System.out.println("kaishi.......init");
33 int port = 65432; 35 int port = 65432;
34 - SocketServer server = new SocketServer(port); 36 + SocketServer server = new SocketServer(port);
35 server.start(); 37 server.start();
36 } 38 }
37 39
src/main/java/com/cnlive/mz/live/action/SocketServer.java renamed to src/main/java/com/cnlive/socket/SocketServer.java
1 -package com.cnlive.mz.live.action; 1 +package com.cnlive.socket;
2 2
  3 +import java.io.BufferedReader;
3 import java.io.IOException; 4 import java.io.IOException;
4 import java.io.InputStream; 5 import java.io.InputStream;
  6 +import java.io.InputStreamReader;
5 import java.io.ObjectInputStream; 7 import java.io.ObjectInputStream;
6 import java.io.ObjectOutputStream; 8 import java.io.ObjectOutputStream;
  9 +import java.io.OutputStream;
7 import java.net.ServerSocket; 10 import java.net.ServerSocket;
8 import java.net.Socket; 11 import java.net.Socket;
9 -import java.util.HashMap;  
10 -import java.util.Map;  
11 import java.util.concurrent.ConcurrentHashMap; 12 import java.util.concurrent.ConcurrentHashMap;
12 13
13 -public class SocketServer{ 14 +public class SocketServer {
14 15
15 /** 16 /**
16 * 要处理客户端发来的对象,并返回一个对象,可实现该接口。 17 * 要处理客户端发来的对象,并返回一个对象,可实现该接口。
@@ -19,12 +20,12 @@ public class SocketServer{ @@ -19,12 +20,12 @@ public class SocketServer{
19 Object doAction(Object rev); 20 Object doAction(Object rev);
20 } 21 }
21 22
22 - public static final class DefaultObjectAction implements ObjectAction{ 23 + /*public static final class DefaultObjectAction implements ObjectAction{
23 public Object doAction(Object rev) { 24 public Object doAction(Object rev) {
24 System.out.println("处理并返回:"+rev); 25 System.out.println("处理并返回:"+rev);
25 return rev; 26 return rev;
26 } 27 }
27 - } 28 + } */
28 29
29 public static void main(String[] args) { 30 public static void main(String[] args) {
30 int port = 65432; 31 int port = 65432;
@@ -34,7 +35,7 @@ public class SocketServer{ @@ -34,7 +35,7 @@ public class SocketServer{
34 35
35 private int port; 36 private int port;
36 private volatile boolean running=false; 37 private volatile boolean running=false;
37 - private long receiveTimeDelay=3000; 38 + private long receiveTimeDelay=7000;
38 private ConcurrentHashMap<Class, ObjectAction> actionMapping = new ConcurrentHashMap<Class,ObjectAction>(); 39 private ConcurrentHashMap<Class, ObjectAction> actionMapping = new ConcurrentHashMap<Class,ObjectAction>();
39 private Thread connWatchDog; 40 private Thread connWatchDog;
40 41
@@ -64,7 +65,7 @@ public class SocketServer{ @@ -64,7 +65,7 @@ public class SocketServer{
64 try { 65 try {
65 ServerSocket ss = new ServerSocket(port,5); 66 ServerSocket ss = new ServerSocket(port,5);
66 while(running){ 67 while(running){
67 - System.out.println("准备客服端请求。。。"); 68 + System.out.println("等待客服端。。。");
68 Socket s = ss.accept(); 69 Socket s = ss.accept();
69 new Thread(new SocketAction(s)).start(); 70 new Thread(new SocketAction(s)).start();
70 } 71 }
@@ -72,6 +73,7 @@ public class SocketServer{ @@ -72,6 +73,7 @@ public class SocketServer{
72 e.printStackTrace(); 73 e.printStackTrace();
73 SocketServer.this.stop(); 74 SocketServer.this.stop();
74 } 75 }
  76 +
75 } 77 }
76 } 78 }
77 79
@@ -83,28 +85,42 @@ public class SocketServer{ @@ -83,28 +85,42 @@ public class SocketServer{
83 this.s = s; 85 this.s = s;
84 } 86 }
85 public void run() { 87 public void run() {
86 - Map <String,Object> map = new HashMap<String,Object>();  
87 - /* try {  
88 - Thread.sleep(10);//设置多少时间读一次客户端请求。。  
89 - } catch (InterruptedException e1) {  
90 - e1.printStackTrace();  
91 - } */  
92 while(running && run){ 88 while(running && run){
93 - if(System.currentTimeMillis()-lastReceiveTime>receiveTimeDelay){  
94 - System.out.println(System.currentTimeMillis()-lastReceiveTime);  
95 - overThis();  
96 - }else{ 89 + try {
  90 + Thread.sleep(3000);
  91 + } catch (InterruptedException e1) {
  92 + // TODO Auto-generated catch block
  93 + e1.printStackTrace();
  94 + }
97 try { 95 try {
98 InputStream in = s.getInputStream(); 96 InputStream in = s.getInputStream();
99 - if(in.available()>0){  
100 - ObjectInputStream ois = new ObjectInputStream(in);  
101 - Object obj = ois.readObject(); 97 + OutputStream out = s.getOutputStream();
  98 + byte[] recData = null;
  99 + while(true) {
  100 + recData = new byte[1024];
  101 + int r = in.read(recData);
  102 + if(r>-1) {
  103 + String data = new String(recData);
  104 + /* if(data.trim().equals("over")) {
  105 + s.close();
  106 + break;
  107 + }*/
  108 + System.out.println("读取到客户端发送的来数据:"+data);
  109 + }
  110 + }
  111 + /* if(in.available()>0){
  112 + BufferedReader br = new BufferedReader(new InputStreamReader(in));
  113 + //out = new PrintWriter(socket.getOutputStream(), true);
  114 + String line = br.readLine();
  115 + System.out.println("you input is : " + line);
  116 + //out.println("you input is :" + line);
  117 + //out.close();
  118 + // in.close();
  119 + }*/
  120 + /*ObjectInputStream ois = new ObjectInputStream(in);
  121 + Object obj = ois.readObject();
102 lastReceiveTime = System.currentTimeMillis(); 122 lastReceiveTime = System.currentTimeMillis();
103 - String str = (String) obj;  
104 - String[] aa =str.split("#");  
105 - map.put("spId", aa[0]);  
106 - map.put("activityId", aa[1]);  
107 - System.out.println("接收:\t"+obj); 123 + System.out.println("接收:\t"+obj);
108 ObjectAction oa = actionMapping.get(obj.getClass()); 124 ObjectAction oa = actionMapping.get(obj.getClass());
109 oa = oa==null?new DefaultObjectAction():oa; 125 oa = oa==null?new DefaultObjectAction():oa;
110 Object out = oa.doAction(obj); 126 Object out = oa.doAction(obj);
@@ -112,10 +128,7 @@ public class SocketServer{ @@ -112,10 +128,7 @@ public class SocketServer{
112 ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); 128 ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
113 oos.writeObject(out); 129 oos.writeObject(out);
114 oos.flush(); 130 oos.flush();
115 - }  
116 - }else{  
117 - Thread.sleep(10);  
118 - } 131 + } */
119 } catch (Exception e) { 132 } catch (Exception e) {
120 System.out.println(System.currentTimeMillis()-lastReceiveTime); 133 System.out.println(System.currentTimeMillis()-lastReceiveTime);
121 e.printStackTrace(); 134 e.printStackTrace();
@@ -123,7 +136,6 @@ public class SocketServer{ @@ -123,7 +136,6 @@ public class SocketServer{
123 } 136 }
124 } 137 }
125 } 138 }
126 - }  
127 139
128 private void overThis() { 140 private void overThis() {
129 if(run)run=false; 141 if(run)run=false;
@@ -137,6 +149,5 @@ public class SocketServer{ @@ -137,6 +149,5 @@ public class SocketServer{
137 System.out.println("关闭:"+s.getRemoteSocketAddress()); 149 System.out.println("关闭:"+s.getRemoteSocketAddress());
138 } 150 }
139 151
140 - }  
141 - 152 + }
142 } 153 }
target/m2e-wtp/web-resources/META-INF/MANIFEST.MF
1 -Manifest-Version: 1.0  
2 -Build-Jdk: 1.7.0_13  
3 -Built-By: Administrator  
4 -Created-By: Maven Integration for Eclipse  
5 - 1 +Manifest-Version: 1.0
  2 +Build-Jdk: 1.7.0_13
  3 +Built-By: Administrator
  4 +Created-By: Maven Integration for Eclipse
  5 +
target/m2e-wtp/web-resources/META-INF/maven/com.cnlive.mz.live.web/cnlive_live_web/pom.properties
1 -#Generated by Maven Integration for Eclipse  
2 -#Thu Aug 11 09:12:27 CST 2016  
3 -version=0.0.1-SNAPSHOT  
4 -groupId=com.cnlive.mz.live.web  
5 -m2e.projectName=cnlive_live_web  
6 -m2e.projectLocation=C\:\\Users\\Administrator\\git\\cnlive_live_web  
7 -artifactId=cnlive_live_web 1 +#Generated by Maven Integration for Eclipse
  2 +#Fri Aug 12 09:21:48 CST 2016
  3 +version=0.0.1-SNAPSHOT
  4 +groupId=com.cnlive.mz.live.web
  5 +m2e.projectName=cnlive_live_web
  6 +m2e.projectLocation=C\:\\Users\\Administrator\\git\\cnlive_live_web
  7 +artifactId=cnlive_live_web