Skip to content

Commit 5f5a1d1

Browse files
committed
t
1 parent fd7c3fa commit 5f5a1d1

14 files changed

+482
-47
lines changed

.idea/libraries/Maven__org_jetbrains_annotations_16_0_3.xml

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

cloudtalk-websocket.iml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@
110110
<orderEntry type="library" scope="PROVIDED" name="Maven: org.projectlombok:lombok:1.18.4" level="project" />
111111
<orderEntry type="library" name="Maven: net.sf.ehcache:ehcache:2.10.4" level="project" />
112112
<orderEntry type="library" name="Maven: com.alibaba:druid:1.0.29" level="project" />
113+
<orderEntry type="module-library">
114+
<library name="Maven: com.alibaba:jconsole:1.8.0">
115+
<CLASSES>
116+
<root url="jar:///Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/jconsole.jar!/" />
117+
</CLASSES>
118+
<JAVADOC />
119+
<SOURCES />
120+
</library>
121+
</orderEntry>
122+
<orderEntry type="module-library">
123+
<library name="Maven: com.alibaba:tools:1.8.0">
124+
<CLASSES>
125+
<root url="jar:///Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/tools.jar!/" />
126+
</CLASSES>
127+
<JAVADOC />
128+
<SOURCES />
129+
</library>
130+
</orderEntry>
113131
<orderEntry type="library" scope="RUNTIME" name="Maven: mysql:mysql-connector-java:5.1.46" level="project" />
114132
<orderEntry type="library" name="Maven: com.github.ulisesbocchio:jasypt-spring-boot-starter:1.14" level="project" />
115133
<orderEntry type="library" name="Maven: com.github.ulisesbocchio:jasypt-spring-boot:1.14" level="project" />
@@ -135,10 +153,12 @@
135153
<orderEntry type="library" name="Maven: org.t-io:tio-core:3.2.1.v20181024-RELEASE" level="project" />
136154
<orderEntry type="library" name="Maven: org.t-io:tio-utils:3.2.1.v20181024-RELEASE" level="project" />
137155
<orderEntry type="library" name="Maven: com.github.ben-manes.caffeine:caffeine:2.6.2" level="project" />
138-
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" />
139156
<orderEntry type="library" name="Maven: org.t-io:tio-http-server:3.2.1.v20181024-RELEASE" level="project" />
140157
<orderEntry type="library" name="Maven: com.esotericsoftware:reflectasm:1.11.7" level="project" />
158+
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.4.1" level="project" />
159+
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" />
160+
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.1" level="project" />
141161
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" />
142-
<orderEntry type="library" name="Maven: org.jetbrains:annotations:16.0.3" level="project" />
162+
<orderEntry type="library" name="Maven: org.jetbrains:annotations:17.0.0" level="project" />
143163
</component>
144164
</module>

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@
197197
<artifactId>tio-websocket-server</artifactId>
198198
<version>3.2.1.v20181024-RELEASE</version>
199199
</dependency>
200+
<dependency>
201+
<groupId>org.apache.httpcomponents</groupId>
202+
<artifactId>httpclient</artifactId>
203+
<version>4.4.1</version>
204+
</dependency>
205+
<dependency>
206+
<groupId>org.apache.httpcomponents</groupId>
207+
<artifactId>httpcore</artifactId>
208+
<version>4.4.1</version>
209+
</dependency>
200210

201211
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
202212
<dependency>

src/main/java/com/zhangwuji/im/api/common/ControllerUtil.java

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
package com.zhangwuji.im.api.common;
22

33

4+
import com.alibaba.fastjson.JSONObject;
45
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
56
import com.zhangwuji.im.api.entity.IMUser;
67
import com.zhangwuji.im.api.service.IIMUserService;
8+
import org.apache.http.Header;
9+
import org.apache.http.HttpResponse;
10+
import org.apache.http.HttpStatus;
11+
import org.apache.http.client.HttpClient;
12+
import org.apache.http.client.methods.HttpPost;
13+
import org.apache.http.entity.StringEntity;
14+
import org.apache.http.impl.client.DefaultHttpClient;
15+
import org.apache.http.params.CoreConnectionPNames;
716
import org.springframework.beans.factory.annotation.Qualifier;
17+
import org.springframework.beans.factory.annotation.Value;
818
import org.springframework.stereotype.Component;
919

1020
import javax.annotation.Resource;
1121
import javax.servlet.http.HttpServletRequest;
22+
import java.nio.charset.Charset;
23+
import java.util.HashMap;
24+
import java.util.LinkedHashMap;
25+
import java.util.Map;
26+
import java.util.UUID;
1227

1328
@Component
1429
public class ControllerUtil {
@@ -17,6 +32,108 @@ public class ControllerUtil {
1732
@Qualifier(value = "imUserService")
1833
private IIMUserService iOnImuserService;
1934

35+
@Value("${cloudtalk.api.url}")
36+
public String cloudTalkHttpApi;
37+
38+
39+
public void sendIMSystemMessage(int cmd,int uid,String msg)
40+
{
41+
Map<String, Object> p= new LinkedHashMap<>();
42+
p.put("app_key","asdfsdf");
43+
p.put("req_user_id",1);
44+
p.put("to_session_id",uid);
45+
p.put("msg_type",cmd);
46+
p.put("msg_data",msg);
47+
p.put("from_user_id",1);
48+
String poststr=new JSONObject(p).toJSONString();
49+
50+
httpPostWithJson(poststr);
51+
}
52+
53+
54+
public boolean httpPostWithJson(String msg){
55+
boolean isSuccess = false;
56+
HttpPost post = null;
57+
try {
58+
HttpClient httpClient = new DefaultHttpClient();
59+
60+
// 设置超时时间
61+
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
62+
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);
63+
64+
post = new HttpPost(cloudTalkHttpApi);
65+
// 构造消息头
66+
post.setHeader("Content-type", "application/json; charset=utf-8");
67+
post.setHeader("Connection", "Close");
68+
String sessionId = getSessionId();
69+
post.setHeader("SessionId", sessionId);
70+
71+
// 构建消息实体
72+
StringEntity entity = new StringEntity(msg, Charset.forName("UTF-8"));
73+
entity.setContentEncoding("UTF-8");
74+
// 发送Json格式的数据请求
75+
entity.setContentType("application/json");
76+
post.setEntity(entity);
77+
78+
HttpResponse response = httpClient.execute(post);
79+
80+
// 检验返回码
81+
int statusCode = response.getStatusLine().getStatusCode();
82+
if(statusCode != HttpStatus.SC_OK){
83+
isSuccess = false;
84+
}else{
85+
int retCode = 0;
86+
String sessendId = "";
87+
// 返回码中包含retCode及会话Id
88+
for(Header header : response.getAllHeaders()){
89+
if(header.getName().equals("retcode")){
90+
retCode = Integer.parseInt(header.getValue());
91+
}
92+
if(header.getName().equals("SessionId")){
93+
sessendId = header.getValue();
94+
}
95+
}
96+
isSuccess=true;
97+
}
98+
} catch (Exception e) {
99+
e.printStackTrace();
100+
isSuccess = false;
101+
}finally{
102+
if(post != null){
103+
try {
104+
post.releaseConnection();
105+
Thread.sleep(500);
106+
} catch (InterruptedException e) {
107+
e.printStackTrace();
108+
}
109+
}
110+
}
111+
return isSuccess;
112+
}
113+
114+
115+
public int getRandom(int count) {
116+
return (int) Math.round(Math.random() * (count));
117+
}
118+
119+
public String string = "abcdefghijklmnopqrstuvwxyzABCDEFG123456789";
120+
121+
public String getRandomString(int length){
122+
StringBuffer sb = new StringBuffer();
123+
int len = string.length();
124+
for (int i = 0; i < length; i++) {
125+
sb.append(string.charAt(getRandom(len-1)));
126+
}
127+
return sb.toString();
128+
}
129+
130+
// 构建唯一会话Id
131+
public static String getSessionId(){
132+
UUID uuid = UUID.randomUUID();
133+
String str = uuid.toString();
134+
return str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
135+
}
136+
20137
public int getIntParameter(HttpServletRequest req,String key,int def)
21138
{
22139
String value=req.getParameter(key);
@@ -74,5 +191,9 @@ public Long timestamp() {
74191
String timestamp = String.format("%010d", timeStampSec);
75192
return Long.parseLong(timestamp);
76193
}
77-
194+
public Integer timestamp2() {
195+
long timeStampSec = System.currentTimeMillis()/1000;
196+
String timestamp = String.format("%010d", timeStampSec);
197+
return Integer.parseInt(timestamp);
198+
}
78199
}

0 commit comments

Comments
 (0)