Skip to content

Commit 1eb97bf

Browse files
committed
纠结了一晚上的编码问题
1 parent 909b14e commit 1eb97bf

File tree

18 files changed

+445
-94
lines changed

18 files changed

+445
-94
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.qcloud.weapp.demo;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
import org.json.JSONException;
8+
import org.json.JSONObject;
9+
10+
import com.qcloud.weapp.Configuration;
11+
import com.qcloud.weapp.ConfigurationManager;
12+
import com.qcloud.weapp.ConfigurationException;
13+
14+
public class QCloud {
15+
16+
public static void setupSDK() {
17+
try {
18+
JSONObject configs = new JSONObject(getConfigJson());
19+
Configuration configuration = new Configuration();
20+
configuration.setServerHost(configs.getString("serverHost"));
21+
configuration.setAuthServerUrl(configs.getString("authServerUrl"));
22+
configuration.setTunnelServerUrl(configs.getString("tunnelServerUrl"));
23+
configuration.setTunnelSignatureKey(configs.getString("tunnelSignatureKey"));
24+
if (configs.has("networkProxy")) {
25+
configuration.setNetworkProxy(configs.getString("networkProxy"));
26+
}
27+
ConfigurationManager.setup(configuration);
28+
System.out.println("QCloud SDK 已成功配置!");
29+
} catch (JSONException e) {
30+
e.printStackTrace();
31+
} catch (ConfigurationException e) {
32+
e.printStackTrace();
33+
}
34+
}
35+
36+
private static String getConfigJson() {
37+
String configFilePath = getConfigFilePath();
38+
System.out.println("QCloud SDK 配置文件路径:" + configFilePath);
39+
40+
String configJsonText = null;
41+
42+
try {
43+
BufferedReader br = new BufferedReader(new FileReader(configFilePath));
44+
StringBuilder sb = new StringBuilder();
45+
String line;
46+
while((line = br.readLine()) != null) {
47+
sb.append(line);
48+
sb.append(System.lineSeparator());
49+
}
50+
configJsonText = sb.toString();
51+
br.close();
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
}
55+
56+
return configJsonText;
57+
}
58+
59+
private static String getConfigFilePath() {
60+
String osName = System.getProperty("os.name").toLowerCase();
61+
String defaultConfigFilePath = null;
62+
boolean isWindows = osName.indexOf("windows") > -1;
63+
boolean isLinux = osName.indexOf("linux") > -1;
64+
65+
if (isWindows) {
66+
defaultConfigFilePath = "C:\\qcloud\\sdk.config";
67+
}
68+
else if (isLinux) {
69+
defaultConfigFilePath = "/etc/qcloud/sdk.config";
70+
}
71+
return defaultConfigFilePath;
72+
}
73+
74+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.qcloud.weapp.demo;
2+
3+
import javax.servlet.ServletConfig;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.annotation.WebServlet;
6+
import javax.servlet.http.HttpServlet;
7+
8+
/**
9+
* Servlet implementation class Startup
10+
*/
11+
@WebServlet(name="Startup", urlPatterns = {}, loadOnStartup = 1)
12+
public class Startup extends HttpServlet {
13+
private static final long serialVersionUID = 1L;
14+
15+
/**
16+
* @see Servlet#init(ServletConfig)
17+
*/
18+
@Override
19+
public void init(ServletConfig config) throws ServletException {
20+
QCloud.setupSDK();
21+
}
22+
}

com.qcloud.weapp.demo/src/com/qcloud/weapp/demo/TunnelServlet.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

com.qcloud.weapp.demo/src/com/qcloud/weapp/demo/LoginServlet.java renamed to com.qcloud.weapp.demo/src/com/qcloud/weapp/demo/servlet/LoginServlet.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.qcloud.weapp.demo;
1+
package com.qcloud.weapp.demo.servlet;
22

33
import java.io.IOException;
44
import javax.servlet.ServletException;
@@ -7,7 +7,7 @@
77
import javax.servlet.http.HttpServletRequest;
88
import javax.servlet.http.HttpServletResponse;
99

10-
10+
import com.qcloud.weapp.ConfigurationException;
1111
import com.qcloud.weapp.authorization.LoginService;
1212
import com.qcloud.weapp.authorization.LoginServiceException;
1313
import com.qcloud.weapp.authorization.UserInfo;
@@ -30,6 +30,8 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
3030
e.printStackTrace();
3131
} catch (LoginServiceException e) {
3232
e.printStackTrace();
33+
} catch (ConfigurationException e) {
34+
e.printStackTrace();
3335
}
3436
}
3537

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.qcloud.weapp.demo.servlet;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.annotation.WebServlet;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
import org.json.JSONException;
13+
import org.json.JSONObject;
14+
15+
import com.qcloud.weapp.ConfigurationException;
16+
import com.qcloud.weapp.authorization.UserInfo;
17+
import com.qcloud.weapp.tunnel.Tunnel;
18+
import com.qcloud.weapp.tunnel.TunnelHandleOptions;
19+
import com.qcloud.weapp.tunnel.TunnelHandler;
20+
import com.qcloud.weapp.tunnel.TunnelMessage;
21+
import com.qcloud.weapp.tunnel.TunnelRoom;
22+
import com.qcloud.weapp.tunnel.TunnelService;
23+
24+
/**
25+
* Servlet implementation class TunnelServlet
26+
*/
27+
@WebServlet("/tunnel")
28+
public class TunnelServlet extends HttpServlet {
29+
private static final long serialVersionUID = -6490955903032763981L;
30+
31+
private static HashMap<String, UserInfo> userMap = new HashMap<String, UserInfo>();
32+
private static TunnelRoom room = new TunnelRoom();
33+
34+
/**
35+
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
36+
*/
37+
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
38+
TunnelService tunnelService = new TunnelService(request, response);
39+
TunnelHandleOptions options = new TunnelHandleOptions();
40+
41+
options.setCheckLogin(true);
42+
43+
try {
44+
tunnelService.handle(new TunnelHandler() {
45+
46+
@Override
47+
public void OnTunnelRequest(Tunnel tunnel, UserInfo userInfo) {
48+
if (tunnel.getTunnelId() == "test") {
49+
userInfo = new UserInfo();
50+
}
51+
if (userInfo != null) {
52+
userMap.put(tunnel.getTunnelId(), userInfo);
53+
}
54+
System.out.println(String.format("Tunnel Connected: %s", tunnel.getTunnelId()));
55+
}
56+
57+
@Override
58+
public void OnTunnelConnect(Tunnel tunnel) {
59+
if (userMap.containsKey(tunnel.getTunnelId())) {
60+
room.addTunnel(tunnel);
61+
JSONObject peopleMessage = new JSONObject();
62+
try {
63+
peopleMessage.put("total", room.getTunnelCount());
64+
peopleMessage.put("enter", new JSONObject(userMap.get(tunnel.getTunnelId())));
65+
} catch (JSONException e) {
66+
e.printStackTrace();
67+
}
68+
room.broadcast("people", peopleMessage);
69+
} else {
70+
tunnel.close();
71+
}
72+
}
73+
74+
@Override
75+
public void OnTunnelMessage(Tunnel tunnel, TunnelMessage message) {
76+
if (message.getType().equals("speak") && userMap.containsKey(tunnel.getTunnelId())) {
77+
JSONObject speakMessage = new JSONObject();
78+
try {
79+
speakMessage.put("word", message.getContent().getString("word"));
80+
speakMessage.put("who", new JSONObject(userMap.get(tunnel.getTunnelId())));
81+
} catch (JSONException e) {
82+
e.printStackTrace();
83+
}
84+
room.broadcast("speak", speakMessage);
85+
} else {
86+
tunnel.close();
87+
}
88+
89+
}
90+
91+
@Override
92+
public void OnTunnelClose(Tunnel tunnel) {
93+
UserInfo leaveUser = null;
94+
if (userMap.containsKey(tunnel.getTunnelId())) {
95+
leaveUser = userMap.get(tunnel.getTunnelId());
96+
userMap.remove(tunnel.getTunnelId());
97+
}
98+
room.removeTunnel(tunnel);
99+
JSONObject peopleMessage = new JSONObject();
100+
try {
101+
peopleMessage.put("total", room.getTunnelCount());
102+
peopleMessage.put("leave", new JSONObject(leaveUser));
103+
} catch (JSONException e) {
104+
e.printStackTrace();
105+
}
106+
room.broadcast("people", peopleMessage);
107+
}
108+
}, options);
109+
} catch (ConfigurationException e) {
110+
e.printStackTrace();
111+
}
112+
}
113+
}

com.qcloud.weapp.demo/src/com/qcloud/weapp/demo/UserServlet.java renamed to com.qcloud.weapp.demo/src/com/qcloud/weapp/demo/servlet/UserServlet.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.qcloud.weapp.demo;
1+
package com.qcloud.weapp.demo.servlet;
22

33
import java.io.IOException;
44
import javax.servlet.ServletException;
@@ -10,6 +10,7 @@
1010
import org.json.JSONException;
1111
import org.json.JSONObject;
1212

13+
import com.qcloud.weapp.ConfigurationException;
1314
import com.qcloud.weapp.authorization.LoginService;
1415
import com.qcloud.weapp.authorization.LoginServiceException;
1516
import com.qcloud.weapp.authorization.UserInfo;
@@ -47,6 +48,8 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
4748
e.printStackTrace();
4849
} catch (JSONException e) {
4950
e.printStackTrace();
51+
} catch (ConfigurationException e) {
52+
e.printStackTrace();
5053
}
5154
}
5255
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.qcloud.weapp;
2+
3+
public class Configuration {
4+
private String serverHost;
5+
private String authServerUrl;
6+
private String tunnelServerUrl;
7+
private String tunnelSignatureKey;
8+
private String networkProxy;
9+
10+
public String getServerHost() {
11+
return serverHost;
12+
}
13+
public void setServerHost(String serverHost) {
14+
this.serverHost = serverHost;
15+
}
16+
public String getAuthServerUrl() {
17+
return authServerUrl;
18+
}
19+
public void setAuthServerUrl(String authServerUrl) {
20+
this.authServerUrl = authServerUrl;
21+
}
22+
public String getTunnelServerUrl() {
23+
return tunnelServerUrl;
24+
}
25+
public void setTunnelServerUrl(String tunnelServerUrl) {
26+
this.tunnelServerUrl = tunnelServerUrl;
27+
}
28+
public String getTunnelSignatureKey() {
29+
return tunnelSignatureKey;
30+
}
31+
public void setTunnelSignatureKey(String tunnelSignatureKey) {
32+
this.tunnelSignatureKey = tunnelSignatureKey;
33+
}
34+
public String getNetworkProxy() {
35+
return networkProxy;
36+
}
37+
public void setNetworkProxy(String networkProxy) {
38+
this.networkProxy = networkProxy;
39+
}
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.qcloud.weapp;
2+
3+
public class ConfigurationException extends Exception {
4+
private static final long serialVersionUID = 570042088042301018L;
5+
6+
public ConfigurationException(String message) {
7+
super(message);
8+
}
9+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.qcloud.weapp;
2+
3+
public class ConfigurationManager {
4+
5+
private static Configuration currentConfiguration;
6+
public static Configuration getCurrentConfiguration() throws ConfigurationException {
7+
if (currentConfiguration == null) {
8+
throw new ConfigurationException("SDK 还没有进行配置,请调用 ConfigurationManager.setup() 方法配置 SDK");
9+
}
10+
return currentConfiguration;
11+
}
12+
13+
public static void setup(Configuration configuration) throws ConfigurationException {
14+
if (configuration == null) {
15+
throw new ConfigurationException("配置不能为空");
16+
}
17+
if (configuration.getServerHost() == null) throw new ConfigurationException("服务器主机配置不能为空");
18+
if (configuration.getAuthServerUrl() == null) throw new ConfigurationException("鉴权服务器配置不能为空");
19+
if (configuration.getTunnelServerUrl() == null) throw new ConfigurationException("信道服务器配置不能为空");
20+
if (configuration.getTunnelSignatureKey() == null) throw new ConfigurationException("SDK 密钥配置不能为空");
21+
currentConfiguration = configuration;
22+
}
23+
}
24+

0 commit comments

Comments
 (0)